Skip to content

Instantly share code, notes, and snippets.

@creationix
Created March 31, 2015 02:02
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 creationix/7c1ca35975375ffae3a3 to your computer and use it in GitHub Desktop.
Save creationix/7c1ca35975375ffae3a3 to your computer and use it in GitHub Desktop.
#include <Adafruit_NeoPixel.h>
static Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, 3, NEO_GRB + NEO_KHZ800);
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos, byte bright) {
if (WheelPos < 85) {
WheelPos *= 3;
return strip.Color(WheelPos * bright / 256, (255 - WheelPos) * bright / 256, 0);
}
else if (WheelPos < 170) {
WheelPos -= 85;
WheelPos *= 3;
return strip.Color((255 - WheelPos) * bright / 256, 0, WheelPos * bright / 256);
}
else {
WheelPos -= 170;
WheelPos *= 3;
return strip.Color(0, WheelPos * bright / 256, (255 - WheelPos) * bright / 256);
}
}
void setup() {
pinMode(0, INPUT);
pinMode(1, INPUT);
pinMode(2, INPUT);
pinMode(3, OUTPUT);
strip.show();
}
int mode = 0;
int speed = 1;
int bright = 1;
int color = 0;
int alt = 0;
int altm = 0;
int altc = 0;
int alt1 = 0;
int alt2 = 100;
int A = 0;
int B = 0;
int C = 0;
void loop() {
int a = 0, b = 0, c = 0;
int down = digitalRead(1);
if (down != A) {
A = down;
if (down) a = 1;
}
down = digitalRead(2);
if (down != C) {
C = down;
if (down) c = 1;
}
down = digitalRead(0);
if (down != B) {
B = down;
if (down) b = 1;
}
if (a) mode = (mode + 1) % 4;
switch (mode) {
case 0: // Rainbow
color = (color + 1) % 256;
strip.setPixelColor(0, Wheel(color, bright * 80 + 15));
strip.show();
if (b) speed = (speed + 1) % 4;
if (c) bright = (bright + 1) % 4;
delay(speed == 0 ? 100 :
speed == 1 ? 40 :
speed == 2 ? 10 :
1);
break;
case 1: // Solid
if (down) color = (color + 1) % 256;
if (c) bright = (bright + 1) % 4;
strip.setPixelColor(0, Wheel(color, bright * 80 + 15));
strip.show();
delay(50);
break;
case 2: // Crazy
strip.setPixelColor(0, Wheel(random(0, 256), random(100, 256)));
strip.show();
delay(random(0, 400));
break;
case 3: // Alternate
if (c) altm = (altm + 1) % 3;
switch (altm) {
case (0):
strip.setPixelColor(0, Wheel(altc ? alt1 : alt2, 100));
strip.show();
altc = !altc;
delay(500);
break;
case (1):
if (down) alt1 = (alt1 + 1) % 256;
strip.setPixelColor(0, Wheel(alt1, 100));
strip.show();
delay(50);
break;
case (2):
if (down) alt2 = (alt2 + 1) % 256;
strip.setPixelColor(0, Wheel(alt2, 100));
strip.show();
delay(50);
break;
}
break;
}
if (a || b || c) delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment