Let the on board RGB led run thru the color circle

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 
00004 /* the led's are connected to vcc, so a PwmOut of 100% will shut off the led and 0% will let it shine ! */
00005 PwmOut r (LED_RED);
00006 PwmOut g (LED_GREEN);
00007 PwmOut b (LED_BLUE);
00008 
00009 // function to convert hue , saturation and  value to RGB
00010 // see http://en.wikipedia.org/wiki/HSL_and_HSV
00011 void hsv2rgb(float H,float S, float V)
00012 {
00013     float f,h,p,q,t;
00014     int i;
00015     if( S == 0.0) {
00016         r = 1.0 - V;  // invert pwm !
00017         g = 1.0 - V;
00018         b = 1.0 - V;
00019         return;
00020     }
00021     if(H > 360.0) H = 0.0;   // check values
00022     if(S > 1.0) S = 1.0; 
00023     if(S < 0.0) S = 0.0;
00024     if(V > 1.0) V = 1.0;
00025     if(V < 0.0) V = 0.0;
00026     h = H / 60.0;
00027     i = (int) h;
00028     f = h - i;
00029     p = V * (1.0 - S);
00030     q = V * (1.0 - (S * f));
00031     t = V * (1.0 - (S * (1.0 - f)));
00032 
00033     switch(i) {
00034         case 0:
00035             r = 1.0 - V;  // invert pwm !
00036             g = 1.0 - t;
00037             b = 1.0 - p;
00038             break;
00039         case 1:
00040             r = 1.0 - q;
00041             g = 1.0 - V;
00042             b = 1.0 - p;
00043             break;
00044         case 2:
00045             r = 1.0 - p;
00046             g = 1.0 - V;
00047             b = 1.0 - t;
00048             break;
00049         case 3:
00050             r = 1.0 - p;
00051             g = 1.0 - q;
00052             b = 1.0 - V;
00053             break;
00054         case 4:
00055             r = 1.0 - t;
00056             g = 1.0 - p;
00057             b = 1.0 - V;
00058             break;
00059         case 5:
00060         default:
00061             r = 1.0 - V;
00062             g = 1.0 - p;
00063             b = 1.0 - q;
00064             break;
00065     }
00066 }
00067 
00068 
00069 
00070 int main() {
00071     float h;       //  hue 
00072     float s,v;   // saturation and  value;
00073     r.period(0.001);  // set pwm period
00074     s = 1.0;
00075     v = 1.0;
00076     h = 0.0;    
00077     for(;;){   // run thru colors 
00078         hsv2rgb(h,s,v);
00079         wait_ms(50);
00080         if(h<360) h++;
00081         else h = 0;
00082     }
00083 }