6.4 Wenn sich jemand nähert, Lauflicht einschalten und Akustisches Signal
Dependencies: mbed
Fork of 06-04-Uebung by
main.cpp@5:8435f2075875, 2016-10-31 (annotated)
- Committer:
- stefan1691
- Date:
- Mon Oct 31 15:10:53 2016 +0000
- Revision:
- 5:8435f2075875
- Parent:
- 4:a470b7ed1f78
A5 statt A4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stefan1691 | 3:713da1a5879b | 1 | /** 6.4 Wenn sich jemand nähert, Lauflicht einschalten und Akustisches Signal |
stefan1691 | 0:a2dc2e18687d | 2 | */ |
stefan1691 | 0:a2dc2e18687d | 3 | #include "mbed.h" |
stefan1691 | 0:a2dc2e18687d | 4 | |
stefan1691 | 0:a2dc2e18687d | 5 | DigitalOut led1( D10 ); |
stefan1691 | 0:a2dc2e18687d | 6 | DigitalOut led2( D11 ); |
stefan1691 | 0:a2dc2e18687d | 7 | DigitalOut led3( D12 ); |
stefan1691 | 0:a2dc2e18687d | 8 | DigitalOut led4( D13 ); |
stefan1691 | 4:a470b7ed1f78 | 9 | PwmOut buzzer( D7 ); // Buzzer |
stefan1691 | 0:a2dc2e18687d | 10 | |
stefan1691 | 5:8435f2075875 | 11 | DigitalIn pirSensor( A5 ); // Bewegungsmelder schaltet auf 1 |
stefan1691 | 2:e5add5104ed3 | 12 | |
stefan1691 | 0:a2dc2e18687d | 13 | int main() |
stefan1691 | 0:a2dc2e18687d | 14 | { |
stefan1691 | 0:a2dc2e18687d | 15 | while (true) |
stefan1691 | 0:a2dc2e18687d | 16 | { |
stefan1691 | 3:713da1a5879b | 17 | // hat sich etwas bewegt? |
stefan1691 | 3:713da1a5879b | 18 | if ( pirSensor ) |
stefan1691 | 0:a2dc2e18687d | 19 | { |
stefan1691 | 3:713da1a5879b | 20 | // 10x Lauflicht durchlaufen |
stefan1691 | 3:713da1a5879b | 21 | for ( int t = 0; t < 10; t++ ) |
stefan1691 | 0:a2dc2e18687d | 22 | { |
stefan1691 | 3:713da1a5879b | 23 | // Buzzer bleibt eingeschaltet werden die LED ON sind |
stefan1691 | 4:a470b7ed1f78 | 24 | buzzer.period( 1.0/900.0) ; // 900hz period |
stefan1691 | 4:a470b7ed1f78 | 25 | buzzer = 0.9; // 90% duty cycle - max volume |
stefan1691 | 3:713da1a5879b | 26 | for ( int i = 0; i < 5; i++ ) |
stefan1691 | 2:e5add5104ed3 | 27 | { |
stefan1691 | 3:713da1a5879b | 28 | switch ( i ) |
stefan1691 | 3:713da1a5879b | 29 | { |
stefan1691 | 3:713da1a5879b | 30 | case 0: |
stefan1691 | 3:713da1a5879b | 31 | led1 = led2 = led3 = led4 = 0; |
stefan1691 | 3:713da1a5879b | 32 | break; |
stefan1691 | 3:713da1a5879b | 33 | case 1: |
stefan1691 | 3:713da1a5879b | 34 | led1 = 1; |
stefan1691 | 3:713da1a5879b | 35 | break; |
stefan1691 | 3:713da1a5879b | 36 | case 2: |
stefan1691 | 3:713da1a5879b | 37 | led1 = 0; |
stefan1691 | 3:713da1a5879b | 38 | led2 = 1; |
stefan1691 | 3:713da1a5879b | 39 | break; |
stefan1691 | 3:713da1a5879b | 40 | case 3: |
stefan1691 | 3:713da1a5879b | 41 | led2 = 0; |
stefan1691 | 3:713da1a5879b | 42 | led3 = 1; |
stefan1691 | 3:713da1a5879b | 43 | break; |
stefan1691 | 3:713da1a5879b | 44 | case 4: |
stefan1691 | 3:713da1a5879b | 45 | led3 = 0; |
stefan1691 | 3:713da1a5879b | 46 | led4 = 1; |
stefan1691 | 3:713da1a5879b | 47 | break; |
stefan1691 | 3:713da1a5879b | 48 | } |
stefan1691 | 3:713da1a5879b | 49 | // wait bis naechstes LED aktiviert wird |
stefan1691 | 3:713da1a5879b | 50 | wait( 0.2 ); |
stefan1691 | 2:e5add5104ed3 | 51 | } |
stefan1691 | 3:713da1a5879b | 52 | // Ruhe und alle LED off |
stefan1691 | 3:713da1a5879b | 53 | buzzer = 0; |
stefan1691 | 3:713da1a5879b | 54 | led4 = 0; |
stefan1691 | 3:713da1a5879b | 55 | wait ( 0.5 ); |
stefan1691 | 0:a2dc2e18687d | 56 | } |
stefan1691 | 3:713da1a5879b | 57 | } else |
stefan1691 | 3:713da1a5879b | 58 | { |
stefan1691 | 3:713da1a5879b | 59 | led1 = led2 = led3 = led4 = 0; |
stefan1691 | 3:713da1a5879b | 60 | buzzer = 0; |
stefan1691 | 2:e5add5104ed3 | 61 | } |
stefan1691 | 3:713da1a5879b | 62 | |
stefan1691 | 3:713da1a5879b | 63 | wait( 0.2 ); // ohne wait laesst sich das Board nicht neu programmieren! |
stefan1691 | 0:a2dc2e18687d | 64 | } |
stefan1691 | 0:a2dc2e18687d | 65 | } |