Skip to content

Instantly share code, notes, and snippets.

@kgleeson
Last active January 3, 2016 09:49
Show Gist options
  • Select an option

  • Save kgleeson/8445401 to your computer and use it in GitHub Desktop.

Select an option

Save kgleeson/8445401 to your computer and use it in GitHub Desktop.
Temp sensor + RGB LED
float temp;
int tempPin = 0;
int ledAnalogOne[] = {3, 5, 6};
const byte RED[] = {255, 0, 0};
const byte ORANGE[] = {83, 4, 0};
const byte YELLOW[] = {255, 255, 0};
const byte GREEN[] = {0, 255, 0};
const byte BLUE[] = {0, 0, 255};
const byte INDIGO[] = {4, 0, 19};
const byte VIOLET[] = {23, 0, 22};
const byte CYAN[] = {0, 255, 255};
const byte MAGENTA[] = {255, 0, 255};
const byte WHITE[] = {255, 255, 255};
const byte BLACK[] = {0, 0, 0};
const byte PINK[] = {158, 4, 79};
void setup(){
for(int i = 0; i < 3; i++){
pinMode(ledAnalogOne[i], OUTPUT);
}
setColor(ledAnalogOne, BLACK);
Serial.begin(9600);
}
void loop(){
temp = analogRead(tempPin);
if (temp < 48) setColor(ledAnalogOne, BLUE);
else if ((temp > 48)&&(temp < 53)) setColor(ledAnalogOne, GREEN);
else if (temp > 52) setColor(ledAnalogOne, RED);
Serial.print("<temp>");
Serial.print(temp * 0.48828125);
Serial.print("</temp>");
Serial.print("<raw>");
Serial.print(temp);
Serial.print("</raw>");
Serial.println();
delay(1000);
}
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment