I am working on a esp32 project where I am trying to light up a series of LED's based on a button press. I got most of it working But I am having trouble getting each of the LED's to turn off after 2 seconds.
Rules are:
If the button is pressed "Switch LED1 ON for 2 seconds"
If the button is Pressed again while LED1 is still on then "Switch LED2 ON for 2 seconds and keep LED1 ON as long as LED2 is on"
If the button is Pressed again while LED-02 is on "Switch LED3 ON for 2 seconds and Keep LED1 and LED2 ON for as long as LED3 is on"
4, Any Button press resets the timer for all 3 LED's
If no new button presses are received within 2 seconds then clear all LED's and wait for new button press
Here is my code so far. All help will be appreciated. Thank you.
const byte buttonPin = 7; // Button Pin
const byte led1Pin = 2; // LED1 Pin
const byte led2Pin = 3; // LED2 Pin
const byte led3Pin = 4; // LED3 Pin
const byte nodesNum = 2;
unsigned long onPeriod = 2500;
int led1State = LOW;
int led2State = LOW;
int led3State = LOW;
byte hasState = 0;
void setup() {
Serial.begin(9600);
// initialize the button pin as a input:
pinMode(buttonPin, INPUT_PULLUP);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
}
void loop() {
checkActivation();
}
void checkActivation() {
boolean BTState = digitalRead(buttonPin); // Current Button State
static boolean prevBTState = HIGH; // Previous Button State
static unsigned long timer = 0;
unsigned long interval = 20;
if (millis() - timer >= interval) {
timer = millis();
BTState = digitalRead(buttonPin); // Get Button Press
if (BTState != prevBTState) // Compare Button State
{
if (BTState == LOW) // If State Change Add to counter ++
{
hasState++; // Check Current state is LOW - State Changed from off to on:
if (hasState > nodesNum) {
hasState = 0;
}
switch (hasState) {
case 0:
digitalWrite(led3Pin, HIGH);
Serial.println("LED3 On");
break;
case 1:
digitalWrite(led1Pin, HIGH);
Serial.println("LED1 On");
break;
case 2:
digitalWrite(led2Pin, HIGH);
Serial.println("LED2 On");
break;
}
}
prevBTState = BTState; // Save Button State
}
}
}
Read your description, looked at the top of your sketch for the pin #s and typed this out, which seems to match your description (debounce is quick & dirty):
Thank you for this as well. I am spending some time going through it to understand it better. I need to incorporate this portion into a larger sketch I am working on that uses a lora signal to trigger it.
//================================================^================================================
// https://forum.arduino.cc/t/adding-a-millis-timer-to-individual-leds-in-case-loop/1307035
//
#define LEDon HIGH //PIN---[220R]---A[LED]K---GND
#define LEDoff LOW
#define PRESSED LOW //+5V---[Internal 50k]---PIN---[Switch]---GND
#define RELEASED HIGH
#define CLOSED LOW //+5V---[Internal 50k]---PIN---[Switch]---GND
#define OPENED HIGH
#define ENABLED true
#define DISABLED false
const byte led1Pin = 2; // LED1 Pin
const byte led2Pin = 3; // LED2 Pin
const byte led3Pin = 4; // LED3 Pin
const byte buttonPin = 7; // Button Pin LOW = pushed
const byte heartbeatLED = 13;
const byte nodesNum = 2;
bool LEDonFlag = DISABLED;
byte lastButtonPin = RELEASED;
byte hasState = 0;
unsigned long heartbeatTime;
unsigned long switchTime;
unsigned long LEDsOFFTime;
// s e t u p ( )
//================================================^================================================
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
digitalWrite(led1Pin, LEDoff);
pinMode(led1Pin, OUTPUT);
digitalWrite(led2Pin, LEDoff);
pinMode(led2Pin, OUTPUT);
digitalWrite(led3Pin, LEDoff);
pinMode(led3Pin, OUTPUT);
pinMode(heartbeatLED, OUTPUT);
} //END of setup()
// l o o p ( )
//================================================^================================================
void loop()
{
//======================================================================== T I M E R heartbeatLED
//is it time to toggle the heartbeat LED ?
if (millis() - heartbeatTime >= 250ul)
{
//restart this TIMER
heartbeatTime = millis();
//toggle the heartbeat LED
digitalWrite(heartbeatLED, digitalRead(heartbeatLED) == HIGH ? LOW : HIGH);
}
//======================================================================== T I M E R switches
//is it time to check our switches ?
if (millis() - switchTime >= 50ul)
{
//restart TIMER
switchTime = millis();
checkSwitches();
}
//======================================================================== T I M E R LEDsOFF
//is it time to turn OFF LEDs ?
if (LEDonFlag == ENABLED && millis() - LEDsOFFTime >= 2000ul)
{
//we are finished with tihis TIMER
LEDonFlag = DISABLED;
//get ready for the next sequence
hasState = 0;
digitalWrite(led1Pin, LEDoff);
digitalWrite(led2Pin, LEDoff);
digitalWrite(led3Pin, LEDoff);
}
} //END of loop()
// c h e c k S w i t c h e s ( )
//================================================^================================================
//
void checkSwitches()
{
byte pinState;
//======================================================================== buttonPin
pinState = digitalRead(buttonPin);
//===================================
//has this switch changed state ?
if (lastButtonPin != pinState)
{
//update to this new state
lastButtonPin = pinState;
//==========================
//did this switch go pressed/closed ?
if (pinState == PRESSED)
{
//======================
switch (hasState)
{
//==============
case 0:
LEDonFlag = ENABLED;
//restart TIMER
LEDsOFFTime = millis();
digitalWrite(led1Pin, LEDon);
Serial.println("LED1 On");
break;
//==============
case 1:
LEDonFlag = ENABLED;
//restart TIMER
LEDsOFFTime = millis();
digitalWrite(led2Pin, HIGH);
Serial.println("LED2 On");
break;
//==============
case 2:
LEDonFlag = ENABLED;
//restart TIMER
LEDsOFFTime = millis();
digitalWrite(led3Pin, HIGH);
Serial.println("LED3 On");
break;
}
hasState++;
//we only have 3 machine states 0, 1, 2
if (hasState > nodesNum)
{
hasState = 0;
}
}
} //END of mySwitch1.pin
//======================================================================== Next Switch
} //END of checkSwitches()
//================================================^================================================
//