#include <FastLED_NeoPixel.h>
#define NB 4
#define NUM_LEDS 24 // Number of NeoPixels
#define DATA_PIN 6 // Digital IO pin connected to the NeoPixels.
#define BRIGHTNESS 50 // LED brightness, 0 (min) to 255 (max)
//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};
FastLED_NeoPixel<NUM_LEDS, DATA_PIN, NEO_GRB> strip; // <- FastLED NeoPixel version
const unsigned long wipeColors[4] = {
0xff00000, // Red
0x00ff00, // Green
0x0000ff, // Blue
0xffa500, // Orange
};
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() {
static byte whichColor;
colorWipe(strip.Color(wipeColors[whichColor]), 50);
whichColor++; if (whichColor >= 4) whichColor = 0;
//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
while ( digitalRead(bp[0]) == LOW) {
bg[0] = false;
rainbow(10, 3);
}
//momentary
while ( digitalRead(bp[1]) == LOW) {
bg[1] = false;
colorWipe(strip.Color(255, 0, 0), 5); // Red
colorWipe(strip.Color( 0, 0, 255), 5); // Blue
}
//momentary
while ( digitalRead(bp[2]) == LOW) {
bg[2] = false;
theaterChase(strip.Color(255, 255, 255), 100, 3, 5); // White, half brightness
}
//momentary
while ( digitalRead(bp[3]) == LOW) {
bg[3] = false;
theaterChase(strip.Color(127, 127, 127), 100, 3, 5); // White, half brightness
}
}
else { //button released
//momentary
if (!bg[i]) {
bg[i] = true;
Serial.print( "b" + String(i) + ":");
Serial.println(0);
}
}
}
delay(30);
}
/*
* Fills a strip with a specific color, starting at 0 and continuing
* until the entire strip is filled. Takes two arguments:
*
* 1. the color to use in the fill
* 2. the amount of time to wait after writing each LED
*/
void colorWipe(uint32_t color, unsigned long wait) {
for (unsigned int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, color);
strip.show();
delay(wait);
}
}
/*
* Runs a marquee style "chase" sequence. Takes three arguments:
*
* 1. the color to use in the chase
* 2. the amount of time to wait between frames
* 3. the number of LEDs in each 'chase' group
* 3. the number of chases sequences to perform
*/
void theaterChase(uint32_t color, unsigned long wait, unsigned int groupSize, unsigned int numChases) {
for (unsigned int chase = 0; chase < numChases; chase++) {
for (unsigned int pos = 0; pos < groupSize; pos++) {
strip.clear(); // turn off all LEDs
for (unsigned int i = pos; i < strip.numPixels(); i += groupSize) {
strip.setPixelColor(i, color); // turn on the current group
}
strip.show();
delay(wait);
}
}
}
/*
* Simple rainbow animation, iterating through all 8-bit hues. LED color changes
* based on position in the strip. Takes two arguments:
*
* 1. the amount of time to wait between frames
* 2. the number of rainbows to loop through
*/
void rainbow(unsigned long wait, unsigned int numLoops) {
for (unsigned int count = 0; count < numLoops; count++) {
// iterate through all 8-bit hues, using 16-bit values for granularity
for (unsigned long firstPixelHue = 0; firstPixelHue < 65536; firstPixelHue += 256) {
for (unsigned int i = 0; i < strip.numPixels(); i++) {
unsigned long pixelHue = firstPixelHue + (i * 65536UL / strip.numPixels()); // vary LED hue based on position
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue))); // assign color, using gamma curve for a more natural look
}
strip.show();
delay(wait);
}
}
}