Functionality:
Before the red dome button is pressed (not a 2 state button), the serial monitor will print a list "0"s and when the red dome button is pressed, the button state will toggle from LOW to HIGH, hence at the serial monitor will print a list of "1"s.
However, when the button is in the state of HIGH, serial monitor print "1"s and user will not be able to toggle the button state from HIGH to LOW. Hence, the button can only toggle from HIGH to LOW automatically after a period, delay(25s).
Therefore, correct behaviour:
Initial state print => 00000000000000(when user presses the red dome button => buttonstate change LOW to HIGH)111111111111111111(when user presses the button, nothing happens)111111111111111111(after 25s delay, buttonState will toggle HIGH to LOW)0000000000000
ISSUE:
At this point in time, users are able to toggle between LOW to HIGH and HIGH to LOW. Meaning, the flow => 00..000(user press button, Toggle LOW to HIGH)111...111(user press button, toggle HIGH to LOW)0000...
And I am not really sure on how to enable the button to only toggle from LOW to HIGH but disable the button to toggle from HIGH to LOW.
Meaning when user push the button, it can change the button state from "0" to "1" but not able to change the button state when it is in "1".
Hence, I would like to request for some help that will allow the following correct behaviour.
Thanks
Code:
const int buttonPin = 2; //the number of the pushbutton pin
const int Relay = 4; //the number of the LED relay pin
uint8_t stateLED = LOW;
uint8_t btnCnt = 1;
int buttonState = 0; //variable for reading the pushbutton status
int buttonLastState = 0;
int outputState = 0;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(Relay, OUTPUT);
digitalWrite(Relay, LOW);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// Check if there is a change from LOW to HIGH
if (buttonLastState == LOW && buttonState == HIGH)
{
outputState = !outputState; // Change outputState
}
buttonLastState = buttonState; //Set the button's last state
// Print the output
if (outputState)
{
switch (btnCnt++) {
case 100:
stateLED = LOW;
digitalWrite(Relay, HIGH); // after 10s turn on
break;
case 250:
digitalWrite(Relay, LOW); // after 20s turn off
//Toggle ButtonState to LOW from HIGH without user pressing the button
digitalWrite(buttonPin, LOW);
break;
case 252: // small loop at the end, to do not repeat the LED cycle
btnCnt--;
break;
}
Serial.println("1");
}else{
Serial.println("0");
if (btnCnt > 0) {
// disable all:
stateLED = LOW;
digitalWrite(Relay, LOW);
}
btnCnt = 0;
}
delay(100);
}