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

Committer:
dreschpe
Date:
Wed Dec 05 08:08:41 2012 +0000
Revision:
3:139f52e5e15c
Parent:
2:52c13333401e
The lcd lib don't claim the printf now, so we have to call LCD.printf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dreschpe 1:670665e77763 1 /**
dreschpe 1:670665e77763 2 * Demo for the RGB Led on the mbed Lab Board
dreschpe 1:670665e77763 3 * Pot 2 changes the color
dreschpe 1:670665e77763 4 * Pot 1 changes the value
dreschpe 1:670665e77763 5 * the saturation is set to maximum
dreschpe 1:670665e77763 6 *
dreschpe 1:670665e77763 7 * Copyright (c) 2012 Peter Drescher - DC2PD
dreschpe 2:52c13333401e 8 * This Demo run only on LPC1768 !
dreschpe 2:52c13333401e 9 * The LPC11U24 has different PWM pins !
dreschpe 1:670665e77763 10 */
dreschpe 1:670665e77763 11
dreschpe 1:670665e77763 12
chris 0:f86c572491c3 13 #include "mbed.h"
dreschpe 3:139f52e5e15c 14 #include "C12832_lcd.h"
dreschpe 1:670665e77763 15 #include "Small_7.h"
dreschpe 1:670665e77763 16 #include "Arial_9.h"
dreschpe 3:139f52e5e15c 17 //#include "stdio.h"
dreschpe 3:139f52e5e15c 18
dreschpe 3:139f52e5e15c 19
dreschpe 3:139f52e5e15c 20 // LCD object
dreschpe 3:139f52e5e15c 21 C12832_LCD lcd;
chris 0:f86c572491c3 22
dreschpe 1:670665e77763 23 /* the led's are connected to vcc, so a PwmOut of 100% will shut off the led and 0% will let it shine ! */
chris 0:f86c572491c3 24 PwmOut r (p23);
chris 0:f86c572491c3 25 PwmOut g (p24);
chris 0:f86c572491c3 26 PwmOut b (p25);
chris 0:f86c572491c3 27
dreschpe 1:670665e77763 28 AnalogIn Pot1(p19);
dreschpe 1:670665e77763 29 AnalogIn Pot2(p20);
dreschpe 1:670665e77763 30
dreschpe 1:670665e77763 31 // function to convert hue , saturation and value to RGB
dreschpe 1:670665e77763 32 // see http://en.wikipedia.org/wiki/HSL_and_HSV
dreschpe 1:670665e77763 33 void hsv2rgb(float H,float S, float V)
dreschpe 1:670665e77763 34 {
dreschpe 1:670665e77763 35 float f,h,p,q,t;
dreschpe 1:670665e77763 36 int i;
dreschpe 1:670665e77763 37 if( S == 0.0) {
dreschpe 1:670665e77763 38 r = 1.0 - V; // invert pwm !
dreschpe 1:670665e77763 39 g = 1.0 - V;
dreschpe 1:670665e77763 40 b = 1.0 - V;
dreschpe 1:670665e77763 41 return;
dreschpe 1:670665e77763 42 }
dreschpe 1:670665e77763 43 if(H > 360.0) H = 0.0; // check values
dreschpe 1:670665e77763 44 if(S > 1.0) S = 1.0;
dreschpe 1:670665e77763 45 if(S < 0.0) S = 0.0;
dreschpe 1:670665e77763 46 if(V > 1.0) V = 1.0;
dreschpe 1:670665e77763 47 if(V < 0.0) V = 0.0;
dreschpe 1:670665e77763 48 h = H / 60.0;
dreschpe 1:670665e77763 49 i = (int) h;
dreschpe 1:670665e77763 50 f = h - i;
dreschpe 1:670665e77763 51 p = V * (1.0 - S);
dreschpe 1:670665e77763 52 q = V * (1.0 - (S * f));
dreschpe 1:670665e77763 53 t = V * (1.0 - (S * (1.0 - f)));
dreschpe 1:670665e77763 54
dreschpe 1:670665e77763 55 switch(i) {
dreschpe 1:670665e77763 56 case 0:
dreschpe 1:670665e77763 57 r = 1.0 - V; // invert pwm !
dreschpe 1:670665e77763 58 g = 1.0 - t;
dreschpe 1:670665e77763 59 b = 1.0 - p;
dreschpe 1:670665e77763 60 break;
dreschpe 1:670665e77763 61 case 1:
dreschpe 1:670665e77763 62 r = 1.0 - q;
dreschpe 1:670665e77763 63 g = 1.0 - V;
dreschpe 1:670665e77763 64 b = 1.0 - p;
dreschpe 1:670665e77763 65 break;
dreschpe 1:670665e77763 66 case 2:
dreschpe 1:670665e77763 67 r = 1.0 - p;
dreschpe 1:670665e77763 68 g = 1.0 - V;
dreschpe 1:670665e77763 69 b = 1.0 - t;
dreschpe 1:670665e77763 70 break;
dreschpe 1:670665e77763 71 case 3:
dreschpe 1:670665e77763 72 r = 1.0 - p;
dreschpe 1:670665e77763 73 g = 1.0 - q;
dreschpe 1:670665e77763 74 b = 1.0 - V;
dreschpe 1:670665e77763 75 break;
dreschpe 1:670665e77763 76 case 4:
dreschpe 1:670665e77763 77 r = 1.0 - t;
dreschpe 1:670665e77763 78 g = 1.0 - p;
dreschpe 1:670665e77763 79 b = 1.0 - V;
dreschpe 1:670665e77763 80 break;
dreschpe 1:670665e77763 81 case 5:
dreschpe 1:670665e77763 82 default:
dreschpe 1:670665e77763 83 r = 1.0 - V;
dreschpe 1:670665e77763 84 g = 1.0 - p;
dreschpe 1:670665e77763 85 b = 1.0 - q;
dreschpe 1:670665e77763 86 break;
dreschpe 1:670665e77763 87 }
dreschpe 1:670665e77763 88 }
dreschpe 1:670665e77763 89
dreschpe 1:670665e77763 90
chris 0:f86c572491c3 91 int main()
chris 0:f86c572491c3 92 {
dreschpe 1:670665e77763 93 float h; // hue
dreschpe 1:670665e77763 94 float s,v; // saturation and value;
dreschpe 2:52c13333401e 95 unsigned short temp;
dreschpe 1:670665e77763 96
dreschpe 3:139f52e5e15c 97 lcd.cls();
dreschpe 3:139f52e5e15c 98 lcd.locate(10,0);
dreschpe 3:139f52e5e15c 99 lcd.set_font((unsigned char*) Arial_9);
dreschpe 3:139f52e5e15c 100 lcd.printf("RGB Led Demo");
dreschpe 3:139f52e5e15c 101 lcd.set_font((unsigned char*) Small_7);
dreschpe 3:139f52e5e15c 102
dreschpe 3:139f52e5e15c 103 r.period(0.001); // set pwm period
dreschpe 1:670665e77763 104 s = 1.0;
dreschpe 1:670665e77763 105 for(;;){
dreschpe 1:670665e77763 106 // get Poti 1 for color
dreschpe 2:52c13333401e 107 temp = Pot1.read_u16();
dreschpe 2:52c13333401e 108 temp = temp >> 6; // truncate to 10 Bit
dreschpe 2:52c13333401e 109 h = temp * 0.3515625; // scale to 0 - 360;
dreschpe 3:139f52e5e15c 110 lcd.locate(0,13);
dreschpe 3:139f52e5e15c 111 lcd.printf("Colour = %3.2f degree",h);
dreschpe 1:670665e77763 112 // get Poti 2 fo value
dreschpe 2:52c13333401e 113 temp = Pot2.read_u16();
dreschpe 2:52c13333401e 114 temp = temp >> 6;
dreschpe 2:52c13333401e 115 v = temp * 0.0009765624; // scale to 0 - 1;
dreschpe 3:139f52e5e15c 116 lcd.locate(0,23);
dreschpe 3:139f52e5e15c 117 lcd.printf("Val = %01.3f ",v);
dreschpe 1:670665e77763 118 hsv2rgb(h,s,v);
dreschpe 1:670665e77763 119 wait_ms(500);
chris 0:f86c572491c3 120 }
chris 0:f86c572491c3 121 }