IDE Broken? Serial messing with Counter++

I think I found a bug in the Arduno IDE.

When I disable Serlial or remove serial from the lines with a comment, my increment counter completly stops working. Even if I serial.println something like a blank space or random character, it HAS to be there or increments stop working. Ive tried removing serial.begin and changing from bytes to ints with no luck. VS Code compiles and runs just fine, not the Arduino IDE. Im 99% sure its not my code. I want to remove serial since im done debugging. What on earth is going on here? Help appreciated!

#include<Arduino.h>

const byte button = 2;
byte buttonstate = 0;
byte counter = 0;
byte doubleTapSleep = 0;
byte doubleTapState = 0;
byte led = 5;
byte DevicePower = 0;

void setup() {
 
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  pinMode(button, INPUT);
  buttonstate = digitalRead(button);
  
}

void loop() {
  
  buttonstate = digitalRead(button);
  doubleTapSleep = 0;
  doubleTapState = 0;
  counter = 0;
  
  Serial.println("MAIN LOOP");
  
  while (buttonstate == LOW && counter < 250) {
    doubleTapState = 1;
    buttonstate = digitalRead(button);

    if (counter > 248 && DevicePower == 0) {
      //Serial.print("on");
      counter = 0;
      PowerUp();
    }

    if (counter > 248 && DevicePower == 1) {
      //Serial.print(doubleTapSleep);
      counter = 0;
      PowerDown();
      }
      
    counter++;
 [b]   Serial.println(counter); /////!!!!!!!!!!!!!!This has to be here or counter++ breaks[/b]
  }

  while (doubleTapState == 1) {
    buttonstate = digitalRead(button);
    doubleTapSleep++;[b]
    Serial.println(doubleTapSleep); ///////////////////!!!!!!!!!!!!!!!!!!!!!!!!This also has to be here or doubleTapSleep++ breaks[/b]

    if (buttonstate == LOW) {
      doubleTapState = 2;
      BatteryReport();
    }
    
    if (doubleTapSleep > 30) {
      doubleTapState = 0;
    }
  }
}





//Power Sequences

int BatteryReport() {
  analogWrite(led, 255);
  delay(100);
  analogWrite(led, 10);
  delay(100);
  analogWrite(led, 255);
  delay(100);
  analogWrite(led, 10);
  delay(100);
  analogWrite(led, 255);
  delay(100);
  analogWrite(led, 0);
}

int PowerUp() {
  analogWrite(led, 10);
  delay(70);
  analogWrite(led, 50);
  delay(70);
  analogWrite(led, 100);
  delay(70);
  analogWrite(led, 150);
  delay(70);
  analogWrite(led, 200);
  delay(70);
  analogWrite(led, 255);
  delay(1000);
  DevicePower = 1;
  //PSU pullup
}

int PowerDown() {
  analogWrite(led, 255);
  delay(70);
  analogWrite(led, 200);
  delay(70);
  analogWrite(led, 150);
  delay(70);
  analogWrite(led, 100);
  delay(70);
  analogWrite(led, 50);
  delay(70);
  analogWrite(led, 10);
  delay(1000);
  analogWrite(led, 0);
 
  DevicePower = 0;
  //psu pullup
  //low power mode
}

What happens if you replace the Serial.println() with a short delay?:

delay(1);

Serial.println() acts as a delay. If your code is dependent on that delay, removing Serial.println() will cause your code to work differently.

without the prints, holding the button down causes the LED to repeatedly fade in and fade out.

of course it takes practically no time for the the counter to increment to ~250 without any delays. (Serial.print takes some time)

pert:
What happens if you replace the Serial.println() with a short delay?:

delay(1);

Serial.println() acts as a delay. If your code is dependent on that delay, removing Serial.println() will cause your code to work differently.

Another way to test this is to change the baud rate from 9600 to something like 115200 to see if that is enough to make your sketch act differently.

mmimadi:
I think I found a bug in the Arduno IDE.

When I disable Serlial or remove serial from the lines with a comment, my increment counter completly stops working.

That could not possibly be a bug in the IDE because when you disable Serial the IDE has no involvement with your program.

The problem must be within your program.

...R