【发布时间】:2018-02-10 18:51:22
【问题描述】:
Raspberry Pi 正在通过 I2C 从 Arduino 发送命令和读取数据。 一切都是这样完成的:
- Raspberry 向 Arduino 发送命令并读取响应。
- Arduino 向大理电源发送命令(开或关)并得到响应。大约需要 40 毫秒。
在 Raspberry Pi 上,它没有得到更新的响应。
如何等到loop() 读完数据?然后发送到树莓派?我等不及了,因为某些命令应该在不到 100 毫秒的时间内重复。
这是我的代码:
#include <Wire.h>
#include "dali.h"
volatile bool state = false;
volatile uint8_t response = 0;
volatile bool newCommand = false;
void setup()
{
dali.Init(A0, 7);
dali.busTest();
dali.transmit(BROADCAST, RECALL_MIN_LEVEL);
Wire.onReceive(ReceiveEvent);
Wire.onRequest(RequestEvent);
Wire.begin(10);
}
void loop()
{
if (newCommand)
{
Serial.println(state);
response = dali.GetData(state);
Serial.println(response);
newCommand = false;
}
}
void ReceiveEvent(int howMany)
{
byte req;
while (Wire.available())
{
req = Wire.read();
}
if (req == 97)
{
state = !state;
newCommand = true;
}
}
void RequestEvent()
{
Wire.write(response);
}
【问题讨论】:
标签: arduino raspberry-pi i2c