【发布时间】:2020-03-26 13:40:10
【问题描述】:
已经阅读了很多帖子,但看不出为什么这行代码会产生错误。这个程序很简单——它使用 I2C 总线来允许从 MASTER Arduino UNO 到从 Arduino UNO 的通信。 下面的代码是MASTER的代码和定义,“#define ANSWERSIZE 5”是编译器不喜欢的那行。
下面的完整程序:
#include <Wire.h>
#define ANSWERSIZE 5 // <--- THIS LINE GENERATES THE ERROR
#define SLAVE_ADDR 9
void setup() {
Wire.begin();
Serial.begin(9600);
Serial.println("I2C MASTER Demonstration");
}
void loop() {
delay(50);
Serial.println("Write data to SLAVE");
Wire.beginTransmission(SLAVE_ADDR);
Wire.write(0);
Wire.endTransmission();
Serial.println("Receive data from SLAVE");
Wire.requestFrom(SLAVE_ADDR.ANSWERSIZE);
String response = "";
while (Wire.available()) {
char b = Wire.read();
response += b;
}
Serial.println(response);
}
【问题讨论】:
-
问题很可能出在 Wire.h
-
Wire.requestFrom(SLAVE_ADDR.ANSWERSIZE);...urks...requestFrom是否采用浮点值?为什么不使用常量而不是滥用预处理器? -
Wire.requestFrom(SLAVE_ADDR.ANSWERSIZE);等价于Wire.requestFrom(9.5);也许应该是逗号,? -
@JohnFileau 不太...
-
@idclev463035818 我今天学到了一些关于
#define的东西
标签: c++ arduino arduino-uno