Skip to content

Instantly share code, notes, and snippets.

@bjpirt
Created December 25, 2015 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjpirt/821ac6c91e4fe61480d4 to your computer and use it in GitHub Desktop.
Save bjpirt/821ac6c91e4fe61480d4 to your computer and use it in GitHub Desktop.
An Arduino sketch that takes a colour and cycles through the spectrum to transition to it and an accompanying node-red flow to hook it up to twitter
[{"id":"800baf19.7ff45","type":"serial-port","z":"2d82a309.d27d5c","serialport":"/dev/ttyUSB1","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","newline":"\\n","bin":"false","out":"char","addchar":true},{"id":"4121fbbc.bede04","type":"twitter in","z":"2d82a309.d27d5c","twitter":"","tags":"cheerlights","user":"false","name":"cheerlights","topic":"tweets","x":150,"y":162.00001525878906,"wires":[["eb35c13.f14ca4"]]},{"id":"eb35c13.f14ca4","type":"function","z":"2d82a309.d27d5c","name":"parse @Cheerlight colours","func":"msg.payload = msg.payload.toLowerCase();\n\nvar result = msg.payload.match(\n/red|green|blue|cyan|white|warmwhite|purple|magenta|yellow|orange|black|pink|oldlace/g);\n\nmsg = [];\nfor (var colour in result) {\n msg.push({payload:result[colour]+\"\\n\"});\n}\n\nconsole.log(msg);\n\nreturn [msg];\n\nif(context.global.enabled){\n return [msg];\n}else{\n return [];\n}","outputs":1,"noerr":0,"x":387.00001525878906,"y":194,"wires":[["b591b09d.4a6e5"]]},{"id":"b591b09d.4a6e5","type":"delay","z":"2d82a309.d27d5c","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"6","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":592.0000305175781,"y":277.0000305175781,"wires":[["494099a2.b6bf68","32aa4285.cd55be"]]},{"id":"494099a2.b6bf68","type":"debug","z":"2d82a309.d27d5c","name":"","active":true,"complete":false,"x":796.0001831054688,"y":330.00006103515625,"wires":[]},{"id":"32aa4285.cd55be","type":"serial out","z":"2d82a309.d27d5c","name":"out","serial":"800baf19.7ff45","x":779,"y":213,"wires":[]},{"id":"3f3ccf41.c0c33","type":"inject","z":"2d82a309.d27d5c","name":"test","topic":"","payload":"cyan red blue red green red","payloadType":"string","repeat":"","crontab":"","once":false,"x":157,"y":217,"wires":[["eb35c13.f14ca4"]]},{"id":"ab239738.54dc68","type":"inject","z":"2d82a309.d27d5c","name":"","topic":"","payload":"red","payloadType":"string","repeat":"","crontab":"","once":false,"x":568,"y":134,"wires":[["32aa4285.cd55be"]]},{"id":"e1c90dd0.1e36f","type":"inject","z":"2d82a309.d27d5c","name":"","topic":"","payload":"cyan","payloadType":"string","repeat":"","crontab":"","once":false,"x":569,"y":72,"wires":[["32aa4285.cd55be"]]}]
#include "FastLED.h"
// The number of leds in the strip
#define NUM_LEDS 50
#define DATA_PIN 11
#define CLOCK_PIN 13
#define TICK_LENGTH 25
// Define the array of leds
CRGB leds[NUM_LEDS];
CHSV led_buffer[NUM_LEDS];
char input_buffer[10];
int input_buffer_pos = 0;
long next_tick;
CHSV old_colour;
CHSV new_colour;
int diff_h;
int diff_s;
int diff_v;
boolean processing;
CHSV red(0, 255, 255);
CHSV green(85, 255, 255);
CHSV blue(170, 255, 255);
CHSV cyan(128, 255, 255);
CHSV white(0, 0, 255);
CHSV oldlace(27, 23, 252);
CHSV purple(212, 255, 128);
CHSV magenta(212, 255, 255);
CHSV yellow(43, 255, 255);
CHSV orange(28, 255, 255);
CHSV pink(248, 64, 255);
CHSV black(0, 0, 0);
int roundVal(float val){
int sign = val < 0 ? -1 : 1;
return sign * ceil(abs(val));
}
void processCmd(){
CHSV temp_colour;
if(!strcmp(input_buffer, "red")){
temp_colour = red;
}else if(!strcmp(input_buffer, "green")){
temp_colour = green;
}else if(!strcmp(input_buffer, "blue")){
temp_colour = blue;
}else if(!strcmp(input_buffer, "cyan")){
temp_colour = cyan;
}else if(!strcmp(input_buffer, "white")){
temp_colour = white;
}else if(!strcmp(input_buffer, "warmwhite")){
temp_colour = oldlace;
}else if(!strcmp(input_buffer, "purple")){
temp_colour = purple;
}else if(!strcmp(input_buffer, "magenta")){
temp_colour = magenta;
}else if(!strcmp(input_buffer, "yellow")){
temp_colour = yellow;
}else if(!strcmp(input_buffer, "orange")){
temp_colour = orange;
}else if(!strcmp(input_buffer, "pink")){
temp_colour = pink;
}else if(!strcmp(input_buffer, "oldlace")){
temp_colour = oldlace;
}else if(!strcmp(input_buffer, "black")){
temp_colour = black;
}else{
Serial.println("ERROR");
return;
}
if(processing){
Serial.println("not ready");
}else{
old_colour = new_colour;
new_colour = temp_colour;
diff_h = roundVal((new_colour.h - old_colour.h) / 50.0);
diff_s = roundVal((new_colour.s - old_colour.s) / 50.0);
diff_v = roundVal((new_colour.v - old_colour.v) / 50.0);
processing = true;
Serial.print(input_buffer);
Serial.println(" ok");
}
}
void checkSerial(){
if(Serial.available() > 0){
char incomingByte = Serial.read();
if((incomingByte == '\r' || incomingByte == '\n')){
if(input_buffer_pos > 0){
input_buffer[input_buffer_pos] = '\0';
processCmd();
input_buffer_pos = 0;
}
}else{
// Not a line to process so store for processing
input_buffer[input_buffer_pos++] = incomingByte;
}
}
}
void setup() {
FastLED.addLeds<WS2801,RGB>(leds, NUM_LEDS);
old_colour = black;
new_colour = black;
next_tick = 0;
processing = false;
Serial.begin(115200);
Serial.println("Cheerlights Ready");
for(int i = 0; i < NUM_LEDS; i++) {
led_buffer[i] = black;
}
FastLED.show();
}
void loop() {
long now = millis();
if(next_tick < now){
if(led_buffer[NUM_LEDS - 1] != new_colour || led_buffer[0] != led_buffer[NUM_LEDS - 1]){
for(int i = 0; i < NUM_LEDS; i++) {
if(i < NUM_LEDS - 1){
led_buffer[i] = led_buffer[i+1];
}else{
// gradually fade to the new colour
if(led_buffer[i].h != new_colour.h){
/*
Serial.print(led_buffer[i].h);
Serial.print(" -> ");
Serial.print(new_colour.h);
Serial.print(" (");
Serial.print(diff_h);
Serial.print(")");
Serial.print(" (");
Serial.print(abs(led_buffer[i].h - new_colour.h));
Serial.println(")");
*/
if(abs(diff_h) > abs(led_buffer[i].h - new_colour.h)){
led_buffer[i].h = new_colour.h;
}else{
led_buffer[i].h += diff_h;
}
}
if(led_buffer[i].s != new_colour.s){
if(abs(diff_s) > abs(led_buffer[i].s - new_colour.s)){
led_buffer[i].s = new_colour.s;
}else{
led_buffer[i].s += diff_s;
}
}
if(led_buffer[i].v != new_colour.v){
if(abs(diff_v) > abs(led_buffer[i].v - new_colour.v)){
led_buffer[i].v = new_colour.v;
}else{
led_buffer[i].v += diff_v;
}
}
}
}
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = led_buffer[i];
}
FastLED.show();
}else{
processing = false;
}
next_tick = now + TICK_LENGTH;
}
checkSerial();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment