// 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)
// button stuff
byte buttonPin = 12; // DIO pin for button N.O. pin
int cases = 3, buttonCounter; // count button presses
unsigned long buttonTimer, buttonTimeout = 50; // used in readButton() debounce timer
bool currentButtonState, lastButtonRead; // record button states
int i, j;
unsigned long sketch01Timer, sketch01Timeout = 200;
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP); // pin --> button (n.o.) button --> gnd
Serial.print("Press yer button on Pin ");
Serial.print(buttonPin);
Serial.println(".");
Serial.print("buttonCounter: ");
Serial.print(buttonCounter);
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() {
readButton();
switch (buttonCounter) {
case 0: sketch01(); break;
case 1: sketch02(); break;
case 2: sketch03(); break;
case 3: timeTravelChaser(); break;
default: break;
}
if (buttonCounter > cases)
buttonCounter = 0;
}
void sketch01() { // GROUP_SIZE chaser
if (millis() - sketch01Timer > sketch01Timeout) { // if time between "now" and "last time" is greater than timeout
sketch01Timer = millis(); // reset timer
if (i++ > NUM_LEDS - GROUP_SIZE) // constrain trailing LED count to NUM_LEDS - GROUP_SIZE
i = 0; // start for trailing LED
fill_solid(leds + i, GROUP_SIZE, CRGB::White ); // fill LEDs from leds + i to leds + GROUP_SIZE
FastLED.show(); // display the buffer
FastLED.clear(); // clear the buffer
}
}
void sketch02() {
Serial.print(2);
}
void sketch03() {
Serial.print(3);
}
void timeTravelChaser() {
Serial.print(4);
}
void flashRapidly(int count, int flashDelay) {
for (int i = 0; i < count; i++) {
fill_solid(leds, NUM_LEDS, CRGB::White); // All LEDs white
FastLED.show();
delay(flashDelay);
FastLED.clear(); // All LEDs off
FastLED.show();
delay(flashDelay);
}
}
void readButton() {
// =======================================================
// READ A BUTTON AND INCREMENT A COUNTER
// =======================================================
bool currentButtonRead = digitalRead(buttonPin); // read button pin
if (currentButtonRead != lastButtonRead) { // if THIS read is not the same as LAST read...
buttonTimer = millis(); // ...start a timer by storing millis()
lastButtonRead = currentButtonRead; // ... and store current state
}
if ((millis() - buttonTimer) > buttonTimeout) { // if button change was longer than debounce timeout
if (currentButtonState == HIGH && lastButtonRead == LOW) { // ... and State is "NOT pressed" while Button "IS PRESSED"
//==================================================
digitalWrite(LED_BUILTIN, HIGH); // The button WAS pressed...
Serial.print(++buttonCounter); // Do something here...
digitalWrite(LED_BUILTIN, LOW); // ... or call functions here.
//==================================================
}
currentButtonState = currentButtonRead; // update button state for first if() condition test
}
}