Look at this code (it is not finished yet).
Do you understand what is being done ?
#define CLOSED LOW
#define OPEN HIGH
#define LEDoff LOW
#define LEDon HIGH
#define ENABLED true
#define DISABLED false
bool LEDcountingFlag = DISABLED;
const byte counterButton = 11;
const byte startButton = 12;
const byte resetButton = 13;
const byte ledOne = 4;
const byte ledTwo = 5;
const byte ledThree = 6;
const byte ledFour = 7;
const byte heartbeatLED = 9;
byte counter;
byte LEDcount;
byte buttonState;
byte buttonState2;
byte buttonState3;
byte lastButtonState = OPEN;
byte lastButtonState2 = OPEN;
byte lastButtonState3 = OPEN;
//timing stuff
unsigned long currentMillis;
unsigned long heartbeatMillis;
unsigned long switchMillis;
unsigned long LEDmillis;
void setup()
{
Serial.begin(9600);
pinMode(ledOne, OUTPUT);
pinMode(ledTwo, OUTPUT);
pinMode(ledThree, OUTPUT);
pinMode(ledFour, OUTPUT);
pinMode(heartbeatLED, OUTPUT);
pinMode(counterButton, INPUT_PULLUP);
pinMode(startButton, INPUT_PULLUP);
pinMode(resetButton, INPUT_PULLUP);
} //END of setup()
void loop()
{
//get the currentArduino run time
currentMillis = millis();
//************************************* heartbeat TIMER
//used to see if our code is blocking
//the heartbeat LED should regularly toggle, every 1/2 second
//is it time to toggle the LED ?
if (currentMillis - heartbeatMillis >= 500)
{
//restart the TIMER
heartbeatMillis = currentMillis;
//toggle the LED uncomment to enable th heartbeat LED flashing
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//************************************* read switch TIMER
//is it time to check the switches ?
if (currentMillis - switchMillis >= 50)
{
//restart the TIMER
switchMillis = currentMillis;
checkSwitches();
}
//************************************* LED counter TIMER
//if enabled is it time to go to the next LED sequence
if (LEDcountingFlag == ENABLED && currentMillis - LEDmillis >= 1000ul)
{
}
} //END of loop()
//*******************************************************************************************
//scan the switches to see if there was a change in state
void checkSwitches()
{
//*************************************
buttonState = digitalRead(counterButton);
//was there a switch change in state ?
if (lastButtonState != buttonState)
{
//update to the new state
lastButtonState = buttonState;
//did the switch close ?
if (buttonState == CLOSED)
{
if (counter < 15)
{
counter++;
}
else
{
counter = 0;
}
Serial.println(counter);
}
}
//*************************************
buttonState2 = digitalRead(startButton);
//was there a switch change in state ?
if (lastButtonState2 != buttonState2)
{
//update to the new state
lastButtonState2 = buttonState2;
//did the switch close ?
if (buttonState2 == CLOSED)
{
//enable LED counter display
LEDcountingFlag = ENABLED;
}
}
//*************************************
buttonState3 = digitalRead(resetButton);
//was there a switch change in state ?
if (lastButtonState3 != buttonState3)
{
//update to the new state
lastButtonState3 = buttonState3;
//did the switch close ?
if (buttonState3 == CLOSED)
{
//disable the LED sequence and turn off the LEDs
LEDcountingFlag = DISABLED;
digitalWrite(ledOne, LEDoff);
digitalWrite(ledTwo, LEDoff);
digitalWrite(ledThree, LEDoff);
digitalWrite(ledFour, LEDoff);
//reset sequence
counter = 0;
}
}
} //END of checkSwitches()
Should be close:
//https://forum.arduino.cc/t/detecting-third-button-press/919620/1
// Version YY/MM/DD Description
// 1.00 21 10 29 functioning sketch
#define CLOSED LOW
#define OPEN HIGH
#define LEDoff LOW
#define LEDon HIGH
#define ENABLED true
#define DISABLED false
//*******************************************************************************************
bool LEDcountingFlag = DISABLED;
const byte led1 = 4;
const byte led2 = 5;
const byte led4 = 6;
const byte led8 = 7;
const byte heartbeatLED = 10;
const byte counterButton = 11;
const byte startButton = 12;
const byte resetButton = 13;
byte counter;
byte LEDcount;
byte buttonState;
byte buttonState2;
byte buttonState3;
byte lastButtonState = OPEN;
byte lastButtonState2 = OPEN;
byte lastButtonState3 = OPEN;
//timing stuff
unsigned long currentMillis;
unsigned long heartbeatMillis;
unsigned long switchMillis;
unsigned long LEDmillis;
//*******************************************************************************************
void setup()
{
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led8, OUTPUT);
pinMode(heartbeatLED, OUTPUT);
pinMode(counterButton, INPUT_PULLUP);
pinMode(startButton, INPUT_PULLUP);
pinMode(resetButton, INPUT_PULLUP);
} //END of setup()
//*******************************************************************************************
void loop()
{
//get the currentArduino run time
currentMillis = millis();
//************************************* heartbeat TIMER
//used to see if our code is blocking
//the heartbeat LED should regularly toggle, every 1/2 second
//is it time to toggle the LED ?
if (currentMillis - heartbeatMillis >= 500)
{
//restart this TIMER
heartbeatMillis = currentMillis;
//toggle the LED
//comment the next line to stop heartbeat LED flashing
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//************************************* read switch TIMER
//is it time to check the switches ?
if (currentMillis - switchMillis >= 50)
{
//restart this TIMER
switchMillis = currentMillis;
checkSwitches();
}
//************************************* LED display TIMER
//if enabled is it time to go to the next LED sequence
if (LEDcountingFlag == ENABLED && currentMillis - LEDmillis >= 1000ul)
{
//restart this timer
LEDmillis = currentMillis;
//adjust the LEDs to the current count
digitalWrite(led1, LEDcount & 0b0001);
digitalWrite(led2, LEDcount & 0b0010);
digitalWrite(led4, LEDcount & 0b0100);
digitalWrite(led8, LEDcount & 0b1000);
//get ready for the next LED display
if (LEDcount++ >= counter)
{
//back to the beginning
LEDcount = 0;
}
}
} //END of loop()
//*******************************************************************************************
//scan the switches to see if there was a change in state
void checkSwitches()
{
//*************************************
buttonState = digitalRead(counterButton);
//was there a switch change in state ?
if (lastButtonState != buttonState)
{
//update to the new state
lastButtonState = buttonState;
//did the switch close ?
if (buttonState == CLOSED)
{
//are we at the maximum count yet ?
if (counter < 15)
{
counter++;
}
//back to zero
else
{
counter = 0;
}
Serial.println(counter);
}
}
//*************************************
buttonState2 = digitalRead(startButton);
//was there a switch change in state ?
if (lastButtonState2 != buttonState2)
{
//update to the new state
lastButtonState2 = buttonState2;
//did the switch close ?
if (buttonState2 == CLOSED)
{
//enable LED counter display
LEDcountingFlag = ENABLED;
}
}
//*************************************
buttonState3 = digitalRead(resetButton);
//was there a switch change in state ?
if (lastButtonState3 != buttonState3)
{
//update to the new state
lastButtonState3 = buttonState3;
//did the switch close ?
if (buttonState3 == CLOSED)
{
//disable the LED sequence and turn off the LEDs
LEDcountingFlag = DISABLED;
digitalWrite(led1, LEDoff);
digitalWrite(led2, LEDoff);
digitalWrite(led4, LEDoff);
digitalWrite(led8, LEDoff);
//reset the count to values
counter = 0;
LEDcount = 0;
}
}
} //END of checkSwitches()
//*******************************************************************************************