我正在尝试对 arduino nanobot 进行编程以避免出现黑线。ITR20001/T 传感器和 nano 使用 I2C 连接。问题是我的传感器似乎只有在我打开机器人后才立即工作,而传感器停止响应。
void loop() {
Wire.beginTransmission(STM8S003F3_IR_devAddr);
Wire.write(110);
Wire.endTransmission();
uint8_t STM8S003F3_IR_IIC_buff[8];
uint8_t a = 0;
static uint8_t cout = 0;
Wire.requestFrom(STM8S003F3_IR_devAddr, 8);
while (Wire.available()) // slave may send less than requested
{
STM8S003F3_IR_IIC_buff[a++] = Wire.read(); // receive a byte as character
}
if ((STM8S003F3_IR_IIC_buff[0] == 0XA0) && (STM8S003F3_IR_IIC_buff[7] == 0XB0)) {
TrackingData_R = (STM8S003F3_IR_IIC_buff[1] << 8) | (STM8S003F3_IR_IIC_buff[2]);
TrackingData_L = (STM8S003F3_IR_IIC_buff[3] << 8) | (STM8S003F3_IR_IIC_buff[4]);
TrackingData_M = (STM8S003F3_IR_IIC_buff[5] << 8) | (STM8S003F3_IR_IIC_buff[6]);
cout = 0;
if (TrackingData_R > 200 && TrackingData_R <= 860) {
left();
} else if (TrackingData_L > 200 && TrackingData_L <= 860) {
right();
} else if (TrackingData_M > 200 && TrackingData_M <= 860) {
around();
forward();
} else {
forward();
}
} else {
/* code */
//Serial.println("Contact Changhua :STM8S003F3_IR data error"); // print the character
cout += 1;
if (cout > 250) {
Wire.beginTransmission(STM8S003F3_IR_devAddr);
Wire.write(110);
Wire.endTransmission();
}
return;
}
}