I'm working on a project with 2 seperate pushbuttons, a start and a stop. I have managed to program the start function correctly, but the stop button won't work. I think this must be programmed the same way (state=false)? I hope there is a solution to this.
The code:
const int Start = 8;
const int Stop = 9;
const int Relais = 7;
const int Button3 = 6;
int Interval = 2000;
int Startstate = 0;
int Stopstate = 0;
boolean Blinkstate = false;
// Integers and Booleans. (don't mind Button 3, it's for a future expansion.
void loop() {
// Read the state of the pushbutton values:
Startstate = digitalRead(Start);
Stopstate = digitalRead(Stop);
// check if the start button is pressed. If it is, the startstate is HIGH:
if (Startstate == HIGH && Stopstate == LOW ) {
Blinkstate = true;
} // set blinkstate active
// check if the start button is pressed. If it is, the stopstate is HIGH:
if (Stopstate == HIGH && Startstate == LOW) {
Blinkstate = false;
} // set blinkstate non-active
while (Blinkstate == true)
blinkFunction(); // when Blinkstate is true, perform the blinkFunction.
}
while (Blinkstate == true)
blinkFunction(); // when Blinkstate is true, perform the blinkFunction.
Once it enters this while loop, how will it ever exit. How can the state of Blinkstate change? If it is stuck in that loop, Blinkstate cannot be read.
And with those delays the program will never be very responsive. You have to wait for the delays to expire before you can read the switches. Better to learn to write non blocking code using millis() or micros for timing.
Non-blocking timing tutorials: Several things at a time. Beginner's guide to millis(). Blink without delay().
How are the switches wired? Are there pulldown resistors on those pins?