#include <EasyButton.h>
#define maxLeds 4
const int ledPins[maxLeds] = {25,26,27,14};
const int btnPins[maxLeds] = {5,4,2,15};
const bool PullupTrue = true;
const bool InvertTrue = true;
const int debounceTimer = 35;
volatile int ledOnTime[maxLeds] = {3,3,3,3};
volatile int ledTriggerTime[maxLeds] = {1,2,3,4};
volatile boolean ledQueue[maxLeds] = {true,true,true,true};
volatile long previousTime[maxLeds] = {0,0,0,0};
long previousM = 0;
volatile int activeLed = maxLeds;
boolean timerStarted = false;
const int sec = 1000;
int turnOnNextLed = true;
EasyButton RedButton(btnPins[0],debounceTimer,PullupTrue,InvertTrue);
EasyButton GreenButton(btnPins[1],debounceTimer,PullupTrue,InvertTrue);
EasyButton BlueButton(btnPins[2],debounceTimer,PullupTrue,InvertTrue);
EasyButton YellowButton(btnPins[3],debounceTimer,PullupTrue,InvertTrue);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
for(int thisPin = 0; thisPin < maxLeds;thisPin++){
pinMode(ledPins[thisPin], OUTPUT);
digitalWrite(ledPins[thisPin], LOW);
}
RedButton.begin();
RedButton.onPressed(RedBtnPressed);
RedButton.enableInterrupt(RedBtnCall);
GreenButton.begin();
GreenButton.onPressed(GreenBtnPressed);
GreenButton.enableInterrupt(GreenBtnCall);
BlueButton.begin();
BlueButton.onPressed(BlueBtnPressed);
BlueButton.enableInterrupt(BlueBtnCall);
YellowButton.begin();
YellowButton.onPressed(YellowBtnPressed);
YellowButton.enableInterrupt(YellowBtnCall);
}
///////////////////////////////////Button Interrupt Callbacks
void loop() {
RedButton.update();
GreenButton.update();
BlueButton.update();
YellowButton.update();
QueueNextLed();
for(int thisLed = 0; thisLed < maxLeds;thisLed++){
if(thisLed == activeLed){
LedTimerFunction(thisLed);
}
}
}
void RedBtnCall(){
RedButton.read();
}
void GreenBtnCall(){
GreenButton.read();
}
void BlueBtnCall(){
BlueButton.read();
}
void YellowBtnCall(){
YellowButton.read();
}
/////////////////////////////////////Buttons pressed funtions
void RedBtnPressed(){
ledQueue[0] = true;
}
void GreenBtnPressed(){
ledQueue[1] = true;
}
void BlueBtnPressed(){
ledQueue[2] = true;
}
void YellowBtnPressed(){
ledQueue[3] = true;
}
void QueueNextLed(){
if(turnOnNextLed){
int counter = 0;
for(int thisLed = 0;thisLed < maxLeds;thisLed++){
if(ledQueue[thisLed]){
activeLed = thisLed;
turnOnNextLed = false;
break;
}
else{
counter++;
}
}
if(counter >= maxLeds){
activeLed = maxLeds;
}
}
}
void LedTimerFunction (int ledNum){
if(timerStarted == false && turnOnNextLed == false){
digitalWrite(ledPins[ledNum], HIGH);
timerStarted = true;
//previousTime[ledNum] = millis();
previousM = millis();
Serial.println("Timer started for LED " + String(ledNum));
}
if (((long)millis() - previousM) >= (ledOnTime[ledNum] * sec)){
Serial.println("Time completed = "+ String((long)millis() - previousM));
digitalWrite(ledPins[ledNum], LOW);
ledQueue[ledNum] = false;
activeLed = maxLeds;
//previousTime[ledNum] = millis();
previousM = millis();
turnOnNextLed = true;
timerStarted = false;
Serial.println("Timer completed for LED " + String(ledNum));
}
}