Here is another version of the code using an additional library. Didn't test it but it should change the led strip only when releasing the buttons.
#include <FastLED.h>
#include <JC_Button.h> // https://github.com/JChristensen/JC_Button
/*
No copyright, use it at your own risks ;-)
*/
// Define the arrays of leds
#define PIN_STRIP1 5
#define PIN_STRIP2 3
#define COLOR_ORDER GRB
#define LED_TYPE WS2812
#define NUM_LEDS 4
uint8_t max_bright = 255;
struct CRGB leds1[NUM_LEDS];
struct CRGB leds2[NUM_LEDS];
// Colors for strip 1 using CRGB constants from FastLED library
int ledColor1[4] = {CRGB::AliceBlue, CRGB::Aqua, CRGB::CornflowerBlue, CRGB::Blue};
// Example colors with HEX codes for strip 2
int ledColor2[4] = {0xf79b9b, 0xe8795a, 0xff0000, 0x721005};
// Define buttons
const byte
PIN_BUTTON1 (9),
PIN_BUTTON2 (6),
PIN_BUTTON_RESET (7);
Button BUTTON1(PIN_BUTTON1);
Button BUTTON2(PIN_BUTTON2);
Button BUTTON_RESET(PIN_BUTTON_RESET);
// Global vars
byte NumStrip1 = 0;
byte NumStrip2 = 0;
void setup() {
LEDS.addLeds<LED_TYPE, PIN_STRIP1, COLOR_ORDER>(leds1, NUM_LEDS);
LEDS.addLeds<LED_TYPE, PIN_STRIP2, COLOR_ORDER>(leds2, NUM_LEDS);
FastLED.setBrightness(max_bright);
FastLED.clear();
FastLED.show();
// Connect buttons like this :
// GND -- Button -- Pin
// pinMode(PIN_BUTTON1, INPUT_PULLUP); // Button 1
// pinMode(PIN_BUTTON2, INPUT_PULLUP); // Button 2
// pinMode(PIN_BUTTON_RESET, INPUT_PULLUP); // Reset button
BUTTON1.begin();
BUTTON2.begin();
BUTTON_RESET.begin();
}
void loop() {
Checkbuttons();
}
void Checkbuttons()
{
BUTTON1.read(); // read BUTTON1
if (BUTTON1.wasReleased()) ChangeStrip1 ();
BUTTON2.read(); // read BUTTON2
if (BUTTON2.wasReleased()) ChangeStrip2 ();
BUTTON_RESET.read(); // read Reset BUTTON
if (BUTTON_RESET.wasReleased()) ResetStrips ();
// bool StateB1 = digitalRead(PIN_BUTTON1);
// delay(50);
// bool StateB2 = digitalRead(PIN_BUTTON2);
// delay(50);
// bool StateBReset = digitalRead(PIN_BUTTON_RESET);
// delay(50);
//
// if (!StateB1)
// if (!StateB2) ChangeStrip2 ();
// if (!StateBReset) ResetStrips ();
}
void ChangeStrip1 () {
leds1[NumStrip1] = ledColor1[NumStrip1];
NumStrip1 = min(NumStrip1 + 1, NUM_LEDS - 1);
FastLED.show();
}
void ChangeStrip2 () {
leds2[NumStrip2] = ledColor2[NumStrip2];
NumStrip2 = min(NumStrip2 + 1, NUM_LEDS - 1);
FastLED.show();
}
void ResetStrips () {
FastLED.clear();
NumStrip1 = 0;
NumStrip2 = 0;
FastLED.show();
}
You need to install the library : in the Arduino IDE, go to sketch > include a library > manage library, it opens a new window where you can search for a library. Search for "JC" or "JC_Button", and click 'info' then 'install' (I hope it works like this in the English version of the IDE).
Then compile and test.
Enjoy and tell me if it works ! If yes, you can delete the commented lines.