Serial.println 仅在 loop() 内部有效

Serial.println works only inside of loop()

我的代码

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");
  }
}

所有打印的都是循环 1(无限期)。

编辑:按照@aMike

的建议添加了 !serial

有什么想法吗?

建立串行连接后添加一个短暂的延迟:

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