Lab1 sent: 26/10/2561
Dependencies: mbed
main.cpp@0:faea7b90370e, 2018-10-19 (annotated)
- Committer:
- kong4580
- Date:
- Fri Oct 19 12:58:49 2018 +0000
- Revision:
- 0:faea7b90370e
Test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kong4580 | 0:faea7b90370e | 1 | #include "mbed.h" |
kong4580 | 0:faea7b90370e | 2 | DigitalIn sw1(USER_BUTTON); |
kong4580 | 0:faea7b90370e | 3 | DigitalIn sw2(D15); |
kong4580 | 0:faea7b90370e | 4 | DigitalIn sw3(D14); |
kong4580 | 0:faea7b90370e | 5 | DigitalOut leds[] = {PA_0,PA_1,PA_4,PB_0,PC_1,PC_0,PA_8,PB_10}; |
kong4580 | 0:faea7b90370e | 6 | void blink(DigitalOut a,float time)// Led Blink |
kong4580 | 0:faea7b90370e | 7 | { |
kong4580 | 0:faea7b90370e | 8 | a=1; |
kong4580 | 0:faea7b90370e | 9 | wait(time); |
kong4580 | 0:faea7b90370e | 10 | a=0; |
kong4580 | 0:faea7b90370e | 11 | wait(time); |
kong4580 | 0:faea7b90370e | 12 | } |
kong4580 | 0:faea7b90370e | 13 | void pattern1(void) |
kong4580 | 0:faea7b90370e | 14 | { |
kong4580 | 0:faea7b90370e | 15 | for(int k=0; k<=7; k++) { //Led Blink 2 times from d0 to d7 |
kong4580 | 0:faea7b90370e | 16 | blink(leds[k],0.1); |
kong4580 | 0:faea7b90370e | 17 | blink(leds[k],0.1); |
kong4580 | 0:faea7b90370e | 18 | wait(0.01); |
kong4580 | 0:faea7b90370e | 19 | } |
kong4580 | 0:faea7b90370e | 20 | } |
kong4580 | 0:faea7b90370e | 21 | void pattern2(void) |
kong4580 | 0:faea7b90370e | 22 | { |
kong4580 | 0:faea7b90370e | 23 | for(int i=0; i<=7; i++) { //Led on from d0 to d7 |
kong4580 | 0:faea7b90370e | 24 | leds[i]=1; |
kong4580 | 0:faea7b90370e | 25 | wait(0.1); |
kong4580 | 0:faea7b90370e | 26 | } |
kong4580 | 0:faea7b90370e | 27 | for(int j=0; j<=7; j++) { //Led off from d0 to d7 |
kong4580 | 0:faea7b90370e | 28 | leds[j]=0; |
kong4580 | 0:faea7b90370e | 29 | wait(0.1); |
kong4580 | 0:faea7b90370e | 30 | } |
kong4580 | 0:faea7b90370e | 31 | } |
kong4580 | 0:faea7b90370e | 32 | void pattern3(void) |
kong4580 | 0:faea7b90370e | 33 | { |
kong4580 | 0:faea7b90370e | 34 | for(int k=0; k<=3; k++) { //Led light on from d0 to d3 and from d7 to d4 |
kong4580 | 0:faea7b90370e | 35 | leds[k]=1; |
kong4580 | 0:faea7b90370e | 36 | leds[7-k]=1; |
kong4580 | 0:faea7b90370e | 37 | wait(0.1); |
kong4580 | 0:faea7b90370e | 38 | } |
kong4580 | 0:faea7b90370e | 39 | for(int x=0; x<=3; x++) { //Led light off from d0 to d3 and from d7 to d4 |
kong4580 | 0:faea7b90370e | 40 | leds[x]=0; |
kong4580 | 0:faea7b90370e | 41 | leds[7-x]=0; |
kong4580 | 0:faea7b90370e | 42 | wait(0.1); |
kong4580 | 0:faea7b90370e | 43 | } |
kong4580 | 0:faea7b90370e | 44 | } |
kong4580 | 0:faea7b90370e | 45 | int main() |
kong4580 | 0:faea7b90370e | 46 | { |
kong4580 | 0:faea7b90370e | 47 | while(1) { |
kong4580 | 0:faea7b90370e | 48 | if (sw2==1) { |
kong4580 | 0:faea7b90370e | 49 | pattern2(); //Call Pattern2 |
kong4580 | 0:faea7b90370e | 50 | } else if (sw1==0) { |
kong4580 | 0:faea7b90370e | 51 | pattern1(); //Call Pattern1 |
kong4580 | 0:faea7b90370e | 52 | } else if(sw3==1) { |
kong4580 | 0:faea7b90370e | 53 | pattern3(); //Call Pattern3 |
kong4580 | 0:faea7b90370e | 54 | } |
kong4580 | 0:faea7b90370e | 55 | } |
kong4580 | 0:faea7b90370e | 56 | } |
kong4580 | 0:faea7b90370e | 57 | |
kong4580 | 0:faea7b90370e | 58 |