Created
October 31, 2015 21:18
-
-
Save kminardo/538d4306b09a34997647 to your computer and use it in GitHub Desktop.
Arduino Pumpkin
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 <IRremote.h> | |
#include <IRremoteInt.h> | |
// Display Colors | |
#define RED 1 | |
#define GREEN 2 | |
#define BLUE 3 | |
#define YELLOW 4 | |
#define CYAN 5 | |
#define MAGENTA 6 | |
#define WHITE 7 | |
#define BLACK 8 | |
int redLED = 9; | |
int greenLED = 10; | |
int blueLED = 6; | |
int remoteIO = 3; | |
bool ledOn = true; | |
int currentColor[] = {255, 255, 255}; | |
int targetColor[] = {255, 0, 0}; | |
int targetColorRef = 1; | |
int strobeRate = 180; | |
int maxStrobeRate = 180; | |
int strobeIncrement = maxStrobeRate / 5; | |
unsigned long previousStrobeMillis = 0; | |
bool rainbowMode = false; | |
unsigned long rainbowPreviousMillis = 0; | |
int rainbowColorRate = 4000; | |
unsigned long previousFadeMillis = 0; | |
int fadeRate = 50; | |
unsigned long currentMillis = 0; | |
IRrecv irrecv(remoteIO); | |
decode_results results; | |
void setup() { | |
pinMode(redLED, OUTPUT); | |
pinMode(greenLED, OUTPUT); | |
pinMode(blueLED, OUTPUT); | |
irrecv.enableIRIn(); | |
Serial.begin(57600); | |
Serial.println("Hello world!"); | |
} | |
void loop() { | |
currentMillis = millis(); | |
if (irrecv.decode(&results)) { | |
irSetColor(results.value); | |
irrecv.resume(); // Receive the next value | |
} | |
if (currentMillis - previousFadeMillis >= fadeRate) { | |
changeColor(); | |
previousFadeMillis = currentMillis; | |
} | |
if (rainbowMode) { | |
if (currentMillis - rainbowPreviousMillis >= rainbowColorRate) { | |
nextColor(); | |
rainbowPreviousMillis = currentMillis; | |
} | |
} | |
if (currentMillis - previousStrobeMillis >= strobeRate) { | |
cycleStrobe(); | |
previousStrobeMillis = currentMillis; | |
} | |
} | |
void irSetColor(int hexCode) { | |
switch (hexCode) { | |
case 0xFF30CF: | |
setTargetColor(RED); | |
break; | |
case 0xFF18E7: | |
setTargetColor(GREEN); | |
break; | |
case 0xFF7A85: | |
setTargetColor(BLUE); | |
break; | |
case 0xFF10EF: | |
setTargetColor(YELLOW); | |
break; | |
case 0xFF38C7: | |
setTargetColor(CYAN); | |
break; | |
case 0xFF5AA5: | |
setTargetColor(MAGENTA); | |
break; | |
case 0xFF42BD: | |
setTargetColor(WHITE); | |
break; | |
case 0xFFA25D: | |
setTargetColor(BLACK); | |
case 0xFF906F: | |
setStrobeRate(-strobeIncrement); | |
break; | |
case 0xFFA857: | |
setStrobeRate(strobeIncrement); | |
break; | |
case 0xFF9867: | |
rainbowMode = !rainbowMode; | |
default: | |
break; | |
} | |
} | |
void setStrobeRate(int speedMod) { | |
// 10 Steps to Minimum 30ms - Down btn adds more time, up btn subtracts it. | |
// Max Speed = 30 | |
// 500 means Off | |
int newRate = strobeRate + speedMod; | |
if (newRate > maxStrobeRate) { | |
rgb(currentColor[0], currentColor[1], currentColor[2]); | |
strobeRate = maxStrobeRate; | |
} else if (newRate <= 0) { | |
strobeRate = strobeIncrement; | |
} else { | |
strobeRate = strobeRate + speedMod; | |
} | |
} | |
void nextColor() { | |
if (targetColorRef != 7) { | |
targetColorRef = targetColorRef + 1; | |
} else { | |
targetColorRef = 1; | |
} | |
setTargetColor(targetColorRef); | |
} | |
void setTargetColor(int color) { | |
switch (color) { | |
case RED: | |
targetColor[0] = 255; | |
targetColor[1] = 0; | |
targetColor[2] = 0; | |
break; | |
case GREEN: | |
targetColor[0] = 0; | |
targetColor[1] = 255; | |
targetColor[2] = 0; | |
break; | |
case BLUE: | |
targetColor[0] = 0; | |
targetColor[1] = 0; | |
targetColor[2] = 255; | |
break; | |
case YELLOW: | |
targetColor[0] = 255; | |
targetColor[1] = 255; | |
targetColor[2] = 0; | |
break; | |
case CYAN: | |
targetColor[0] = 0; | |
targetColor[1] = 255; | |
targetColor[2] = 255; | |
break; | |
case MAGENTA: | |
targetColor[0] = 255; | |
targetColor[1] = 0; | |
targetColor[2] = 255; | |
break; | |
case WHITE: | |
targetColor[0] = 255; | |
targetColor[1] = 255; | |
targetColor[2] = 255; | |
break; | |
case BLACK: | |
targetColor[0] = 0; | |
targetColor[1] = 0; | |
targetColor[2] = 0; | |
break; | |
default: | |
Serial.println("Invalid Color"); | |
break; | |
} | |
} | |
void changeColor() { | |
for (int i = 0; i < 3; i++) { | |
if (currentColor[i] > targetColor[i]) { | |
currentColor[i] = currentColor[i] - 015; | |
if (currentColor[i] < 000) { | |
currentColor[i] = 000; | |
} | |
} else if (currentColor[i] < targetColor[i]) { | |
currentColor[i] = currentColor[i] + 015; | |
if (currentColor[i] > 255) { | |
currentColor[i] = 255; | |
} | |
} | |
} | |
rgb(currentColor[0], currentColor[1], currentColor[2]); | |
} | |
void cycleStrobe() { | |
if (strobeRate < maxStrobeRate) { | |
if (ledOn) { | |
changeColor(); | |
} else { | |
digitalWrite(redLED, LOW); | |
digitalWrite(greenLED, LOW); | |
digitalWrite(blueLED, LOW); | |
} | |
ledOn = !ledOn; | |
} else { | |
ledOn = true; | |
} | |
} | |
void rgb(int r, int g, int b) { | |
if (ledOn) { | |
analogWrite(redLED, r); | |
analogWrite(greenLED, g); | |
analogWrite(blueLED, b); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment