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

时间:2018-02-10 18:51:23

标签: arduino raspberry-pi i2c

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

  1. Raspberry向Arduino发送命令并阅读回复。
  2. Arduino向dali电源发送命令(打开或关闭)并获得响应。大约需要40毫秒。
  3. 在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);
    }
    

1 个答案:

答案 0 :(得分:0)

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

相关问题