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

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
michael_antonucci
Date:
Fri Oct 14 13:19:46 2022 +0000
Parent:
2:302216e564bf
Commit message:
Still not understanding how to debounce the switch, but the code is fully functional besides the switch bouncing. GRADE THIS

Changed in this revision

A3_1_Single_Timer.lib Show diff for this revision Revisions of this file
Assignment3_2.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show diff for this revision Revisions of this file
--- a/A3_1_Single_Timer.lib	Tue Oct 11 13:38:14 2022 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://os.mbed.com/users/slicht_instructor/code/A3_1_Single_Timer/#5597320f2dba
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Assignment3_2.cpp	Fri Oct 14 13:19:46 2022 +0000
@@ -0,0 +1,60 @@
+//Blinks LED2 every 200ms and LED3 every 300ms using two Ticker objects and turns LED1 on and off using a hardware interrupt.
+//Created: M. Antonucci 10/13/2022(Modified S. Licht code)
+#include "mbed.h"
+Serial pc(USBTX, USBRX); //tx, rx
+Ticker tickerLED2;  //create ticker object
+Ticker tickerLED3;  //create ticker object
+Timer debounce;   //create timer object
+Timer elapsed;   //create timer object
+InterruptIn button(p17);   //setup interrupt for the button
+DigitalOut LEDOut1(LED1);   //identify the leds being used
+DigitalOut LEDOut2(LED2);
+DigitalOut LEDOut3(LED3);
+
+void changeLED2()  //the function that will be called by the ticker object.
+{
+    LEDOut2 = !LEDOut2;
+}
+
+void changeLED3()  //the function that will be called by the ticker object.
+{
+    LEDOut3 = !LEDOut3;
+}
+
+void changeLED1()   //the function that will be called by the interrupt object.
+{
+    LEDOut1 = !LEDOut1;
+    pc.printf("%f\r\n",elapsed.read());
+}
+
+void toggle(void);   //referencing the 'void' that is after the main code
+
+int main()
+{
+    debounce.start();   //start the debounce timer
+    elapsed.start();   //starts the code run time timer
+    tickerLED2.attach(&changeLED2,0.2); //the address of the function to call
+    //and the interval in seconds between
+    //calls to that function
+    tickerLED3.attach(&changeLED3,0.3);
+
+    button.fall(&changeLED1);   //When button voltage falls, the LED changes
+
+    while(1) {
+        //wait(.1);
+        //wait(.1);
+        //wait(.1);
+        //the main loop is spinning every 300ms, but the LED needs to go faster!
+    } //while
+
+}
+
+int db = 0;   //defines a variable for debounce
+
+void toggle() {
+    db = debounce.read_ms();   //defines db as the value of the debounce timer
+    if (db>5)
+    LEDOut1 = !LEDOut1;   //stops the LED from switching if the debounce timer is too low
+    pc.printf("%i\r\n",db);   //prints db
+    debounce.reset();   //reset the debounce timer each time the button is pressed with a gap of more than 5ms 
+}
\ No newline at end of file
--- a/main.cpp	Tue Oct 11 13:38:14 2022 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,37 +0,0 @@
-//Base code for modification for Assignment 3.2
-//Blinks LED2 every 200ms using a single Ticker object.
-//Created: S. Licht, 10/04/2020
-#include "mbed.h"
-Ticker tickerLED2;  //creat ticker object
-InterruptIn switch1(p5);
-DigitalOut LEDOut2(LED2);
-DigitalOut LEDOut4(LED4);
-
-void changeLED2()  //the function that will be called by the ticker object.
-{
-    LEDOut2 = !LEDOut2;
-}
-
-void changeLED4()  //the function that will be called by the ticker object.
-{
-    LEDOut4 = !LEDOut4;
-}
-
-
-int main()
-{
-    tickerLED2.attach(&changeLED2,0.5); //the address of the function to call
-    //and the interval in seconds between
-    //calls to that function
-
-    while(1) {
-        wait(0.1);
-        wait(0.1);
-        wait(0.1);
-        //the main loop is spinning every 300ms, but the LED needs to go faster!
-
-    } //while
-
-    switch1.rise(&changeLED4);
-
-}