首页 > 解决方案 > Serial.println 仅在 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");
  }
}

打印的所有内容都是 Loop1(不确定地)。

编辑:按照@aMike 的建议添加了 !serial

任何的想法?

标签: arduinoesp8266arduino-esp8266

解决方案


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

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

推荐阅读