#include <Adafruit_NeoPixel.h>
#define NB 4
#define PIXEL_COUNT 24 // Number of NeoPixels
#define PIXEL_PIN 6 // Digital IO pin connected to the NeoPixels.
//BUTTONS
////Momentary button vars
int bp[NB] = {7, 8, 9, 10}; //arduino input pin numbers
boolean bg[NB] = {false, false, false, false};
//Toggle button vars
int btv[NB] = {0, 0, 0, 0};
int btamt[NB] = {2, 2, 2, 2};;
boolean btg[NB] = {true, true, true, true};
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(9600);
for (int i = 0; i < NB; i++) pinMode(bp[i], INPUT_PULLUP);
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(200); // Set BRIGHTNESS (max = 255)
}
void loop() {
//BUTTONS
for (int i = 0; i < NB; i++) {
if ( digitalRead(bp[i]) == LOW) { //button pushed
//momentary
if (bg[i]) {
bg[i] = false;
Serial.print("b");
Serial.print(String(i));
Serial.print(":");
// Serial.print( "b" + String(i) + ":");
Serial.println(1);
}
//momentary
if ( digitalRead(bp[0]) == LOW) {
bg[0] = false;
rainbow(25);
}
//momentary
if ( digitalRead(bp[1]) == LOW) {
bg[1] = false;
colorWipe(strip.Color(255, 0, 0), 50); // Red
colorWipe(strip.Color( 0, 0, 255), 50); // Blue
}
//momentary
if ( digitalRead(bp[2]) == LOW) {
bg[2] = false;
colorWipe(strip.Color(255, 0, 0), 50); // Red
colorWipe(strip.Color( 0, 0, 255), 50); // Blue
}
//momentary
if ( digitalRead(bp[3]) == LOW) {
bg[3] = false;
theaterChase(strip.Color(127, 127, 127), 50); // White, half brightness
}
}
else { //button released
//momentary
if (!bg[i]) {
bg[i] = true;
Serial.print( "b" + String(i) + ":");
Serial.println(0);
colorWipe(strip.Color(255, 0, 0), 50); // Red
colorWipe(strip.Color( 0, 255, 0), 50); // Green
colorWipe(strip.Color( 0, 0, 255), 50); // Blue
colorWipe(strip.Color(255, 165, 0), 50); // Orange
}
}
}
delay(30);
}
// Some functions of our own for creating animated effects -----------------
// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}
// Theater-marquee-style chasing lights. Pass in a color (32-bit value,
// a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
// between frames.
void theaterChase(uint32_t color, int wait) {
for(int a=0; a<10; a++) { // Repeat 10 times...
for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
strip.clear(); // Set all pixels in RAM to 0 (off)
// 'c' counts up from 'b' to end of strip in steps of 3...
for(int c=b; c<strip.numPixels(); c += 3) {
strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
}
strip.show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
}
}
// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
void rainbow(int wait) {
// Hue of first pixel runs 5 complete loops through the color wheel.
// Color wheel has a range of 65536 but it's OK if we roll over, so
// just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
// means we'll make 5*65536/256 = 1280 passes through this loop:
for(long firstPixelHue = 0; firstPixelHue < 1*65536; firstPixelHue += 256) {
// strip.rainbow() can take a single argument (first pixel hue) or
// optionally a few extras: number of rainbow repetitions (default 1),
// saturation and value (brightness) (both 0-255, similar to the
// ColorHSV() function, default 255), and a true/false flag for whether
// to apply gamma correction to provide 'truer' colors (default true).
strip.rainbow(firstPixelHue);
// Above line is equivalent to:
// strip.rainbow(firstPixelHue, 1, 255, 255, true);
strip.show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
}
// Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
void theaterChaseRainbow(int wait) {
int firstPixelHue = 0; // First pixel starts at red (hue 0)
for(int a=0; a<30; a++) { // Repeat 30 times...
for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
strip.clear(); // Set all pixels in RAM to 0 (off)
// 'c' counts up from 'b' to end of strip in increments of 3...
for(int c=b; c<strip.numPixels(); c += 3) {
// hue of pixel 'c' is offset by an amount to make one full
// revolution of the color wheel (range 65536) along the length
// of the strip (strip.numPixels() steps):
int hue = firstPixelHue + c * 65536L / strip.numPixels();
uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
}
strip.show(); // Update strip with new contents
delay(wait); // Pause for a moment
firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
}
}
}