toggling a true / false value each time a loop is called

Hi everyone,

I am trying to get a true/false value to switch each time I enter a specific loop. The code below isn’t working right, I am not sure if the “!” operator can even be used for this purpose

void setup() {
boolean TickTock = false; // switches between "time remaining" and "temperature" display in the "maintain heat" mode

void loop() {

        if (millis() - Time5 > DisplaySwitchTime) {
          TickTock = !TickTock; // switch the display
          Time5 = millis(); // 

According to the reference page, this is exactly what the code should look like but the value of "TickTock" does not change,it is always 0

You need to go back to the reference page and look at the section on "scope".

Also, you need to post ALL your code.

The code snippet you posted will not even compile. Did you leave some part of your sketch out?

boolean TickTock = false; // switches between "time remaining" and "temperature" display in the "maintain heat" mode

If you left out the declaration of the global variable TickTock, this local variable declaration/initialization is useless, since this variable goes out of scope immediately.

If you didn't, then TickTock is not even in scope when you try to toggle it. Seeing all the other variables, especially types, is important. The if() statement in the function may never evaluate to true if some types are wrong.

Thanks for the replies, everyone.

I did not include the entire code, its big and I didn't want everyone to have to dig around in my mess.

I found the problem, in another section of the code I had written

if(TickTock = false)

instead of

if(TickTock == false)

Now, I see why posting the whole code is important :blush:

Write it if ( false = TickTock), and the compiler will flag the error for you.

Thanks AWOL. Are you referring to the space after the "if" and the second space after the "(" ?

No, the compiler ignores white space, however it doesn’t ignore the order of operands.

Hi Brian

No, you did not find the error...

This code is flipping the boolean - can you spot the difference?

unsigned long Time5;
unsigned long DisplaySwitchTime = 1000;
boolean TickTock = false;

void setup(){

void loop(){
  if (millis() - Time5 > DisplaySwitchTime){
    TickTock = !TickTock;
    Time5 = millis();


Fletcher; I figured I had found the error since my code now works :)

I do not see a difference, please share?

Hi Brian

You had the boolean declared inside the:

void setup(){ ....


Psst, Fletcher, see reply #2. ;)