Arduino Serial.println仅在循环()内部工作

Arduino Serial.println仅在循环()内部工作,arduino,esp8266,arduino-esp8266,Arduino,Esp8266,Arduino Esp8266,我的代码 所有打印的内容都是Loop1(不精确) 编辑:添加!由@aMike建议的序列号 有什么想法吗?在建立串行连接后添加一个短延迟: void setup() { Serial.begin(115200); while (!Serial) {} Serial.println("Setup"); } int t = 0; void loop() { Serial.println("Loop1"); if (t==0){ t

我的代码

所有打印的内容都是Loop1(不精确)

编辑:添加!由@aMike建议的序列号


有什么想法吗?

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

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

为了让Loop2部分工作,尝试将
intt=0setup()
部分中的code>阅读串行文档,了解为什么需要在
setup
中等待串行设备准备就绪:这可能也解释了Loop1丢失的原因(如果真的丢失了)。@Spyre如果它在setup()中定义,则超出范围。@aMike我添加了!串行,但终端监视器没有区别读取数据表,您会理解的。正如您所说,延迟(500)是使其工作的原因。我甚至不需要添加while(!Serial)。当然,它可以工作,但是请理解,当您希望确保程序在执行其他操作之前等待通信建立时(换句话说,当您希望确保主机连接时),使用
while(!Serial)
非常有用。
void setup() {
  Serial.begin(115200);
  while (!Serial);
  delay(500); // this will ensure displaying your content on the serial monitor
Serial.println("Setup");
}