// https://forum.arduino.cc/t/joining-4-arduino-sketches-in-to-one-sketch-and-adding-a-button/1416208/
#include <FastLED.h>
#define NUM_LEDS 10// Total number of LEDs in your strip
#define DATA_PIN 3 // The Arduino pin connected to the Data In line
#define LED_TYPE WS2812B // Adjust to your specific LED type
#define COLOR_ORDER GRB // Adjust to your specific color order (may be RGB or BGR)
#define BRIGHTNESS 255 // Adjust brightness (0-255)
CRGB leds[NUM_LEDS];
// sketch one
#define GROUP_SIZE 2 // Number of LEDs in each group
#define FLASH_DELAY 200 // Delay in milliseconds for each flash (controls speed)
// sketch two
#define CHASE_DELAY 100
#define CHASER_SIZE 4
#define CHASER_COLOR CRGB(255, 255, 255)
#define BUTTON_PIN 2
#define NUM_MODES 5
int mode, currentButtonRead, lastButtonRead;
unsigned long timer, timeout = 100;
bool currentButtonState;
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
// FastLED.setCorrection(TypicalLEDStrip); // what is this and is it needed?
FastLED.setBrightness(BRIGHTNESS);
FastLED.clear(); // Turn all LEDs off
FastLED.show(); // Update the strip
}
void loop() {
switch (mode) {
case 0: sketch00(); readButton(); break;
case 1: sketch01(); break;
case 2: sketch02(); break;
case 3: timeTravelChaser(); break;
case 4: flashRapidly(50, 20); // Flash 50 times with a 20ms delay
}
}
void sketch00() {
for (int i = 0; i < NUM_LEDS; i += GROUP_SIZE) {
for (int j = 0; j < GROUP_SIZE; j++) {
readButton();
if (i + j < NUM_LEDS) { // Ensure we don't go out of bounds
leds[i + j] = CRGB::White;
}
}
FastLED.show(); // Display the change
delay(FLASH_DELAY);
for (int j = 0; j < GROUP_SIZE; j++) {
readButton();
if (i + j < NUM_LEDS) {
leds[i + j] = CRGB::Black; // Black is off
}
}
FastLED.show();
delay(FLASH_DELAY / 2);
}
}
void sketch01() {
for (int i = 0; i <= NUM_LEDS; i++) {
FastLED.clear();
for (int j = 0; j < CHASER_SIZE; j++) {
readButton();
if (i + j < NUM_LEDS) {
leds[i + j] = CHASER_COLOR;
}
}
FastLED.show();
delay(CHASE_DELAY);
}
}
void sketch02() {
for (int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed++) {
readButton();
leds[whiteLed] = CRGB::White;
FastLED.show();
delay(30);
leds[whiteLed] = CRGB::Black;
}
}
void timeTravelChaser() {
int delayTime = 100; // Initial delay in milliseconds (slowest speed)
int minDelay = 10; // Minimum delay for flashing effect
int delayDecrement = 5; // How much to speed up each cycle
while (delayTime >= minDelay) {
for (int i = 0; i < NUM_LEDS + 1; i++) {
readButton();
FastLED.clear();
if (i < NUM_LEDS) {
leds[i] = CRGB::White;
}
if (i + 1 < NUM_LEDS) {
leds[i + 1] = CRGB::White;
}
FastLED.show();
delay(delayTime); // Use the variable delay
}
delayTime -= delayDecrement;
}
FastLED.clear();
FastLED.show();
}
void flashRapidly(int count, int flashDelay) {
for (int i = 0; i < count; i++) {
readButton();
fill_solid(leds, NUM_LEDS, CRGB::White); // All LEDs white
FastLED.show();
delay(flashDelay);
FastLED.clear(); // All LEDs off
FastLED.show();
delay(flashDelay);
}
}
void advanceMode() {
mode = (mode + 1) % NUM_MODES;
}
void readButton() {
bool currentButtonRead = digitalRead(BUTTON_PIN); // read button pin
if (currentButtonRead != lastButtonRead) { // if button pin changes...
timer = millis(); // ...start a timer
lastButtonRead = currentButtonRead; // ... and store current state
}
if ((millis() - timer) > timeout) { // if button change was longer than debounce timeout
if (currentButtonState == HIGH && lastButtonRead == LOW) { // ... and State is "RELEASED" while Button Read is "PRESSED"
Serial.print(mode);
mode++;
Serial.print(mode);
if (mode > 4)
mode = 0;
currentButtonState = currentButtonRead; // update button state
}
}
}