Raspberry Pi master,Arduino I2C slave。将数据从其他服务返回到主Raspberry Pi

问题描述 投票:0回答:1

Raspberry Pi通过I2C发送命令并从Arduino读取数据。一切都是这样完成的:

  1. Raspberry向Arduino发送命令并阅读回复。
  2. Arduino向dali电源发送命令(打开或关闭)并获得响应。大约需要40毫秒。

在Raspberry Pi上,它没有获得更新的响应。如何等到loop()读完数据?然后发送到Raspberry Pi?我等不及了,因为有些命令应该在不到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
1个回答
0
投票

您没有将Serial.begin()添加到设置中,然后尝试在循环中调用Serial.print(),这可能会冻结您的Arduino。

© www.soinside.com 2019 - 2024. All rights reserved.