Michael Antonucci / Mbed 2 deprecated A3_2_Ticker

Dependencies:   mbed

Committer:
michael_antonucci
Date:
Fri Oct 14 13:19:46 2022 +0000
Revision:
3:e0ef8152a0f6
Still not understanding how to debounce the switch, but the code is fully functional besides the switch bouncing. GRADE THIS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
michael_antonucci 3:e0ef8152a0f6 1 //Blinks LED2 every 200ms and LED3 every 300ms using two Ticker objects and turns LED1 on and off using a hardware interrupt.
michael_antonucci 3:e0ef8152a0f6 2 //Created: M. Antonucci 10/13/2022(Modified S. Licht code)
michael_antonucci 3:e0ef8152a0f6 3 #include "mbed.h"
michael_antonucci 3:e0ef8152a0f6 4 Serial pc(USBTX, USBRX); //tx, rx
michael_antonucci 3:e0ef8152a0f6 5 Ticker tickerLED2; //create ticker object
michael_antonucci 3:e0ef8152a0f6 6 Ticker tickerLED3; //create ticker object
michael_antonucci 3:e0ef8152a0f6 7 Timer debounce; //create timer object
michael_antonucci 3:e0ef8152a0f6 8 Timer elapsed; //create timer object
michael_antonucci 3:e0ef8152a0f6 9 InterruptIn button(p17); //setup interrupt for the button
michael_antonucci 3:e0ef8152a0f6 10 DigitalOut LEDOut1(LED1); //identify the leds being used
michael_antonucci 3:e0ef8152a0f6 11 DigitalOut LEDOut2(LED2);
michael_antonucci 3:e0ef8152a0f6 12 DigitalOut LEDOut3(LED3);
michael_antonucci 3:e0ef8152a0f6 13
michael_antonucci 3:e0ef8152a0f6 14 void changeLED2() //the function that will be called by the ticker object.
michael_antonucci 3:e0ef8152a0f6 15 {
michael_antonucci 3:e0ef8152a0f6 16 LEDOut2 = !LEDOut2;
michael_antonucci 3:e0ef8152a0f6 17 }
michael_antonucci 3:e0ef8152a0f6 18
michael_antonucci 3:e0ef8152a0f6 19 void changeLED3() //the function that will be called by the ticker object.
michael_antonucci 3:e0ef8152a0f6 20 {
michael_antonucci 3:e0ef8152a0f6 21 LEDOut3 = !LEDOut3;
michael_antonucci 3:e0ef8152a0f6 22 }
michael_antonucci 3:e0ef8152a0f6 23
michael_antonucci 3:e0ef8152a0f6 24 void changeLED1() //the function that will be called by the interrupt object.
michael_antonucci 3:e0ef8152a0f6 25 {
michael_antonucci 3:e0ef8152a0f6 26 LEDOut1 = !LEDOut1;
michael_antonucci 3:e0ef8152a0f6 27 pc.printf("%f\r\n",elapsed.read());
michael_antonucci 3:e0ef8152a0f6 28 }
michael_antonucci 3:e0ef8152a0f6 29
michael_antonucci 3:e0ef8152a0f6 30 void toggle(void); //referencing the 'void' that is after the main code
michael_antonucci 3:e0ef8152a0f6 31
michael_antonucci 3:e0ef8152a0f6 32 int main()
michael_antonucci 3:e0ef8152a0f6 33 {
michael_antonucci 3:e0ef8152a0f6 34 debounce.start(); //start the debounce timer
michael_antonucci 3:e0ef8152a0f6 35 elapsed.start(); //starts the code run time timer
michael_antonucci 3:e0ef8152a0f6 36 tickerLED2.attach(&changeLED2,0.2); //the address of the function to call
michael_antonucci 3:e0ef8152a0f6 37 //and the interval in seconds between
michael_antonucci 3:e0ef8152a0f6 38 //calls to that function
michael_antonucci 3:e0ef8152a0f6 39 tickerLED3.attach(&changeLED3,0.3);
michael_antonucci 3:e0ef8152a0f6 40
michael_antonucci 3:e0ef8152a0f6 41 button.fall(&changeLED1); //When button voltage falls, the LED changes
michael_antonucci 3:e0ef8152a0f6 42
michael_antonucci 3:e0ef8152a0f6 43 while(1) {
michael_antonucci 3:e0ef8152a0f6 44 //wait(.1);
michael_antonucci 3:e0ef8152a0f6 45 //wait(.1);
michael_antonucci 3:e0ef8152a0f6 46 //wait(.1);
michael_antonucci 3:e0ef8152a0f6 47 //the main loop is spinning every 300ms, but the LED needs to go faster!
michael_antonucci 3:e0ef8152a0f6 48 } //while
michael_antonucci 3:e0ef8152a0f6 49
michael_antonucci 3:e0ef8152a0f6 50 }
michael_antonucci 3:e0ef8152a0f6 51
michael_antonucci 3:e0ef8152a0f6 52 int db = 0; //defines a variable for debounce
michael_antonucci 3:e0ef8152a0f6 53
michael_antonucci 3:e0ef8152a0f6 54 void toggle() {
michael_antonucci 3:e0ef8152a0f6 55 db = debounce.read_ms(); //defines db as the value of the debounce timer
michael_antonucci 3:e0ef8152a0f6 56 if (db>5)
michael_antonucci 3:e0ef8152a0f6 57 LEDOut1 = !LEDOut1; //stops the LED from switching if the debounce timer is too low
michael_antonucci 3:e0ef8152a0f6 58 pc.printf("%i\r\n",db); //prints db
michael_antonucci 3:e0ef8152a0f6 59 debounce.reset(); //reset the debounce timer each time the button is pressed with a gap of more than 5ms
michael_antonucci 3:e0ef8152a0f6 60 }