#include <Adafruit_NeoPixel.h>
const int buttonPin = 19; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int strandPin = 6;
const int strandPin2 = 5;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, strandPin, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(60, strandPin2, NEO_GRB + NEO_KHZ800);
int mode = 1;
int max_mode = 7;
int lastButton = 1;
long lastMillis = 0;
int brightness = 128;
int march = 0;
uint32_t white = strip.Color(255, 255, 255);
uint32_t red = strip.Color(255, 0, 0);
uint32_t green = strip.Color(0, 255, 0);
uint32_t cyan = strip.Color(0, 255, 255);
uint32_t blue = strip.Color(0, 0, 255);
uint32_t black = strip.Color(0, 0, 0);
uint32_t orange = strip.Color(255, 127, 0);
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
strip.begin();
strip2.begin();
all(green);
strip.show();
strip2.show();
}
void loop() {
// read the state of the pushbutton value:
int button = digitalRead(buttonPin);
if ((button != lastButton) && (millis() - lastMillis > 50)) {
if (button) {
newMode();
}
lastMillis = millis();
lastButton = button;
}
switch (mode) {
default:
brightness = 256;
all(green);
break;
case 2:
brightness = 32;
all(green);
break;
case 3:
++march;
brightness = 256;
rainbow();
break;
case 4:
++march;
brightness = 3;
rainbow();
break;
case 5:
brightness = 64;
sweepForward(green);
march = (march + 1) % 40;
brightness = 256;
sweepForward(cyan);
break;
case 6:
brightness = 64;
sweepDown(green);
march = (march + 1) % 80;
brightness = 256;
sweepDown(cyan);
break;
case 7:
brightness = 0;
all(black);
break;
}
refresh();
delay(15);
}
void sweepForward(uint32_t color) {
if (march < 20) {
setPixel(19-march, color);
setPixel(60+19-march, color);
}
setPixel(20+march, color);
setPixel(60+20+march, color);
}
void sweepDown(uint32_t color) {
if (march < 20) {
setPixel(19-march, color);
setPixel(20+march, color);
}
else if (march < 40) {
setPixel(20+march, color);
}
else if (march < 60) {
setPixel(119-(march-40), color);
}
else {
setPixel(march, color);
setPixel(99-(march-60), color);
}
}
void rainbow() {
for (int i = 0; i < 40; ++i) {
int pos = (i*(256/40) + march*2) & 0xFF;
// int blue = 128 + ((pos < 128) ? pos : (255 - pos));
uint32_t color = Wheel(pos);
if (i < 20) {
setPixel(19-i, color);
setPixel(79-i, color);
}
setPixel(20+i, color);
setPixel(80+i, color);
}
}
void newMode() {
++mode;
if (mode > max_mode) {
mode = 1;
}
for (int i = 1; i <= mode; ++i) {
flash();
}
reset();
}
void reset() {
march = 0;
brightness = 64;
all(green);
refresh();
}
void flash() {
brightness = 64;
all(green);
refresh();
digitalWrite(ledPin, 1);
delay(100);
all(black);
refresh();
digitalWrite(ledPin, 0);
delay(100);
}
void all(uint32_t color) {
for (uint16_t i = 0; i < 120; ++i) {
setPixel(i, color);
}
}
void refresh() {
strip.show();
strip2.show();
}
void setPixel(int i, uint32_t color) {
int red = (color >> 16) & 0xFF;
int green = (color >> 8) & 0xFF;
int blue = color & 0xFF;
red = (red * brightness) >> 8;
green = (green * brightness) >> 8;
blue = (blue * brightness) >> 8;
color = strip.Color(red, green, blue);
if (i < 60)
strip.setPixelColor(i, color);
else
strip2.setPixelColor(i - 60, color);
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}
// 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) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}