Skip to content

Instantly share code, notes, and snippets.

@kgleeson
Last active January 3, 2016 23:29
Show Gist options
  • Save kgleeson/8535830 to your computer and use it in GitHub Desktop.
Save kgleeson/8535830 to your computer and use it in GitHub Desktop.
float temp;
int tempPin = 0;
int ledAnalogOne[] = {3, 5, 6};
const byte RED[] = {255,0,0}; //0
const byte GREEN[] = {0,255,0}; //1
const byte BLUE[] = {0,0,255}; //2
const byte CYAN[] = {0,255,255}; //3
const byte WHITE[] = {255,255,255}; //4
const byte WARMWHITE[] = {253,245,230}; //5
const byte MAGENTA[] = {255,0,255}; //6
const byte YELLOW[] = {255,255,0}; //7
const byte ORANGE[] = {255,165,0}; //8
const byte PINK[] = {255,192,203}; //9
const byte PURPLE[] = {128,0,128}; //10
const byte OLDLACE[] = {255,192,203}; //11
const byte BLACK[] = {0,0,0}; //12
void setup()
{
for(int i = 0; i < 3; i++)
{
pinMode(ledAnalogOne[i], OUTPUT);
}
setColor(ledAnalogOne, BLACK);
Serial.begin(9600);
}
void loop()
{
if (Serial.available())
{
char ch = Serial.read();
Serial.print("Received: ");
int colourCode = ch - 0;
Serial.println(ch);
switchColour(colourCode);
}
}
void switchColour(int colour)
{
const byte* output = 0;
switch (colour)
{
case 48:
output = RED;
Serial.println("Setting to Red");
break;
case 49:a
output = GREEN;
Serial.println("Setting to Green");
break;
case 50:
output = BLUE;
Serial.println("Setting to Blue");
break;
case 51:
output = CYAN;
Serial.println("Setting to Cyan");
break;
case 52:
output = WHITE;
Serial.println("Setting to White");
break;
case 53:
output = WARMWHITE;
Serial.println("Setting to Warm White");
break;
case 54:
output = MAGENTA;
Serial.println("Setting to Magenta");
break;
case 55:
output = YELLOW;
Serial.println("Setting to Yellow");
break;
case 56:
output = ORANGE;
Serial.println("Setting to Orange");
break;
case 57:
output = PINK;
Serial.println("Setting to Pink");
break;
case 97:
output = PURPLE;
Serial.println("Setting to Purple");
break;
case 98:
output = OLDLACE;
Serial.println("Setting to Old Lace");
break;
default:
output = BLACK;
Serial.println("Setting to Black");
break;
}
setColor(ledAnalogOne, output);
}
void setColor(int* led, byte* color)
{
for(int i = 0; i < 3; i++)
{
analogWrite(led[i], 255 - color[i]);
}
}
void setColor(int* led, const byte* color)
{
byte tempByte[] = {color[0], color[1], color[2]};
setColor(led, tempByte);
}
void fadeToColor(int* led, byte* startColor, byte* endColor, int fadeSpeed)
{
int changeRed = endColor[0] - startColor[0];
int changeGreen = endColor[1] - startColor[1];
int changeBlue = endColor[2] - startColor[2];
int steps = max(abs(changeRed),max(abs(changeGreen), abs(changeBlue)));
for(int i = 0 ; i < steps; i++)
{
byte newRed = startColor[0] + (i * changeRed / steps);
byte newGreen = startColor[1] + (i * changeGreen / steps);
byte newBlue = startColor[2] + (i * changeBlue / steps);
byte newColor[] = {newRed, newGreen, newBlue};
setColor(led, newColor);
delay(fadeSpeed);
}
setColor(led, endColor);
}
void fadeToColor(int* led, const byte* startColor, const byte* endColor, int fadeSpeed)
{
byte tempByte1[] = {startColor[0], startColor[1], startColor[2]};
byte tempByte2[] = {endColor[0], endColor[1], endColor[2]};
fadeToColor(led, tempByte1, tempByte2, fadeSpeed);
}
import serial
import urllib
import time
port = '/dev/ttyUSB0'
# port = '/dev/tty.usbserial-A800eo5N'
baud = 9600
ser = serial.Serial(port, baud)
def read_current_colour():
cheerlights_url = 'http://api.thingspeak.com/channels/1417/field/1/last.txt'
return urllib.urlopen(cheerlights_url).read()
def get_colour_code(colour):
colour_dict = {"red": '0',
"green": '1',
"blue": '2',
"cyan": '3',
"white": '4',
"warmwhite": '5',
"magenta": '6',
"yellow": '7',
"orange": '8',
"pink": '9',
"purple": 'a',
"oldlace": 'b',
"black": 'c'}
return colour_dict.get(colour)
def main():
old_value = 'None'
current_value = ''
while True:
current_value = read_current_colour()
if current_value != old_value:
colour = get_colour_code(current_value)
ser.write(colour)
old_value = current_value
time.sleep(60)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment