system
1
I am looking to change between states in a loop. i.e. every second time I go through the loop, I want to do a different test.
e.g. If State was true, change to false. And if If State was false, change to true.
I tried something like below but it obviously doesn't work because it is changing back to true before the task is being completed.
if (Test_State==true) {
Test_State==false;
}
if (Test_State==false) {
Test_State==true;
}
Any ideas?
Thanks.
This is wrong anyway:
if (Test_State==true) {
Test_State==false;
}
You mean:
if (Test_State==true) {
Test_State = false;
}
However the simplest is to "negate" the state. eg.
void loop ()
{
// do whatever
// flip the state
Test_State = !Test_State;
}
system
3
Thank you for both comments! The "!" is exactly what I was looking for!
if ( Test_State == true )
{
Test_State = false;
}
if ( Test_State == false )
{
Test_State = true;
}
So do you walk through your code line by line?
What happens if you start with Test_State as true?
What happens if you start with Test_State as false?
First line - if ( Test_State == true )
then what happens?
What value does Test_State have?
And when it gets to - if ( Test_State == false )
what happens?
You might want to have a read about else that goes with if:
http://arduino.cc/en/Reference/Else