全局变量arduino

Global variable arduino

我正在使用 I2C 将主 Arduino 与 4 个从属 Arduinos 通信,并在每个 Arduino 从属设备上使用 Shield(OULIMEX Shield LCD 16x2)。 我使用 I2C 将数据从主机发送到从机。所以我在主人中使用这段代码:

#include <Wire.h>
#include <math.h>
#include <floatToString.h>

double incomingData;

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  incomingData = Serial.parseFloat();    //read incoming data
}

void loop()
{
  delay (1000);

  if (Serial.available())
  {

    incomingData = Serial.parseFloat(); //read incoming data
    Wire.beginTransmission(8);    // transmit to device #8 



    if ((M==0) || (M==1) || (M==2))
      Wire.beginTransmission(8);    // transmit to device #8 *****************************************************************

    else
      Wire.beginTransmission(7);    // transmit to device #7 *****************************************************************     
    M++;
    if (M==5)
      M=0;

    String a = ""; 
    a = floatToString(test,incomingData,3,5,true);
    for(i=0; a[i]!='[=10=]'; ++i);    // length of the string

    Wire.write(i);
    Wire.write(floatToString(test,incomingData,3,5,true));      //  sends one byte
    Wire.endTransmission();    //    stop transmitting
    }

}

我希望将数据打印在盾牌上,但我正在以相同的方式将所有从站与主站连接起来。为此,我有两个问题:

1- 我用来打印值的全局数据总是打印为 0,并没有给出真实值;

2- 所有盾牌都打印相同的东西:例如,我在第一个盾牌上打印 "hello",在第二个盾牌上打印 "hi",但两者都打印相同的东西(你好或嗨)。

第一个从机使用的代码是:

#include <LCD16x2.h>
#include <Wire.h>

LCD16x2 lcd;

int buttons;

int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor

float numOut;
int comp=1 ;
String wordd = "";
int M =0;

void setup()
{

  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output  

}

void loop()
{
 delay(500);
}   
 // function that executes whenever data is received from master
 // this function is registered as an event, see setup()
  void receiveEvent(int howMany) {
  wordd = "";
  int x = Wire.read();
  for (int i=0; i<=x; i++)
  {
    char c = Wire.read();
    wordd += c;
  }
  numOut = wordd.toFloat();
  Serial.println(numOut,3);         // print the integer
}

提前致谢!!

我觉得是master shield的程序结构不好

这个块 select 是从属但在第一行你已经 select #8 我认为这让奴隶们感到困惑。

Wire.beginTransmission(8);    // transmit to device #8 

    if ((M==0) || (M==1) || (M==2))
      Wire.beginTransmission(8);    // transmit to device #8 
    else
      Wire.beginTransmission(7);    // transmit to device #7 

这个块应该在函数的末尾

M++;
if (M==5)
  M=0;

然后你解析字符串中的值。 但是省略第一个字符,因为你写的是 ++i 而不是 i++

此外,您使用 ; 关闭循环所以它什么都不做

String a = ""; 
a = floatToString(test,incomingData,3,5,true);
for(i=0; a[i]!='[=12=]'; ++i);    // length of the string

最后你写字节的序号 然后又是整个字符串

所以你应该得到“0”(或“1”,因为 ++i) 如果 Wire.write() 支持,则后跟您的电话号码

Wire.write(i);
Wire.write(floatToString(test,incomingData,3,5,true));      //  sends one byte
Wire.endTransmission();    //    stop transmitting
}

你的草图应该是:

if (Serial.available())
    {
    incomingData = Serial.parseFloat(); //read incoming data

    String a = ""; 
    a = floatToString(test,incomingData,3,5,true);

    if ((M==0) || (M==1) || (M==2))
        Wire.beginTransmission(8);    // transmit to device #8 
    else
        Wire.beginTransmission(7);    // transmit to device #7 

    for(i=0; a[i]!='[=14=]'; ++i)    // length of the string
        Wire.write(a[i]);        // write one byte


    Wire.endTransmission();    //    stop transmitting

    M++;
    if (M==5) M=0;
    }

让我知道这是否有效。

我已经问过这个问题,但我想我已经找到了答案。在 void 设置之前必须声明一个全局变量,void 循环也是如此,就像这样:

type YourGlobalVariable;

void setup()
{
}
void loop()
{
}

所以,这正是我已经做过的。它对我不起作用的原因是我使用了这个功能:

  void receiveEvent(int howMany) {}

我真的不知道它的哪些属性使其不适用于全局变量,但它像我说的那样工作。

谢谢大家