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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002   * Demo for the RGB Led on the mbed Lab Board
00003   * Pot 2 changes the color 
00004   * Pot 1 changes the value
00005   * the saturation is set to maximum
00006   * 
00007   * Copyright (c) 2012 Peter Drescher - DC2PD
00008   * This Demo run only on LPC1768 !
00009   * The LPC11U24 has different PWM pins !
00010   */   
00011 
00012 
00013 #include "mbed.h"
00014 #include "C12832_lcd.h"
00015 #include "Small_7.h"
00016 #include "Arial_9.h"
00017 //#include "stdio.h"
00018 
00019 
00020 // LCD object
00021 C12832_LCD lcd;
00022 
00023 /* the led's are connected to vcc, so a PwmOut of 100% will shut off the led and 0% will let it shine ! */
00024 PwmOut r (p23);
00025 PwmOut g (p24);
00026 PwmOut b (p25);
00027 
00028 AnalogIn Pot1(p19);
00029 AnalogIn Pot2(p20);
00030 
00031 // function to convert hue , saturation and  value to RGB
00032 // see http://en.wikipedia.org/wiki/HSL_and_HSV
00033 void hsv2rgb(float H,float S, float V)
00034 {
00035     float f,h,p,q,t;
00036     int i;
00037     if( S == 0.0) {
00038         r = 1.0 - V;  // invert pwm !
00039         g = 1.0 - V;
00040         b = 1.0 - V;
00041         return;
00042     }
00043     if(H > 360.0) H = 0.0;   // check values
00044     if(S > 1.0) S = 1.0; 
00045     if(S < 0.0) S = 0.0;
00046     if(V > 1.0) V = 1.0;
00047     if(V < 0.0) V = 0.0;
00048     h = H / 60.0;
00049     i = (int) h;
00050     f = h - i;
00051     p = V * (1.0 - S);
00052     q = V * (1.0 - (S * f));
00053     t = V * (1.0 - (S * (1.0 - f)));
00054 
00055     switch(i) {
00056         case 0:
00057             r = 1.0 - V;  // invert pwm !
00058             g = 1.0 - t;
00059             b = 1.0 - p;
00060             break;
00061         case 1:
00062             r = 1.0 - q;
00063             g = 1.0 - V;
00064             b = 1.0 - p;
00065             break;
00066         case 2:
00067             r = 1.0 - p;
00068             g = 1.0 - V;
00069             b = 1.0 - t;
00070             break;
00071         case 3:
00072             r = 1.0 - p;
00073             g = 1.0 - q;
00074             b = 1.0 - V;
00075             break;
00076         case 4:
00077             r = 1.0 - t;
00078             g = 1.0 - p;
00079             b = 1.0 - V;
00080             break;
00081         case 5:
00082         default:
00083             r = 1.0 - V;
00084             g = 1.0 - p;
00085             b = 1.0 - q;
00086             break;
00087     }
00088 }
00089 
00090     
00091 int main()
00092 {
00093     float h;       //  hue 
00094     float s,v;   // saturation and  value;
00095     unsigned short temp;
00096     
00097     lcd.cls();
00098     lcd.locate(10,0);
00099     lcd.set_font((unsigned char*) Arial_9);
00100     lcd.printf("RGB Led Demo");
00101     lcd.set_font((unsigned char*) Small_7);
00102     
00103     r.period(0.001);  // set pwm period
00104     s = 1.0;
00105     for(;;){
00106         // get Poti 1 for color
00107         temp = Pot1.read_u16();
00108         temp = temp >> 6;        // truncate to 10 Bit
00109         h = temp * 0.3515625;  // scale to 0 - 360;
00110         lcd.locate(0,13);
00111         lcd.printf("Colour = %3.2f degree",h);
00112         // get Poti 2 fo value
00113         temp = Pot2.read_u16();
00114         temp = temp >> 6;
00115         v = temp * 0.0009765624;  // scale to 0 - 1;
00116         lcd.locate(0,23);
00117         lcd.printf("Val = %01.3f ",v);
00118         hsv2rgb(h,s,v);
00119         wait_ms(500);
00120     }
00121 }