Variable Doesn't Increment First Time

So just getting started, and I wrote a program that almost does what I want:

int counter = 0;
int blinker = 1;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  // initialise built-in LED pin as output
  pinMode(13, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.print("Hello #");
  Serial.println(counter);
  counter += 1;
  Serial.println("incremented");
  // blink the LED
  for (int i = 0; i < blinker; i++) {
      digitalWrite(13, HIGH);
      delay(100);
      digitalWrite(13, LOW);
      delay(100);
  }
  blinker += 1;
  if (blinker > 5) {
    blinker = 0;
  }
  delay(1000);
}

It should print increasing values to the serial port, and flash the LED 1 through 5 times. And it does all that, exactly as I want, except the first time:

14:59:33.716 -> Hello #0
14:59:33.716 -> incremented
14:59:35.180 -> Hello #0
14:59:35.180 -> incremented
14:59:36.375 -> Hello #1
14:59:36.375 -> incremented
14:59:37.755 -> Hello #2
14:59:37.755 -> incremented
14:59:39.373 -> Hello #3

Maybe I'm just missing something obvious, but: why does it print 'Hello #0' twice?

i get the following output

Hello #0
incremented
Hello #1
incremented
Hello #2
incremented
Hello #3
incremented
Hello #4
incremented
Hello #5
incremented
Hello #6
incremented
Hello #7
incremented
Hello #8
incremented
Hello #9
incremented
Hello #10
incremented
Hello #11
incremented
Hello #12
incremented
Hello #13
incremented
Hello #14
incremented

1 Like

That is what I get, too.

For some Arduino boards there is a buffer that shows some previous text.
Therefor I sometimes do this:

void setup() 
{
  Serial.begin(9600);
  Serial.println();         // <- added
  Serial.println( "The sketch has started.");   // <- added

  pinMode(13, OUTPUT);
}

Also try pressing the reset button on the Arduino board. That will restart the Arduino board without that buffer interfering.

I assume that it will be fixed in some future Arduino IDE version.

I added those two lines, and now I get this, which amuses me:

16:45:46.958 -> The sketch has started.
16:45:46.958 -> Hello #0
16:45:46.958 -> incremented
16:45:48.427 -> 
16:45:48.427 -> The sketch has started.
16:45:48.464 -> Hello #0
16:45:48.464 -> incremented
16:45:49.734 -> Hello #1

Hitting the reset button does produce the expected output.

That is normal. It is either a buffer thing or perhaps there is a double reset, I'm not sure.

Ask a silly question, but have you clear the contents of the serial monitor everytime in all tests?

With the serial monitor open, remove the Arduino from the USB.
And press the clear button on the serial monitor to blank out the recent contents.
What text displayed when you plug the Arduino into USB again?

@gcjr, @groundFungus, and me all get correct output with your code, so there must be something unusual about your environment

What Arduino are you using. What ide version? Where are you printing?

In my environment (Mac Pro) a sketch on my UNO will run for about 180 milliseconds after upload and before Serial Monitor connects and cause a reset. I have taken to adding a 200 millisecond delay after Serial.begin().

Note: At 9600 baud the serial output generated before Serial Monitor connects and causes a reset is readable. At other baud rates the text before the reset shows up garbled.

That did it. Thanks.

I'm running on a Mac; 'about' says 'Arduino 1.8.15'.

The board itself says 'ARDUINO UNO R3'.

@johnwasser, also running on a Mac, gave an explanation and solution.

Thanks for everybody's help!

linux here.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.