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.
Homework2.cpp@0:368cb048f407, 2022-10-07 (annotated)
- Committer:
- michael_antonucci
- Date:
- Fri Oct 07 03:50:32 2022 +0000
- Revision:
- 0:368cb048f407
Fully commented and working code. GRADE THIS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michael_antonucci | 0:368cb048f407 | 1 | #include "mbed.h" |
michael_antonucci | 0:368cb048f407 | 2 | |
michael_antonucci | 0:368cb048f407 | 3 | DigitalIn button(p17); // Sets input to pin 17 |
michael_antonucci | 0:368cb048f407 | 4 | DigitalOut led1(LED1); // defines the first output to LED1 |
michael_antonucci | 0:368cb048f407 | 5 | DigitalOut led2(LED2); // defines the second output to LED2 |
michael_antonucci | 0:368cb048f407 | 6 | Serial pc(USBTX,USBRX); |
michael_antonucci | 0:368cb048f407 | 7 | int counter; // defines an integer to counter |
michael_antonucci | 0:368cb048f407 | 8 | int x; // defines an integer to x |
michael_antonucci | 0:368cb048f407 | 9 | |
michael_antonucci | 0:368cb048f407 | 10 | int main() |
michael_antonucci | 0:368cb048f407 | 11 | { |
michael_antonucci | 0:368cb048f407 | 12 | while(1) { |
michael_antonucci | 0:368cb048f407 | 13 | x=button.read(); // "x" is equal to 1 or 0, depending on the state of the button |
michael_antonucci | 0:368cb048f407 | 14 | if(counter>=10) { // when the integer "counter" is greater than or equal to 10, LED1 turns on |
michael_antonucci | 0:368cb048f407 | 15 | led1=1; |
michael_antonucci | 0:368cb048f407 | 16 | } else { // if the integer "x" is less than 10, LED1 is off |
michael_antonucci | 0:368cb048f407 | 17 | led1=0; |
michael_antonucci | 0:368cb048f407 | 18 | } |
michael_antonucci | 0:368cb048f407 | 19 | if (x==1) { // if "x" equals one, LED2 turns on and off at one second frequency |
michael_antonucci | 0:368cb048f407 | 20 | led2=1; |
michael_antonucci | 0:368cb048f407 | 21 | wait(1); |
michael_antonucci | 0:368cb048f407 | 22 | led2=0; |
michael_antonucci | 0:368cb048f407 | 23 | wait(1); |
michael_antonucci | 0:368cb048f407 | 24 | } else { // if "x" doesn't equal one (i.e. it equals zero), LED2 turns on and off at a 0.3 second frequency |
michael_antonucci | 0:368cb048f407 | 25 | led2=1; |
michael_antonucci | 0:368cb048f407 | 26 | wait(0.3); |
michael_antonucci | 0:368cb048f407 | 27 | led2=0; |
michael_antonucci | 0:368cb048f407 | 28 | wait(0.3); |
michael_antonucci | 0:368cb048f407 | 29 | } |
michael_antonucci | 0:368cb048f407 | 30 | if (button.read() != x) { // for every change in the button's state, 1 is added to the integer "counter" |
michael_antonucci | 0:368cb048f407 | 31 | counter=counter+1; |
michael_antonucci | 0:368cb048f407 | 32 | pc.printf("Instance: %i\r\n", counter); |
michael_antonucci | 0:368cb048f407 | 33 | |
michael_antonucci | 0:368cb048f407 | 34 | } |
michael_antonucci | 0:368cb048f407 | 35 | } |
michael_antonucci | 0:368cb048f407 | 36 | } |