I try'd your code but undesirable things happened. The 1st string 1st led flickers and when button 1 is pressed and held it lights up the rgb strip while holding it down starting at 1 and ending at 2nd strip 1, if you hold down the reset button it stops flickering until you let go.
I have made some real primative code that almost works perfectly. have a look. Only problem is that the turning on of the leds is glitchy. it will light more than one at a time and wont light the 4th works on both strings each controlled by its respective button, and the third button reset works.
#include <FastLED.h>
#define NUM_LEDS 4
#define DATA_PIN 5
CRGB leds[NUM_LEDS];
#define NUM_LEDS2 4
#define DATA_PIN2 3
CRGB leds2[NUM_LEDS2];
const int buttonPin = 7;
const int buttonPin2 = 6;
const int buttonPin3 = 9;
int buttonState = 0;
int buttonState1 = 0;
int buttonState2 = 0;
class PushButton
{
public:
PushButton(uint8_t pin) // Constructor (executes when a PushButton object is created)
: pin(pin) { // remember the push button pin
pinMode(pin, INPUT_PULLUP); // enable the internal pull-up resistor
};
bool isPressed() // read the button state check if the button has been pressed, debounce the button as well
{
bool pressed = false;
bool state = digitalRead(pin); // read the button's state
int8_t stateChange = state - previousState; // calculate the state change since last time
if (stateChange == falling) { // If the button is pressed (went from high to low)
if (millis() - previousBounceTime > debounceTime) { // check if the time since the last bounce is higher than the threshold
pressed = true; // the button is pressed
}
}
if (stateChange == rising) { // if the button is released or bounces
previousBounceTime = millis(); // remember when this happened
}
previousState = state; // remember the current state
return pressed; // return true if the button was pressed and didn't bounce
};
private:
uint8_t pin;
bool previousState = HIGH;
unsigned long previousBounceTime = 0;
const static unsigned long debounceTime = 25;
const static int8_t rising = HIGH - LOW;
const static int8_t falling = LOW - HIGH;
};
PushButton pushbutton = {7};
class PushButton2
{
public:
PushButton2(uint8_t pin2) // Constructor (executes when a PushButton object is created)
: pin2(pin2) { // remember the push button pin
pinMode(pin2, INPUT_PULLUP); // enable the internal pull-up resistor
};
bool isPressed() // read the button state check if the button has been pressed, debounce the button as well
{
bool pressed = false;
bool state2 = digitalRead(pin2); // read the button's state
int8_t stateChange2 = state2 - previousState2; // calculate the state change since last time
if (stateChange2 == falling) { // If the button is pressed (went from high to low)
if (millis() - previousBounceTime2 > debounceTime2) { // check if the time since the last bounce is higher than the threshold
pressed = true; // the button is pressed
}
}
if (stateChange2 == rising) { // if the button is released or bounces
previousBounceTime2 = millis(); // remember when this happened
}
previousState2 = state2; // remember the current state
return pressed; // return true if the button was pressed and didn't bounce
};
private:
uint8_t pin2;
bool previousState2 = HIGH;
unsigned long previousBounceTime2 = 0;
const static unsigned long debounceTime2 = 25;
const static int8_t rising = HIGH - LOW;
const static int8_t falling = LOW - HIGH;
};
PushButton2 pushbutton2 = {6};
class PushButton3
{
public:
PushButton3(uint8_t pin3) // Constructor (executes when a PushButton object is created)
: pin3(pin3) { // remember the push button pin
pinMode(pin3, INPUT_PULLUP); // enable the internal pull-up resistor
};
bool isPressed() // read the button state check if the button has been pressed, debounce the button as well
{
bool pressed = false;
bool state3 = digitalRead(pin3); // read the button's state
int8_t stateChange3 = state3 - previousState3; // calculate the state change since last time
if (stateChange3 == falling) { // If the button is pressed (went from high to low)
if (millis() - previousBounceTime3 > debounceTime3) { // check if the time since the last bounce is higher than the threshold
pressed = true; // the button is pressed
}
}
if (stateChange3 == rising) { // if the button is released or bounces
previousBounceTime3 = millis(); // remember when this happened
}
previousState3 = state3; // remember the current state
return pressed; // return true if the button was pressed and didn't bounce
};
private:
uint8_t pin3;
bool previousState3 = HIGH;
unsigned long previousBounceTime3 = 0;
const static unsigned long debounceTime3 = 25;
const static int8_t rising = HIGH - LOW;
const static int8_t falling = LOW - HIGH;
};
PushButton3 pushbutton3 = {9};
void setup() {
// put your setup code here, to run once:
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.addLeds<NEOPIXEL, DATA_PIN2>(leds2, NUM_LEDS2);
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
FastLED.clear();
FastLED.show();
}
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (pushbutton.isPressed()) {
// turn LED on:
press1();
} else if
(pushbutton.isPressed()){
press2();
delay(150);
} else if
(pushbutton.isPressed()){
press3();
delay(150);
} else if
(pushbutton.isPressed()){
press4();
delay(150);
}
else if (pushbutton2.isPressed()){
string2press1();
delay(150);
}
else if (pushbutton2.isPressed()){
string2press2();
delay(150);
}
else if (pushbutton2.isPressed()){
string2press3();
delay(150);
}
else if (pushbutton2.isPressed()){
string2press4();
}
else if (pushbutton3.isPressed()){
resetStrings();
}
}
void press1(){
leds[0] = CRGB::Blue;
FastLED.show();
delay(30);
}
void press2(){
leds[1] = CRGB::Blue;
FastLED.show();
delay(30);
leds[0] = CRGB::Blue;
FastLED.show();
delay(30);
}
void press3(){
leds[2] = CRGB::Blue;
FastLED.show();
delay(30);
leds[1] = CRGB::Blue;
FastLED.show();
delay(30);
leds[0] = CRGB::Blue;
FastLED.show();
delay(30);
}
void press4(){
leds[3] = CRGB::Blue;
FastLED.show();
delay(3);
leds[2] = CRGB::Blue;
FastLED.show();
delay(30);
leds[1] = CRGB::Blue;
FastLED.show();
delay(30);
leds[0] = CRGB::Blue;
FastLED.show();
delay(30);
}
void string2press1(){
leds2[0] = CRGB::Blue;
FastLED.show();
delay(30);
}
void string2press2(){
leds2[1] = CRGB::Blue;
FastLED.show();
delay(30);
leds2[0] = CRGB::Blue;
FastLED.show();
delay(30);
}
void string2press3(){
leds2[2] = CRGB::Blue;
FastLED.show();
delay(30);
leds2[1] = CRGB::Blue;
FastLED.show();
delay(30);
leds2[0] = CRGB::Blue;
FastLED.show();
delay(30);
}
void string2press4(){
leds2[3] = CRGB::Blue;
FastLED.show();
delay(3);
leds2[2] = CRGB::Blue;
FastLED.show();
delay(30);
leds2[1] = CRGB::Blue;
FastLED.show();
delay(30);
leds2[0] = CRGB::Blue;
FastLED.show();
delay(30);
}
void resetStrings(){
FastLED.clear();
FastLED.show();
}
I know it aint pretty but i think it can be made to work. Is there a better way to activate my functions one button press at a time. maybe ? Could maybe a switch Case be used to activate the led on functions.