Example program to cycle the RGB LED on the mbed application board through all colours

Dependencies:   C12832_lcd LCD_fonts mbed

Fork of app-board-RGB by Chris Styles

Test Program to show the RGB Led on the mbed Lab Board

The color is changed by Pot 2, the value by Pot 1.

The program use a function to convert hue , saturation and value to RGB.

With this parameters you can change the color thru the rainbow. see http://en.wikipedia.org/wiki/HSL_and_HSV

Revision:
1:670665e77763
Parent:
0:f86c572491c3
Child:
2:52c13333401e
--- a/main.cpp	Mon Oct 15 12:19:12 2012 +0000
+++ b/main.cpp	Sat Oct 20 00:21:54 2012 +0000
@@ -1,19 +1,119 @@
+/**
+  * Demo for the RGB Led on the mbed Lab Board
+  * Pot 2 changes the color 
+  * Pot 1 changes the value
+  * the saturation is set to maximum
+  * 
+  * Copyright (c) 2012 Peter Drescher - DC2PD
+  */   
+
+
 #include "mbed.h"
+#include "Small_7.h"
+#include "Arial_9.h"
+#include "stdio.h"
+#include "C12832_lcd.h"
 
+/* the led's are connected to vcc, so a PwmOut of 100% will shut off the led and 0% will let it shine ! */
 PwmOut r (p23);
 PwmOut g (p24);
 PwmOut b (p25);
 
+// LCD object
+C12832_LCD LCD("LCD");
+
+AnalogIn Pot1(p19);
+AnalogIn Pot2(p20);
+
+// function to convert hue , saturation and  value to RGB
+// see http://en.wikipedia.org/wiki/HSL_and_HSV
+void hsv2rgb(float H,float S, float V)
+{
+    float f,h,p,q,t;
+    int i;
+    if( S == 0.0) {
+        r = 1.0 - V;  // invert pwm !
+        g = 1.0 - V;
+        b = 1.0 - V;
+        return;
+    }
+    if(H > 360.0) H = 0.0;   // check values
+    if(S > 1.0) S = 1.0; 
+    if(S < 0.0) S = 0.0;
+    if(V > 1.0) V = 1.0;
+    if(V < 0.0) V = 0.0;
+    h = H / 60.0;
+    i = (int) h;
+    f = h - i;
+    p = V * (1.0 - S);
+    q = V * (1.0 - (S * f));
+    t = V * (1.0 - (S * (1.0 - f)));
+
+    switch(i) {
+        case 0:
+            r = 1.0 - V;  // invert pwm !
+            g = 1.0 - t;
+            b = 1.0 - p;
+            break;
+        case 1:
+            r = 1.0 - q;
+            g = 1.0 - V;
+            b = 1.0 - p;
+            break;
+        case 2:
+            r = 1.0 - p;
+            g = 1.0 - V;
+            b = 1.0 - t;
+            break;
+        case 3:
+            r = 1.0 - p;
+            g = 1.0 - q;
+            b = 1.0 - V;
+            break;
+        case 4:
+            r = 1.0 - t;
+            g = 1.0 - p;
+            b = 1.0 - V;
+            break;
+        case 5:
+        default:
+            r = 1.0 - V;
+            g = 1.0 - p;
+            b = 1.0 - q;
+            break;
+    }
+}
+
+    
 int main()
 {
-    r.period(0.001);
-    while(1) {
-        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.01);
-        }
+    float h;       //  hue 
+    float s,v;   // saturation and  value;
+    float temp,temp2;
+    r.period(0.001);  // set pwm period
+    
+    LCD.claim(stdout);      // send stdout to the LCD display
+    LCD.cls();
+    LCD.locate(10,0);
+    LCD.set_font((unsigned char*) Arial_9);
+    printf("RGB Led Demo");
+    LCD.set_font((unsigned char*) Small_7);
+    s = 1.0;
+    for(;;){
+        // get Poti 1 for color
+        temp = Pot1.read();
+        temp2 = Pot1.read();
+        h = (temp + temp2) * 180;
+        LCD.locate(0,13);
+        printf("Colour = %3.2f ",h);
+        // get Poti 2 fo value
+        temp = Pot2.read();
+        temp2 = Pot2.read();
+        v = (temp + temp2) / 2;
+        LCD.locate(0,23);
+        printf("Val = %01.3f ",v);
+        LCD.copy_to_lcd();
+        hsv2rgb(h,s,v);
+        wait_ms(500);
     }
 }
\ No newline at end of file