Fully commented code. GRADE THIS
Embed:
(wiki syntax)
Show/hide line numbers
Homework2.cpp
00001 #include "mbed.h" 00002 00003 DigitalIn button(p17); // Sets input to pin 17 00004 DigitalOut led1(LED1); // defines the first output to LED1 00005 DigitalOut led2(LED2); // defines the second output to LED2 00006 Serial pc(USBTX,USBRX); 00007 int counter; // defines an integer to counter 00008 int x; // defines an integer to x 00009 00010 int main() 00011 { 00012 while(1) { 00013 x=button.read(); // "x" is equal to 1 or 0, depending on the state of the button 00014 if(counter>=10) { // when the integer "counter" is greater than or equal to 10, LED1 turns on 00015 led1=1; 00016 } else { // if the integer "x" is less than 10, LED1 is off 00017 led1=0; 00018 } 00019 if (x==1) { // if "x" equals one, LED2 turns on and off at one second frequency 00020 led2=1; 00021 wait(1); 00022 led2=0; 00023 wait(1); 00024 } else { // if "x" doesn't equal one (i.e. it equals zero), LED2 turns on and off at a 0.3 second frequency 00025 led2=1; 00026 wait(0.3); 00027 led2=0; 00028 wait(0.3); 00029 } 00030 if (button.read() != x) { // for every change in the button's state, 1 is added to the integer "counter" 00031 counter=counter+1; 00032 pc.printf("Instance: %i\r\n", counter); 00033 00034 } 00035 } 00036 }
Generated on Fri Oct 7 2022 03:51:07 by
1.7.2
