全局变量arduino [英] Global variable arduino

查看:36
本文介绍了全局变量arduino的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 I2C 将 Arduino 主控与 4 个从属 Arduino 通信,并在每个 Arduino 从属设备上连接一个 Shield (OULIMEX Shield LCD 16x2).我使用 I2C 将数据从主机发送到从机.所以我在 master 中使用了这段代码:

I'm using I2C to communicate a Master Arduino to 4 Slaves Arduinos, and an Shield (OULIMEX Shield LCD 16x2) on every Arduino slave. I send Data from the master to slaves using I2C. So I use this code in the master :

#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]!='\0'; ++i);    // length of the string

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

}

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

I wanted the Data to be printed on the Shield, but I'm connecting all slaves with the same way with the master. For that I have two problems :

1- 我用来打印值的全局数据总是打印为 0,而不是给出实际值;

1- The global data I'm using to print value is always printed as 0, and not giving the real value;

2- 所有 Shield 都打印相同的内容:例如,我在第一个 Shield 中打印hello",在第二个 Shield 中打印hi",但是 both 打印相同的内容(hello 或 hi).

2- All Shields print the same thing : For exemple, I print "hello" in the first Shield, and I print "hi" in the second Shield, but bouth are printing the same thing (hello or hi).

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

The code using for the first slave is :

#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
}

提前谢谢你!!

推荐答案

这个问题我已经问过了,但我想我已经有了答案.必须在 void 设置和 void 循环之前声明一个全局变量,如下所示:

I already ask this question but I think I have the answer of it. A global variable have to be diclared befor the void setup, and the void loop too, like that :

type YourGlobalVariable;

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

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

So, it is exactly how I did already. The reason it didn't work for me, it was cause of I used this function :

  void receiveEvent(int howMany) {}

我真的不知道它的哪些属性让它不适用于全局变量,但它就像我已经说过的那样工作.

I don't really know what are the properties of it that let it not work for a global variables, but It works like I sayd already.

谢谢大家

这篇关于全局变量arduino的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆