RGB LED example using BusOut

Dependencies:   mbed

Fork of 1620_App_Board_RGB_GPIO by Craig Evans

Revision:
2:12e0dd6bced5
Parent:
1:11303019663d
--- a/main.cpp	Fri Feb 17 11:41:13 2017 +0000
+++ b/main.cpp	Fri Feb 17 11:57:04 2017 +0000
@@ -8,33 +8,25 @@
 
 #include "mbed.h"
 
-DigitalOut red_led(p24);
-DigitalOut green_led(p23);
-DigitalOut blue_led(p22);
+//              R , G , B
+BusOut rgb_led(p24,p23,p22);
+//             LSB     MSB
 
 void init_leds();
 
 int main()
 {
-
+    // turn off LEDs
     init_leds();
 
     while(1) {
 
-        // writing a 1 turns the LED off, 0 makes it turn on
-        
-        blue_led.write(1);  // blue off
-        red_led.write(0);   // red on
-        wait(0.5);
-        
-        red_led.write(1);   // red off
-        green_led.write(0); // green on
-        wait(0.5);
-        
-        green_led.write(1); // green off
-        blue_led.write(0);  // blue on
-        wait(0.5);
-
+        // loop through 3-bit values and set RGB colour
+        for(int val = 0; val < 8 ; val++) {
+            rgb_led.write(val);
+            // rgb_led = val; // syntax equivalent
+            wait(0.2);    
+        }
 
     }
 }
@@ -42,13 +34,12 @@
 void init_leds()
 {
     // LEDs are common anode (active-low) so writing a 1 will turn them off
-    red_led.write(1);
-    green_led.write(1);
-    blue_led.write(1);
-
+    // We have a 3-bit bus for the RGB LED, so writing 7 (0b111) will turn
+    // all 3 off 
+    rgb_led.write(7);
+    
     // this syntax is equivalent
-    //red_led = 1;
-    //green_led = 1;
-    //blue_led = 1;
+    // rgb = 7;
+    // rgb = 0b111;  // can also use binary literals since C++ compiler upgrade 
 }