gpt4 book ai didi

arduino - 陷入 Arduino IDE 中的 for 循环

转载 作者:行者123 更新时间:2023-12-04 03:46:07 25 4
gpt4 key购买 nike

我正在使用 Arduino 来查找 I2C 地址,在进行到一半时出现了这个新故障,我不知道是 IDE 的问题还是我疯了。

我知道其中大部分内容可能并不重要,但我不知道发生了什么,所以这是我的整个循环。

void loop(){

byte error, address;
int nDevices;

Serial.println("Scanning...");

nDevices = 0;
for(address = 0; address <= 255; address++ )
{

Wire.beginTransmission(address);

error = Wire.endTransmission();
Serial.print(address);
Serial.print("|");
Serial.println(error);
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address, HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknow error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
delay(200);
//Serial.println(address);
}
delay(150);
Serial.println("Exiting");

if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");

delay(30000);

exit(0);
}

正如您在图片中看到的,我将 for 循环返回到 address=0 中,而没有在 loop() 中在其之后或之前打印任何内容。为什么会发生这种情况?

Look at the output in the Serial

最佳答案

我确信这与您将 address 声明为一个字节有关,该字节最多可以是整数 255。会发生的情况是,如果将 1 添加到字节值 255,它会再次循环到 0

address = 255并且for循环返回检查条件时会发生什么,255通过并且address++添加1address 所以现在 address = 0

https://www.arduino.cc/en/reference/byte

关于arduino - 陷入 Arduino IDE 中的 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43788240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com