Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dmiddlecamp
Last active January 3, 2017 19:59
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 dmiddlecamp/87f3a6ed3abac451f7600890884979ee to your computer and use it in GitHub Desktop.
Save dmiddlecamp/87f3a6ed3abac451f7600890884979ee to your computer and use it in GitHub Desktop.
#define RED_PIN D0
#define GREEN_PIN D1
#define BLUE_PIN D2
#define COMMON_CATHODE 1
bool rainbow_mode = false;
unsigned int counter = 0;
void setup() {
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
Particle.function("setColor", setColor);
Particle.function("rainbow", setRainbow);
}
void loop() {
if (rainbow_mode) {
Wheel(counter++);
delay(10);
}
}
int setColor(String command) {
int red, green, blue;
// lets do percentages, from 0-100% (off->on)
sscanf(command.c_str(), "%d,%d,%d", &red, &green, &blue);
Particle.publish("colors", String::format("%d-%d-%d", red, green, blue));
#ifdef COMMON_CATHODE
// with our circuit and this led, 255 is fully off, and 0 is fully on
red = map(red, 0, 100, 255, 0);
green = map(green, 0, 100, 255, 0);
blue = map(blue, 0, 100, 255, 0);
#else
red = map(red, 0, 100, 0, 255);
green = map(green, 0, 100, 0, 255);
blue = map(blue, 0, 100, 0, 255);
#endif
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
analogWrite(BLUE_PIN, blue);
return red + green + blue;
}
int setRainbow(String command) {
rainbow_mode = command.equals("on");
return 0;
}
void Wheel(uint8_t WheelPos) {
if (WheelPos < 85) {
setWheelColor(WheelPos * 3, 255 - WheelPos * 3, 0);
}
else if (WheelPos < 170) {
WheelPos -= 85;
setWheelColor(255 - WheelPos * 3, 0, WheelPos * 3 );
}
else {
WheelPos -= 170;
setWheelColor(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
void setWheelColor(uint8_t red, uint8_t green, uint8_t blue) {
#ifdef COMMON_CATHODE
red = map(red, 0, 255, 255, 0);
green = map(green, 0, 255, 255, 0);
blue = map(blue, 0, 255, 255, 0);
#endif
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
analogWrite(BLUE_PIN, blue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment