Each time push button pressed it turn on LED1 and LED2 and start counting, after (const long period) seconds turn off LED2. pressing the push button again will turn off LED1 and turn on LED2 and starts counting again.
What i want this code to do?
I want to keep checking the if statement; if LEDstatus == LOW do that. check again; is it still == LOW? do that again (in a loop).
What is the problem?
(If LEDstatus == LOW) doing the "commands" inside that IF only once.
I want to keep checking the if statement; if LEDstatus == LOW do that. check again; is it still == LOW? do that again (in a loop).
The if statement does NOT loop. If you want to check the statement over and over, you have to do the checking in the body of some statement that does loop. Like the aptly-named loop() function.
PaulS:
The if statement does NOT loop. If you want to check the statement over and over, you have to do the checking in the body of some statement that does loop. Like the aptly-named loop() function.
i'm not familiar with aptly-named loop(), can you please provide an example?
There are 2 functions that must be included in an Arduino sketch. The setup() function runs 1 time and is used to initailize the machine. The other is the loop() function that runs over and over (loops) endlessly and contains the body of the program.
but i can't have the toggle thing if i just remove the other parts of that code in my first post!
The "other parts" of your code go where the comment says "some stuff" or where the comment says "more stuff", depending on whether you want the "other parts" to happen before, or after, you check the value in LEDStatus.
PaulS:
The "other parts" of your code go where the comment says "some stuff" or where the comment says "more stuff", depending on whether you want the "other parts" to happen before, or after, you check the value in LEDStatus.
There are some things missing from your description:
You end in a different state then you start. So how to continue? Is led1 supposed to turn on ever again?
What should happen when you press the button again when led2 is on?
And about the if(), I really don't see the problem.
You do have a non-blocking loop called loop()
You have a statement you want to check and you do know how
You want to check that over and over again
Aka, just make it part of that damn loop()
And a tip about keeping track of output states:
digitalWrite(pin_LED2, LOW);
LED2status = LOW;
There will be a moment you forget to update that status variable as well. And hé, who want to do stuff twice anyway? Solution 1
Don't call the digitalWrite() there. Only set the state variable where you want to change (or define) the state of the output. And just unconditionally call digitalWrite() with that state in the loop()
void loop(){
if(youWantToChangeState){
state = HIGH;
}
//more stuff
//..even more stuff
digitalWrite(pin, state);
}
Solution 2
In my opinion he easiest solution. Don't bother with the state variable at all. Just use digitalRead() instead. Yes, this also works on an output