#define FASTLED_INTERRUPT_RETRY_COUNT 1
#include <FastLED.h>
// ==============================================================
// How many leds in your strip?
#define NUM_LEDS 28 // change this to match your strip length
// ==============================================================
#define DATA_PIN 3
#define ANIMATION_BUTTON 4
#define CYCLE_COLOR_BUTTON 2
bool animationDirection = false;
bool isColorChanged = false;
long selectedColor = CRGB::Green;
int numOfLedsOn = 0;
// Define the array of leds
CRGB leds[NUM_LEDS];
void setup()
{
Serial.begin(9600);
Serial.println("Code Starting");
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS); // GRB ordering is typical
// Set all leds to black
for (int i = 0; i < NUM_LEDS; i++)
{
leds[i] = CRGB::Black;
}
FastLED.show();
// Set up the button pins
pinMode(ANIMATION_BUTTON, INPUT_PULLUP);
pinMode(CYCLE_COLOR_BUTTON, INPUT_PULLUP);
// attach the interrupt to the cycle color button pin
attachInterrupt(digitalPinToInterrupt(CYCLE_COLOR_BUTTON), cycleColor, RISING);
}
void loop()
{
// Check if the animation button is pressed
if (!digitalRead(ANIMATION_BUTTON))
{
if (!animationDirection)
{
animationDirection = true;
fillUpAnimation();
}
else
{
animationDirection = false;
fillDownAnimation();
}
}
if (isColorChanged)
{
isColorChanged = false;
colorChanged();
}
}
void fillUpAnimation()
{
// Fill up the strip with red
for (int i = 0; i < NUM_LEDS; i++)
{
leds[i] = selectedColor;
FastLED.show();
numOfLedsOn++;
if (isColorChanged)
{
isColorChanged = false;
colorChanged();
}
Serial.println("Going up");
delay(500);
}
}
void fillDownAnimation()
{
// Fill down the strip with red
for (int i = NUM_LEDS - 1; i >= 0; i--)
{
leds[i] = CRGB::Black;
FastLED.show();
numOfLedsOn--;
if (isColorChanged)
{
isColorChanged = false;
colorChanged();
}
Serial.println("Going down");
delay(500);
}
}
void colorChanged()
{
// turn all the on leds to selected color
for (int i = 0; i < numOfLedsOn; i++)
{
leds[i] = selectedColor;
}
FastLED.show();
}
void cycleColor()
{
// Cycle through the colors
if (selectedColor == CRGB::Green)
{
selectedColor = CRGB::Red;
}
else if (selectedColor == CRGB::Red)
{
selectedColor = CRGB::Blue;
}
else if (selectedColor == CRGB::Blue)
{
selectedColor = CRGB::Brown;
}
else if (selectedColor == CRGB::Brown)
{
selectedColor = CRGB::White;
}
else if (selectedColor == CRGB::White)
{
selectedColor = CRGB::Gold;
}
else
{
selectedColor = CRGB::Green;
}
isColorChanged = true;
Serial.println("Color changed");
delay(1000);
}