Created
April 20, 2016 20:04
-
-
Save hdurdle/984708755108543a10a3fc0e9bc6746b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <Ethernet.h> | |
int bluPin = 6; | |
int grnPin = 5; | |
int redPin = 3; | |
int redVal = 0; | |
int bluVal = 0; | |
int grnVal = 0; | |
int prevR = redVal; | |
int prevG = bluVal; | |
int prevB = grnVal; | |
int off[3] = { 0, 0, 0 }; | |
int red[3] = { 255, 0, 0 }; | |
int green[3] = { 0, 255, 0 }; | |
int blue[3] = { 0, 0, 255 }; | |
int white[3] = { 255, 255, 255 }; | |
int wait = 1; | |
int hold = 0; | |
unsigned long timeLastCommand; | |
byte mac[] = { 0xFA, 0xDE, 0xFA, 0xDE, 0xFA, 0xDE }; | |
EthernetServer server(80); | |
String readString; | |
void setup() { | |
Serial.begin(9600); | |
Serial.println("OMGRGBFTW"); | |
Ethernet.begin(mac); | |
server.begin(); | |
pinMode(redPin, OUTPUT); | |
pinMode(grnPin, OUTPUT); | |
pinMode(bluPin, OUTPUT); | |
cycle(3, true); | |
delay(100); | |
cycle(2, false); | |
} | |
void cycle(int count, boolean rgb) { | |
for (int i = 0; i < count; i++) { | |
if (rgb) { | |
setColor(red); | |
delay(100); | |
setColor(green); | |
delay(100); | |
setColor(blue); | |
delay(100); | |
setColor(off); | |
} else { | |
setColor(white); | |
delay(100); | |
setColor(off); | |
} | |
} | |
} | |
void loop() { | |
EthernetClient client = server.available(); | |
if (client) { | |
while (client.connected()) { | |
if (client.available()) { | |
char c = client.read(); | |
if (readString.length() < 100) { | |
readString += c; | |
} | |
//if HTTP request has ended | |
if (c == '\n') { | |
client.println("HTTP/1.1 200 OK"); // send new page | |
client.println("Content-Type: text/html"); | |
client.println(); | |
client.println("<html><head><title>OMGRGBFTW</title></head>"); | |
client.println("<body>"); | |
client.println("<h1>OMGRGBFTW</h1>"); | |
client.println("<hr />"); | |
client.println("<br />"); | |
client.println("<a href=\"/?red\"\">Pulse Red</a>"); | |
client.println("<br />"); | |
client.println("<a href=\"/?green\"\">Pulse Green</a>"); | |
client.println("<br />"); | |
client.println("<a href=\"/?blue\"\">Pulse Blue</a>"); | |
client.println("<br />"); | |
client.println("<a href=\"/?off\"\">Off</a><br />"); | |
client.println("<a href=\"/?fadeOff\"\">Fade Off</a><br />"); | |
client.println("</body></html>"); | |
delay(1); | |
client.stop(); | |
if(readString.indexOf("?setHex=") >0) { | |
int loc = readString.indexOf("?setHex=")+8; | |
String hexstring = readString.substring(loc, loc+6); | |
long number = (long) strtol( &hexstring[0], NULL, 16); | |
int rHex = number >> 16; | |
int gHex = number >> 8 & 0xFF; | |
int bHex = number & 0xFF; | |
int colorToSet[3] = { rHex, gHex, bHex }; | |
setColor(colorToSet); | |
prevR = rHex; prevG = gHex; prevB = bHex; | |
redVal = rHex; grnVal = gHex; bluVal = bHex; | |
} | |
if(readString.indexOf("?fadeHex=") >0) { | |
int loc = readString.indexOf("?fadeHex=")+9; | |
String hexstring = readString.substring(loc, loc+6); | |
long number = (long) strtol( &hexstring[0], NULL, 16); | |
int rHex = number >> 16; | |
int gHex = number >> 8 & 0xFF; | |
int bHex = number & 0xFF; | |
int colorToSet[3] = { rHex, gHex, bHex }; | |
crossFade(colorToSet); | |
} | |
if(readString.indexOf("?pulseHex=") >0) { | |
int loc = readString.indexOf("?pulseHex=")+10; | |
String hexstring = readString.substring(loc, loc+6); | |
long number = (long) strtol( &hexstring[0], NULL, 16); | |
int rHex = number >> 16; | |
int gHex = number >> 8 & 0xFF; | |
int bHex = number & 0xFF; | |
int colorToSet[3] = { rHex, gHex, bHex }; | |
pulseColor(colorToSet); | |
} | |
if(readString.indexOf("?red") >0) { | |
pulseColor(red); | |
} | |
if(readString.indexOf("?green") >0) { | |
pulseColor(green); | |
} | |
if(readString.indexOf("?blue") >0) { | |
pulseColor(blue); | |
} | |
if(readString.indexOf("?fadeOff") >0) { | |
crossFade(off); | |
} | |
if(readString.indexOf("?off") >0) { | |
setColor(off); | |
prevR = 0; prevG = 0; prevB = 0; | |
redVal = 0; grnVal = 0; bluVal = 0; | |
} | |
// clearing string for next read | |
readString = ""; | |
} | |
} | |
} | |
} | |
} | |
void setColor(int color[3]) { | |
analogWrite(redPin, color[0]); | |
analogWrite(grnPin, color[1]); | |
analogWrite(bluPin, color[2]); | |
prevR = color[0]; | |
prevG = color[1]; | |
prevB = color[2]; | |
} | |
void pulseColor(int color[3]) { | |
crossFade(color); | |
crossFade(off); | |
crossFade(color); | |
crossFade(off); | |
crossFade(color); | |
} | |
int calculateStep(int prevValue, int endValue) { | |
int step = endValue - prevValue; | |
if (step) { | |
step = 1020 / step; | |
} | |
return step; | |
} | |
int calculateVal(int step, int val, int i) { | |
if ((step) && i % step == 0) { | |
if (step > 0) { | |
val += 1; | |
} else if (step < 0) { | |
val -= 1; | |
} | |
} | |
if (val > 255) { | |
val = 255; | |
} else if (val < 0) { | |
val = 0; | |
} | |
return val; | |
} | |
void crossFade(int color[3]) { | |
int R = (color[0]); | |
int G = (color[1]); | |
int B = (color[2]); | |
int stepR = calculateStep(prevR, R); | |
int stepG = calculateStep(prevG, G); | |
int stepB = calculateStep(prevB, B); | |
for (int i = 0; i <= 1020; i++) { | |
redVal = calculateVal(stepR, redVal, i); | |
grnVal = calculateVal(stepG, grnVal, i); | |
bluVal = calculateVal(stepB, bluVal, i); | |
analogWrite(redPin, redVal); | |
analogWrite(grnPin, grnVal); | |
analogWrite(bluPin, bluVal); | |
delay(wait); | |
} | |
prevR = redVal; | |
prevG = grnVal; | |
prevB = bluVal; | |
delay(hold); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment