Button example.

Dependencies:   mbed

Revision:
4:1676578fd4cd
Parent:
3:cb287ef68787
Child:
5:0143ac4615a0
--- a/main.cpp	Wed Jan 10 14:27:38 2018 +0000
+++ b/main.cpp	Fri Mar 02 13:40:23 2018 +0000
@@ -21,35 +21,21 @@
 
         // check if button A pressed
         if ( button_A.read() == 1) {
-
             // writing a 1 turns the LED off, 0 makes it turn on (active-low)
             red_led.write(0);  // if it is, turn the red LED on
+        } else if (button_B.read() == 1) { // check if B pressed
+            green_led.write(0);  // if it is, turn the green LED on
+        } else if ( button_C.read() == 1) { // check if C pressed
+            blue_led.write(0);  // if it is, turn the blue LED on
         } else {
-            red_led.write(1);  // if it isn't, turn the red LED on
+            // if no buttons pressed, ensure all the LEDs are off 
+            red_led.write(1);
+            green_led.write(1);
+            blue_led.write(1);
         }
-
-        // check if button B pressed
-        if ( button_B.read() == 1) {
-
-            // writing a 1 turns the LED off, 0 makes it turn on (active-low)
-            green_led.write(0);  // if it is, turn the red LED on
-        } else {
-            green_led.write(1);  // if it isn't, turn the red LED on
-        }
+        
+    wait(0.1);  // small delay
 
-        // check if button C pressed
-        if ( button_C.read() == 1) {
-
-            // writing a 1 turns the LED off, 0 makes it turn on (active-low)
-            blue_led.write(0);  // if it is, turn the red LED on
-        } else {
-            blue_led.write(1);  // if it isn't, turn the red LED on
-        }
-
-
-        wait(0.1);  // small delay
-
-    }
 }
 
 void init_buttons()