/*
Project: FastLED Light Saber
Description: Demonstrates a very basic animation
for a light saber using FastLED
Creation date: 7/22/23
Author: AnonEngineering
License: https://en.wikipedia.org/wiki/Beerware
*/
#include <FastLED.h>
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 25
const int LED_PIN = 2;
const int BTN_PIN = 3;
const long INTERVAL = 50;
bool saberMode = false;
int oldBtnState = 1;
unsigned long previousTime = 0;
CRGB leds[NUM_LEDS];
void checkButton() {
int btnState = digitalRead(BTN_PIN);
if (btnState != oldBtnState) {
if (btnState == LOW) {
saberMode = !saberMode;
//Serial.println("Button pressed");
Serial.print("Saber mode: ");
Serial.println(saberMode ? "On" : "Off");
} else {
//Serial.println("Button released");
}
delay(20); // for debounce
}
oldBtnState = btnState;
}
void doSaber() {
static int ledVal = 0;
if (saberMode == true ) {
if (millis() - previousTime >= INTERVAL) {
previousTime = millis();
leds[ledVal] = CRGB(2, 248, 6);
ledVal++;
if (ledVal > NUM_LEDS) ledVal = NUM_LEDS;
FastLED.show();
}
} else {
if (millis() - previousTime >= INTERVAL) {
previousTime = millis();
leds[ledVal] = CRGB::Black;
ledVal--;
if (ledVal < 0) ledVal = 0;
FastLED.show();
}
}
}
void setup() {
Serial.begin(115200);
pinMode(BTN_PIN, INPUT_PULLUP);
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
}
void loop() {
checkButton();
doSaber();
}