I banged up this sketch tonight to set a LED using a full color presentation from a web page.
This requires a RGB color LED attached to D5,6,9 (configurable at top of script). It also requires the Ethernet board and appropriate libraries.
/*
===================================================
Use a web page to set a RGB LED to the selected color
===================================================
===================================================
Converted to analog write commands and removed BlinkM
Also removed simple text box page and used code from
http://www.colorpicker.com/ to pick the LED color.
Mike Audleman
Jun 3, 2012
===================================================
===================================================
Prior Header
===================================================
*/
// Small Arduino Webserver --- for BlinkM
// by Andreas Cahen ---** updated for Arduino 0021 - by PsycleSam and Co. 20101219 tested on arduino uno with updated ethernet shield
//
// Used libraries and examples:
//
// Arduino Ethershield, server.pde
// Blinkm Library, BlinkMfuncs.h +++**SPI Library, SPI.h
// ---** no longer used - TextString Library, WString.h
#include <SPI.h>
#include <Ethernet.h>
//#include <WString.h>
#include <Wire.h>
//#include <BlinkM_funcs.h>
#define maxLength 25
//#define blinkm_addr 0x09
// Sets circuit for LED.
// 0 for LED tied to ground
// 1 for LED tied to v+
int LEDInvert = 1;
byte mac[] = {
0x12, 0x12, 0x12, 0x12, 0x12, 0x12 }; //Set your own MAC
// Using DHCP
//byte ip[] = {
// 192, 168, 0, 200 };
String inString = String(maxLength);
int val;
int r = 0;
int g = 0;
int b = 0;
char colorBuff[4];
int LEDr = 5 ;//LED Red pin
int LEDg = 6 ;//LED Green pin
int LEDb = 9 ;//LED Blue pin
EthernetServer server(80);
/*
----------------------------------------------------------
Setup configuration.
----------------------------------------------------------
*/
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac);
server.begin();
pinMode(LEDr, OUTPUT);
pinMode(LEDg, OUTPUT);
pinMode(LEDb, OUTPUT);
// Init Testing of the LED
SetAll( 0 );
RampPin( LEDr );
RampPin( LEDg );
RampPin( LEDb );
delay(500);
SetAll( 255 );
delay(100);
SetAll( 0 );
}
/*
----------------------------------------------------------
Main Logic Loop
----------------------------------------------------------
*/
void loop()
{
int bufLength;
EthernetClient client = server.available();
if (client) {
boolean current_line_is_blank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (inString.length() < maxLength) {
//inString.append(c);
inString += c;
}
if (c == '\n' && current_line_is_blank) {
if (inString.indexOf("?") > -1) {
int Pos_CC = inString.indexOf("k=")+2;
int End_CC = inString.indexOf("&", Pos_CC);
if(End_CC < 0){
End_CC = inString.length() + 1;
}
String ColorHex = inString.substring(Pos_CC,End_CC);
Serial.println( "Received Hex String " + ColorHex );
r = CheckRange( HexToDec(ColorHex.substring(0,2)) );
g = CheckRange( HexToDec(ColorHex.substring(2,4)) );
b = CheckRange( HexToDec(ColorHex.substring(4,6)) );
SetLED( LEDr, r );
SetLED( LEDg, g );
SetLED( LEDb, b );
}
ShowWebPage (client);
break;
}
if (c == '\n') {
current_line_is_blank = true;
}
else if (c != '\r') {
current_line_is_blank = false;
}
}
}
delay(1);
inString = "";
client.stop();
}
}
/*
----------------------------------------------------------
Simply ramps the specific pin
----------------------------------------------------------
*/
void RampPin( int dPin ) {
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
SetLED( dPin, fadeValue );
delay(10);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
SetLED( dPin, fadeValue );
delay(10);
}
}
/*
----------------------------------------------------------
Sets all colors to set value (used for all on/all off)
----------------------------------------------------------
*/
void SetAll( int value ) {
SetLED( LEDr, value );
SetLED( LEDg, value );
SetLED( LEDb, value );
}
/*
----------------------------------------------------------
Sets the LED pin and accounts for positve or negative wired LED
----------------------------------------------------------
*/
void SetLED( int dPin, int value ) {
if (LEDInvert){
analogWrite(dPin, (255-CheckRange( value )));
}
else {
analogWrite(dPin, CheckRange( value ));
}
}
/*
----------------------------------------------------------
Validates the passed value 0 > val > 255
----------------------------------------------------------
*/
int CheckRange( int val ) {
int tmpVal = val;
if ( tmpVal > 255 ){
tmpVal=255;
};
if ( tmpVal < 0 ){
tmpVal=0;
};
return tmpVal;
}
/*
----------------------------------------------------------
Displays the web page
----------------------------------------------------------
*/
void ShowWebPage (EthernetClient theClient) {
String t = "";
theClient.println("HTTP/1.1 200 OK");
theClient.println("Content-Type: text/html");
theClient.println();
theClient.println("<html>");
t = "<script type='text/javascript'";
theClient.println("<head><base href='http://www.colorpicker.com/'><link rel='stylesheet' href='css/colorpicker.com.css' type='text/css'>");
theClient.print(t);
theClient.println(" src='js/mootools-1.2.4-core.js'></script>");
theClient.print(t);
theClient.println(" src='js/colorpicker.com.js'></script></head>");
theClient.println("<body>");
theClient.println("<div><div style='position:absolute;top:0px;left:0px;width:430px'><div id='colorDiv' style=display:none;>.</div><div id='colorBoxes'></div></div>");
theClient.println("<center><div id='colorWidgetDiv' style='position:relative;padding-left:100px;width:450px;height:340px;margin-top:70px;'></div></center></div>");
theClient.println("<form method=get onSubmit='SE();'>");
theClient.println("<input type='hidden' id='k' name='k'value='0'>");
theClient.println("<input type='submit' id='H' name='H' value='Submit'>");
theClient.println("<input type='hidden' id='cc' name='colorcode' value='0'>");
theClient.println("</form>");
theClient.println("<script language='javascript'>");
theClient.println("function SE(){var s = $('input[class^=ColorPicker_com_colorSlider_rgbCode]');var c = s.get('value');c = c.toString(c); ");
t = "').set('value',c); ";
theClient.print("$('k");
theClient.println(t);
theClient.print("$('cc");
theClient.println(t);
theClient.println("}");
theClient.println("</script>");
theClient.println("</body></html>");
}
/*
----------------------------------------------------------
Converts a single hex character to a byte
----------------------------------------------------------
*/
int hex2dec(byte c) {
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
}
}
/*
----------------------------------------------------------
Converts a 2 byte hex string to an int
----------------------------------------------------------
*/
int HexToDec(String c) {
int Digit1 = hex2dec( c.charAt(0) ) * 16;
int Digit2 = hex2dec( c.charAt(1) );
return Digit1 + Digit2;
}
Enjoy,
Wolfie