Skip to content

Instantly share code, notes, and snippets.

@DanH42
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DanH42/a7da41aa4a1a7d32f420 to your computer and use it in GitHub Desktop.
Save DanH42/a7da41aa4a1a7d32f420 to your computer and use it in GitHub Desktop.
Read color codes (0-255, ['#', r, g, b, '\n']) from incoming UDP packets and write them out to LEDs, and adjust brightness with ['%', n, '\n']
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 0, 20);
unsigned int localPort = 8888;
EthernetUDP Udp;
unsigned char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
const byte curve[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
0x02,0x02,0x03,0x03,0x03,0x03,0x03,0x03,0x04,0x04,0x04,0x04,0x04,0x05,0x05,0x05,
0x05,0x06,0x06,0x06,0x07,0x07,0x07,0x08,0x08,0x08,0x09,0x09,0x0A,0x0A,0x0B,0x0B,
0x0C,0x0C,0x0D,0x0D,0x0E,0x0F,0x0F,0x10,0x11,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1F,0x20,0x21,0x23,0x24,0x26,0x27,0x29,0x2B,0x2C,
0x2E,0x30,0x32,0x34,0x36,0x38,0x3A,0x3C,0x3E,0x40,0x43,0x45,0x47,0x4A,0x4C,0x4F,
0x51,0x54,0x57,0x59,0x5C,0x5F,0x62,0x64,0x67,0x6A,0x6D,0x70,0x73,0x76,0x79,0x7C,
0x7F,0x82,0x85,0x88,0x8B,0x8E,0x91,0x94,0x97,0x9A,0x9C,0x9F,0xA2,0xA5,0xA7,0xAA,
0xAD,0xAF,0xB2,0xB4,0xB7,0xB9,0xBB,0xBE,0xC0,0xC2,0xC4,0xC6,0xC8,0xCA,0xCC,0xCE,
0xD0,0xD2,0xD3,0xD5,0xD7,0xD8,0xDA,0xDB,0xDD,0xDE,0xDF,0xE1,0xE2,0xE3,0xE4,0xE5,
0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xED,0xEE,0xEF,0xEF,0xF0,0xF1,0xF1,0xF2,
0xF2,0xF3,0xF3,0xF4,0xF4,0xF5,0xF5,0xF6,0xF6,0xF6,0xF7,0xF7,0xF7,0xF8,0xF8,0xF8,
0xF9,0xF9,0xF9,0xF9,0xFA,0xFA,0xFA,0xFA,0xFA,0xFB,0xFB,0xFB,0xFB,0xFB,0xFB,0xFC,
0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFD,0xFD,0xFD,0xFD,0xFD,0xFD,0xFD,0xFD,
0xFD,0xFD,0xFD,0xFD,0xFD,0xFD,0xFD,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,0xFF
};
byte r = 0;
byte g = 0;
byte b = 0;
float brightness = 1;
void setup(){
Ethernet.begin(mac, ip);
Udp.begin(localPort);
pinMode(3, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
}
void loop(){
int packetSize = Udp.parsePacket();
if(Udp.available()){
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
if(packetSize == 5 && packetBuffer[0] == '#'){
r = packetBuffer[1];
g = packetBuffer[2];
b = packetBuffer[3];
updateColor();
}else if(packetSize == 3 && packetBuffer[0] == '%'){
brightness = packetBuffer[1] / 255.0;
updateColor();
}
}
}
void updateColor(){
analogWrite(3, curve[(byte)floor(r * brightness)]);
analogWrite(6, curve[(byte)floor(g * brightness)]);
analogWrite(9, curve[(byte)floor(b * brightness)]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment