[SOLVED] Weird global variable assignation issue

I'm an experienced developer trying Arduino for the first time. And I'm facing a weird global variable assignation issue.

Code:

bool someGlobalVar = false;

void setup() {
  Serial.begin(9600);
  // someGlobalVar = false; // I even tried this
}

void loop() {
  // static bool someGlobalVar = false; // I even tried a local static variable
  Serial.println("loop");
  Serial.print("someGlobalVar: ");
  Serial.println(someGlobalVar);

  if (someGlobalVar == false) { // but in the 1st run, someGlobalVar is always true
    Serial.println("switching...");
    someGlobalVar = true;
  }

  delay(1000);
}

Expected:

loop
someGlobalVar: 0
switching...
loop
someGlobalVar: 1
loop
someGlobalVar: 1
...

Actual:

loop
someGlobalVar: 1
loop
someGlobalVar: 1
loop
someGlobalVar: 1
...

In the 1st run, someGlobalVar always turns out to be true instead of false. Any idea what's going wrong?

What evidence do you have of that ?
What do you see in the Serial monitor of you reset the board ?

@UKHeliBob it isn't. But that's not the point @gamesticker is asking about.

I'm stumped!

EDIT: @UKHeliBob changed his question. Ignore these comments. Except the comment about me being stumped...

@PaulRB you posted while I was updating my post, sorry !

Um, that's what I'm doing. I have pasted the output of Serial Monitor, see?

My guess is that everything works as expected, but that the first iteration missed.

Perhaps try adding a delay(5000) in setup().

Tried adding delay in setup, doesn't help.

Off topic... Bang goes my theory that the forum only shows a post as updated if it knows someone else could have read it before it was changed. Must be based on timing or some other criteria.

Which Arduino are you using ?
Try putting a delay(1000) at the end of setup() or increasing the baud rate to 115200

Sorry to have confused things by editing my post. Edits made within some short period do not show as changes

No, we couldn't see, because nothing gets printed in setup(). That would have made it clear.

Maybe try that? Print the value in setup(), with different text so it's obvious that print came from setup().

tried on wokwi

works as expected

bool someGlobalVar = false;

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("loop");
  Serial.print("someGlobalVar: "); Serial.println(someGlobalVar);

  if (someGlobalVar == false) {
    Serial.println("switching...");
    someGlobalVar = true;
  } else {
    Serial.println("not switching...");
  }

  delay(1000);
}
  • What difference does printing from setup make? loop is already printing :smirk:? Anyways I tried adding print in setup, the statement doesn't show up in Serial Monoitor.
  • Changed rate to 115200, makes no difference.
  • Added delay(1000) in setup, makes no difference.
  • Board is Seeed XIAO RP2040. I tried in the emulator, indeed works as expected. Can the board be a problem? Can anyone try on a board please?
  • Tried adding multiple delays in loop, doesn't work.

Now we are getting somewhere
My bet is that you are printing before the Serial interface has time to initialise

I have no experience of that board but try adding

while (!Serial);

in setup() after the Serial.begin(). It will stop the code proceeding until the Serial interface is available

Try putting the following in setup after the Serial.begin()

while (!Serial); //wait for USB-to-Serial connection to be established

Alternatively, if there is a reset button on the board, try pressing that and see if you get the correct output in the Serial monitor. Some boards do not properly reset when the USB connection is established.

Many types of Arduino get automatically reset when you open serial monitor, so what you see are the very first things printed after it starts up. But not all types of Arduino do that.

For example, when I use a usb-serial adapter to upload code to an esp-01 module, I have to reset the chip and put it it programming mode by pressing pushbuttons, before I click Upload in the IDE. Then I have to press the reset button after upload has completed to get the code to run. When I open serial monitor, it does not automatically reset the chip, so the text I see on serial monitor is not the first text to be printed.

Ah-ha! That's the reason for what you see. The text you expected to see is being printed, but serial monitor does not catch it for some reason.

Thanks everyone for the prompt suggestions.

I have added serial statements just to simplify the usecase. The actual code does not use Serial, but does a one time mouse movement based on value of a global variable isMouseInit. But the sequence is never executed.

Actual code:

#include "Mouse.h"

bool isMouseInit = false;

void setup() {
  Mouse.begin();
}

void loop() {
  if (!isMouseInit) {
    Mouse.move(50, 50);
    isMouseInit = !isMouseInit;
  }

  delay(1000);
}
  • Expected: mouse should move in the 1st iteration
  • Observed: mouse doesn't move

Sounds like a much better approach than to use (unpredictable) delays. Is the any way to test the Initialization status of Mouse?