Assignment3.2 code. Debouncing is still an issue. GRADE THIS

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Assignment3_2.cpp Source File

Assignment3_2.cpp

00001 //Blinks LED2 every 200ms and LED3 every 300ms using two Ticker objects and turns LED1 on and off using a hardware interrupt.
00002 //Created: M. Antonucci 10/13/2022(Modified S. Licht code)
00003 #include "mbed.h"
00004 Serial pc(USBTX, USBRX); //tx, rx
00005 Ticker tickerLED2;  //create ticker object
00006 Ticker tickerLED3;  //create ticker object
00007 Timer debounce;   //create timer object
00008 Timer elapsed;   //create timer object
00009 InterruptIn button(p17);   //setup interrupt for the button
00010 DigitalOut LEDOut1(LED1);   //identify the leds being used
00011 DigitalOut LEDOut2(LED2);
00012 DigitalOut LEDOut3(LED3);
00013 
00014 void changeLED2()  //the function that will be called by the ticker object.
00015 {
00016     LEDOut2 = !LEDOut2;
00017 }
00018 
00019 void changeLED3()  //the function that will be called by the ticker object.
00020 {
00021     LEDOut3 = !LEDOut3;
00022 }
00023 
00024 void changeLED1()   //the function that will be called by the interrupt object.
00025 {
00026     LEDOut1 = !LEDOut1;
00027     pc.printf("%f\r\n",elapsed.read());
00028 }
00029 
00030 void toggle(void);   //referencing the 'void' that is after the main code
00031 
00032 int main()
00033 {
00034     debounce.start();   //start the debounce timer
00035     elapsed.start();   //starts the code run time timer
00036     tickerLED2.attach(&changeLED2,0.2); //the address of the function to call
00037     //and the interval in seconds between
00038     //calls to that function
00039     tickerLED3.attach(&changeLED3,0.3);
00040 
00041     button.fall(&changeLED1);   //When button voltage falls, the LED changes
00042 
00043     while(1) {
00044         //wait(.1);
00045         //wait(.1);
00046         //wait(.1);
00047         //the main loop is spinning every 300ms, but the LED needs to go faster!
00048     } //while
00049 
00050 }
00051 
00052 int db = 0;   //defines a variable for debounce
00053 
00054 void toggle() {
00055     db = debounce.read_ms();   //defines db as the value of the debounce timer
00056     if (db>5)
00057     LEDOut1 = !LEDOut1;   //stops the LED from switching if the debounce timer is too low
00058     pc.printf("%i\r\n",db);   //prints db
00059     debounce.reset();   //reset the debounce timer each time the button is pressed with a gap of more than 5ms 
00060 }