Haha, well, we all had to start somewhere.
Do it in small steps.
Let’s start with the 3rd sketch, and move the loop() function to a separate function. This would look something like this. Look at the part of the code that I made bold.
Now the entire code of the loop has become its own function.
#include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 36
#define blinkcount 3
#define DATA_PIN 8
int button = 4; //D2 (gpio4)
int buttonState=0;
// Define the array of leds
CRGB leds[NUM_LEDS];
void setup() {
// Uncomment/edit one of the following lines for your leds arrangement.
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
pinMode(button, INPUT);
LEDS.setBrightness(60);
}
void loop() {
doLEDEffect();
}
void doLEDEffect() {
buttonState=digitalRead(button); // put your main code here, to run repeatedly:
if (buttonState == HIGH) {
for(int blink=0; blink<blinkcount; blink++) {
for(int i=0; i<NUM_LEDS; i++) {
leds[i].setRGB(255,0,0);
FastLED.show();
delay(10);
}
delay(100);
// Cleanup swipe after swipe
//setAll(0,0,0);
for(int i=0; i<NUM_LEDS; i++) {
leds[i].setRGB(0,0,0);
FastLED.show();
delay(10);
}
delay(100);
}
setAll(0,0,0);
}
else {
setAll(0,0,0);
}
}
// Set all LEDs to a given color and apply it (visible)
void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_LEDS; i++ ) {
leds[i].setRGB(red, green, blue);
}
FastLED.show();
}