Reputation: 990
functionality:
User depresses the red dome button Red Dome Button, it is suppose to signal that the buttonState is HIGH and at the serial monitor, it should be printing "1" at every 100ms and after a delay of 5s: the LED light will be in the HIGH state which will light up for about 10s and after, the LED light will switch to the LOW state, meaning the LED light will be off.
Hence the flow:
Correct behaviour:
initial state-> Serial monitor displays "0"s when user press the button -> Serial monitor displays "1"s at every 100ms and after a delay of 10seconds, the LED state will be HIGH.
and after a delay of 10s the LED state will be LOW, the serial monitor display is still at "1"s at every 100ms to signal that the buttonstate of the red dome button is still at HIGH
Issue:
Current Behaviour: initial state-> Serial monitor displays "0"s when user press the button -> Serial monitor displays a single "1" and is not displaying a continuous "1"s, but after a delay of 10seconds, the LED state will be HIGH.
and after a delay of 10s the LED state will be LOW. At this point, the LED should not be HIGH again, however, after a delay of 10s, the LED state becomes HIGH and LOW after 10s. It then becomes a loop. the serial monitor display is still at "1" to signal that the buttonstate of the red dome button is still at HIGH
Hence, how do I enable that the once the button is pressed, it will show a continuous "1" and a delay of 10s, the LED will be a state of HIGH and a further 10s delay, the LED state will be LOW. and the LED will remain a state of LOW thereafter even though,the buttonstate is at HIGH
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 5s turn on
break;
case 200:
digitalWrite(Relay, LOW); // after 10s turn off
break;
case 102: // small loop at the end, to do not repeat the cycle
btnCnt--;
break;
}
Serial.println("1");
}else{
Serial.println("0");
if (btnCnt > 0) {
// disable all:
stateLED = LOW;
digitalWrite(Relay, LOW);
}
btnCnt = 0;
}
delay(100);
}
Upvotes: 0
Views: 89
Reputation: 3739
What do you expect, when you have delay(10000)
and then delay(2000);
. When it should print these "1" every 100ms if you're waiting so much longer?
Your outputState is changed on button change, but you can skip that part by using button state directly - it's exactly the same.
I can imagine something like that (untested, it's just concept):
const int buttonPin = 2;
const int Relay = 4;
uint8_t stateLED = LOW;
uint8_t btnCnt = 1;
void loop() {
if (digitalRead(buttonPin) == HIGH) {
switch (btnCnt++) {
case 0: case 1:
stateLED = HIGH; // no idea, why is that in original code, but whatever
break;
case 50:
stateLED = LOW;
digitalWrite(Relay, HIGH); // after 5s turn on
break;
case 100:
digitalWrite(Relay, LOW); // after 10s turn off
break;
case 102: // small loop at the end, to do not repeat the cycle
btnCnt--;
break;
}
Serial.println("1");
} else {
if (btnCnt > 0) {
Serial.println("0");
// disable all:
stateLED = LOW;
digitalWrite(Relay, LOW);
}
btnCnt = 0;
}
delay(100);
}
void setup() {
Serial.begin(57600);
pinMode(buttonPin, INPUT);
pinMode(Relay, OUTPUT);
digitalWrite(Relay, LOW);
}
Upvotes: 0