Search code examples
arduinoesp8266arduino-esp8266

Serial.println works only inside of loop()


My code

void setup() {
  Serial.begin(115200);
  while (!Serial) {}
  Serial.println("Setup");
}

int t = 0;

void loop() {
  Serial.println("Loop1");
  if (t==0){
    t = 1;
    Serial.println("Loop2");
  }
}

All that is printed is Loop1 (indefinetly).

Edit: added !serial as suggested by @aMike

Any idea?


Solution

  • Add a short delay after establishing the serial connection:

    void setup() {
      Serial.begin(115200);
      while (!Serial);
      delay(500); // this will ensure displaying your content on the serial monitor
    Serial.println("Setup");
    }