Lab1 sent: 26/10/2561

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 DigitalIn sw1(USER_BUTTON);
00003 DigitalIn sw2(D15);
00004 DigitalIn sw3(D14);
00005 DigitalOut leds[] = {PA_0,PA_1,PA_4,PB_0,PC_1,PC_0,PA_8,PB_10};
00006 void blink(DigitalOut a,float time)// Led Blink
00007 {
00008     a=1;
00009     wait(time);
00010     a=0;
00011     wait(time);
00012 }
00013 void pattern1(void)
00014 {
00015     for(int k=0; k<=7; k++) {   //Led Blink 2 times from d0 to d7
00016         blink(leds[k],0.1);
00017         blink(leds[k],0.1);
00018         wait(0.01);
00019     }
00020 }
00021 void pattern2(void)
00022 {
00023     for(int i=0; i<=7; i++) {   //Led on from d0 to d7
00024         leds[i]=1;
00025         wait(0.1);
00026     }
00027     for(int j=0; j<=7; j++) {  //Led off from d0 to d7
00028         leds[j]=0;
00029         wait(0.1);
00030     }
00031 }
00032 void pattern3(void)
00033 {
00034     for(int k=0; k<=3; k++) {   //Led light on from d0 to d3 and from d7 to d4
00035         leds[k]=1;
00036         leds[7-k]=1;
00037         wait(0.1);
00038     }
00039     for(int x=0; x<=3; x++) {   //Led light off from d0 to d3 and from d7 to d4
00040         leds[x]=0;
00041         leds[7-x]=0;
00042         wait(0.1);
00043     }
00044 }
00045 int main()
00046 {
00047     while(1) {
00048         if (sw2==1) {
00049             pattern2();         //Call Pattern2
00050         } else if (sw1==0) {
00051             pattern1();         //Call Pattern1
00052         } else if(sw3==1) {
00053             pattern3();         //Call Pattern3
00054         }
00055     }
00056 }
00057 
00058