Example PWM control of RGB LEDs

Dependencies:   mbed

Fork of frdm_rgbled by Freescale

Using three PWM channels to mix the onboard RGB (Red, Green, Blue) LED to make a variety of colors.

NOTE: This example code does not work with FRDM-K64F.

Revision:
1:eabc6f5b51d6
Parent:
0:cf8a48b1fb23
Child:
2:03e5c29343d1
--- a/main.cpp	Thu Oct 11 14:08:32 2012 +0000
+++ b/main.cpp	Fri Oct 12 13:49:40 2012 +0000
@@ -1,12 +1,22 @@
 #include "mbed.h"
 
-BusOut rgb(LED_RED,LED_GREEN,LED_BLUE);
+PwmOut r (LED_RED);
+PwmOut g (LED_GREEN);
+PwmOut b (LED_BLUE);
 
-int main() {
-    int i=0;   
+int main()
+{
+    r.period(0.001);
+    g.period(0.001);
+    b.period(0.001);
+
     while(1) {
-        rgb=i;
-        wait (0.25);
-        i++;            
-     }
+        for(float i = 0.0; i < 1.0 ; i += 0.001) {
+            float p = 3 * i;
+            r = 1.0 - ((p < 1.0) ? 1.0 - p : (p > 2.0) ? p - 2.0 : 0.0);
+            g = 1.0 - ((p < 1.0) ? p : (p > 2.0) ? 0.0 : 2.0 - p);
+            b = 1.0 - ((p < 1.0) ? 0.0 : (p > 2.0) ? 3.0 - p : p - 1.0);  ;  
+            wait (0.0025);
+        }
+    }
 }
\ No newline at end of file