Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@3:896a932d47fc, 2022-10-06 (annotated)
- Committer:
- a_ferguson
- Date:
- Thu Oct 06 16:36:02 2022 +0000
- Revision:
- 3:896a932d47fc
- Parent:
- 2:af576070220a
Complete version to be graded (fixed switch bouncing issue)
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| a_ferguson | 0:1cf58db95ce6 | 1 | #include "mbed.h" |
| a_ferguson | 0:1cf58db95ce6 | 2 | |
| a_ferguson | 3:896a932d47fc | 3 | Serial pc(USBTX,USBRX); // do not need when we aren't communicating with pc |
| a_ferguson | 3:896a932d47fc | 4 | DigitalOut myled1(LED1); //LEDs 1 and 2 |
| a_ferguson | 1:c186d9b63164 | 5 | DigitalOut myled2(LED2); |
| a_ferguson | 0:1cf58db95ce6 | 6 | DigitalIn button(p17); // button attached to p17 |
| a_ferguson | 3:896a932d47fc | 7 | Timer led2timer; // create timer |
| a_ferguson | 0:1cf58db95ce6 | 8 | |
| a_ferguson | 0:1cf58db95ce6 | 9 | int main() { |
| a_ferguson | 0:1cf58db95ce6 | 10 | |
| a_ferguson | 3:896a932d47fc | 11 | int currentstate=0; // set variables as integers |
| a_ferguson | 2:af576070220a | 12 | int previousstate=0; |
| a_ferguson | 2:af576070220a | 13 | int counter=0; |
| a_ferguson | 2:af576070220a | 14 | |
| a_ferguson | 3:896a932d47fc | 15 | led2timer.start(); // starts timer |
| a_ferguson | 3:896a932d47fc | 16 | |
| a_ferguson | 0:1cf58db95ce6 | 17 | while(1) { |
| a_ferguson | 2:af576070220a | 18 | |
| a_ferguson | 2:af576070220a | 19 | currentstate=button.read(); // set current state |
| a_ferguson | 2:af576070220a | 20 | |
| a_ferguson | 3:896a932d47fc | 21 | if (currentstate==1){ // button pressed |
| a_ferguson | 3:896a932d47fc | 22 | if (led2timer.read()>=1){ // blinks 1s interval when pressed |
| a_ferguson | 3:896a932d47fc | 23 | myled2=!myled2; |
| a_ferguson | 3:896a932d47fc | 24 | led2timer.reset(); // reset timer |
| a_ferguson | 3:896a932d47fc | 25 | } |
| a_ferguson | 2:af576070220a | 26 | |
| a_ferguson | 3:896a932d47fc | 27 | if (currentstate==previousstate){ // no change |
| a_ferguson | 2:af576070220a | 28 | } |
| a_ferguson | 2:af576070220a | 29 | |
| a_ferguson | 2:af576070220a | 30 | else{ // change in state adds counter |
| a_ferguson | 2:af576070220a | 31 | counter=counter+1; |
| a_ferguson | 2:af576070220a | 32 | pc.printf("count=%i \r\n",counter); |
| a_ferguson | 2:af576070220a | 33 | } |
| a_ferguson | 2:af576070220a | 34 | |
| a_ferguson | 3:896a932d47fc | 35 | } // if |
| a_ferguson | 2:af576070220a | 36 | |
| a_ferguson | 3:896a932d47fc | 37 | else { // button released |
| a_ferguson | 3:896a932d47fc | 38 | if (led2timer.read()>=0.3){ // blinks 0.3s interval when released |
| a_ferguson | 3:896a932d47fc | 39 | myled2=!myled2; |
| a_ferguson | 3:896a932d47fc | 40 | led2timer.reset(); // reset timer |
| a_ferguson | 3:896a932d47fc | 41 | } |
| a_ferguson | 2:af576070220a | 42 | |
| a_ferguson | 3:896a932d47fc | 43 | if (currentstate==previousstate){ // no change |
| a_ferguson | 2:af576070220a | 44 | } |
| a_ferguson | 2:af576070220a | 45 | |
| a_ferguson | 2:af576070220a | 46 | else{ // change in state adds counter |
| a_ferguson | 2:af576070220a | 47 | counter=counter+1; |
| a_ferguson | 2:af576070220a | 48 | pc.printf("count=%i \r\n",counter); |
| a_ferguson | 2:af576070220a | 49 | } |
| a_ferguson | 2:af576070220a | 50 | |
| a_ferguson | 3:896a932d47fc | 51 | } // else |
| a_ferguson | 2:af576070220a | 52 | |
| a_ferguson | 2:af576070220a | 53 | if (counter==10){ // counter reaches 10 turns on led1 |
| a_ferguson | 2:af576070220a | 54 | myled1=1; |
| a_ferguson | 3:896a932d47fc | 55 | } // if |
| a_ferguson | 2:af576070220a | 56 | |
| a_ferguson | 3:896a932d47fc | 57 | previousstate=currentstate; //set previous state at loop end |
| a_ferguson | 2:af576070220a | 58 | |
| a_ferguson | 3:896a932d47fc | 59 | wait(0.1); // prevents the counter from counting switch bounces |
| a_ferguson | 3:896a932d47fc | 60 | // wait is ok because it it shorter than the timer 0.3 |
| a_ferguson | 3:896a932d47fc | 61 | |
| a_ferguson | 3:896a932d47fc | 62 | } // while |
| a_ferguson | 2:af576070220a | 63 | |
| a_ferguson | 3:896a932d47fc | 64 | } // main |
