Code to demonstrate use of the two buttons to toggle LED's

Dependencies:   mbed

Simple code to demonstrate the use of buttons to toggle LEDs on and off.

Files at this revision

API Documentation at this revision

Comitter:
djmannion
Date:
Fri Dec 12 18:02:52 2014 +0000
Parent:
0:f932320f7606
Commit message:
Interrupts now used

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r f932320f7606 -r 0133472f4825 main.cpp
--- a/main.cpp	Fri Dec 12 09:01:15 2014 +0000
+++ b/main.cpp	Fri Dec 12 18:02:52 2014 +0000
@@ -5,30 +5,40 @@
 Last Update: 12.12.2014
 */
 #include "mbed.h"
-//Define pin names and numbers ( I know these probably aren't needed but I put them in to demonstrate pin numbers)
+/* Define pin names and numbers (I know these probably aren't needed but I put them in to demonstrate pin numbering) */
 #define LED1 p18
 #define LED2 p19
 #define SW1 p16
 #define SW2 p17
+/*Function Prototypes*/
+void trigger1();//Button1 Interrupt function
+void trigger2();//Button2 Interrup Function
+
 //Instantiate IO objects
 DigitalOut led_1(LED1);
 DigitalOut led_2(LED2);
 DigitalIn  sw_1(SW1);
 DigitalIn  sw_2(SW2);
+//Instantiate input interrupts
+InterruptIn sw1Press(SW1);
+InterruptIn sw2Press(SW2);
 
 int main() {
+    //Initialise LED output
     led_1=0;
     led_2=0;
-    while(1) {
-        if(!sw_1)
-        {
-            led_1=!led_1; 
-            wait(0.25);   //Button Debounce ;)
-        }
-        if(!sw_2)
-        {
-            led_2=!led_2;
-            wait(0.25);   //Button Debounce ;)
-        }
-    }
+    //Choose falling edge interrupt and connect to apppropriate interrup function
+    sw1Press.fall(&trigger1);
+    sw2Press.fall(&trigger2);
+    while(1) {}
 }
+/*Button1 interrupt function*/
+void trigger1()
+{
+ led_1=!led_1;
+}
+/*Button2 interrupt function*/
+void trigger2()
+{
+ led_2=!led_2; 
+}
\ No newline at end of file