#include <FastLED.h>
#include <Button2.h>
#define BUTTON_PIN 14
#define BUTTON2_PIN 12
#define LED_PIN 5
#define NUM_LEDS 16
#define BRIGHTNESS 255
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
CRGBPalette16 currentPalette;
int color_index = 0;
int palette_index = 0;
bool direction = false;
Button2 button, button2;
bool leds_off = false;
DEFINE_GRADIENT_PALETTE(green_to_red) {
0, 173, 255, 47, // green
127, 255, 218, 0, // yellow
255, 231, 0, 0
}; // red
CRGBPalette16 green_red_palette = green_to_red;
void FillLEDsFromPaletteColors( CRGBPalette16 palette, uint8_t colorIndex)
{
uint8_t brightness = 255;
for ( int i = 0; i < NUM_LEDS; ++i) {
leds[i] = ColorFromPalette( palette, colorIndex, brightness, LINEARBLEND);
colorIndex += 256 / NUM_LEDS;
}
}
void keyLongClick(Button2& btn) {
unsigned int time = btn.wasPressedFor();
if (time > 1500) {
for (int i = 0; i < NUM_LEDS; i++)
leds[i] = CRGB::Black;
FastLED.show();
}
Serial.println("OFF");
leds_off = true;
}
void keyPressed(Button2& btn) {
leds_off = false;
palette_index ++;
if (palette_index > 8)
palette_index = 0;
switch (palette_index)
{
case 0:
currentPalette = RainbowColors_p;
color_index = 0;
Serial.println("RainbowColors");
break;
case 1:
currentPalette = RainbowStripeColors_p;
color_index = 0;
Serial.println("RainbowStripeColors");
break;
case 2:
currentPalette = CloudColors_p;
color_index = 0;
Serial.println("CloudColors");
break;
case 3:
currentPalette = PartyColors_p;
color_index = 0;
Serial.println("PartyColors");
break;
case 4:
currentPalette = LavaColors_p;
color_index = 0;
Serial.println("LavaColors");
break;
case 5:
currentPalette = OceanColors_p;
color_index = 0;
Serial.println("OceanColors");
break;
case 6:
currentPalette = ForestColors_p;
color_index = 0;
Serial.println("ForestColors");
break;
case 7:
currentPalette = HeatColors_p;
color_index = 0;
Serial.println("HeatColors");
break;
case 8:
currentPalette = green_to_red;
color_index = 0;
Serial.println("green_to_red");
break;
}
}
void key2Pressed(Button2& btn) {
if (direction)
direction = false;
else
direction = true;
}
void setup() {
Serial.begin(115200);
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness( BRIGHTNESS );
button.begin(BUTTON_PIN);
button2.begin(BUTTON2_PIN);
button.setLongClickHandler(keyLongClick);
button.setPressedHandler(keyPressed);
button2.setPressedHandler(key2Pressed);
currentPalette = RainbowColors_p;
Serial.println("RainbowColors");
}
void loop() {
button.loop();
button2.loop();
if (!leds_off)
{
FillLEDsFromPaletteColors(currentPalette, color_index);
color_index += (direction ? 1 : -1) * 3;
FastLED.show();
}
delay(10); // this speeds up the simulation
}