全局变量arduino [英] Global variable arduino
问题描述
我使用I2C在每个Arduino从属设备上将Master Arduino与4个Slaves Arduinos和Shield(OULIMEX Shield LCD 16x2)进行通信。
我使用I2C将数据从主机发送到从机。所以我在master中使用这个代码:
#include< Wire.h>
#include< math.h>
#include< floatToString.h>
double incomingData;
void setup()
{
Wire.begin();
Serial.begin(9600);
incomingData = Serial.parseFloat(); //读入数据
}
void loop()
{
delay(1000);
if(Serial.available())
{
incomingData = Serial.parseFloat(); //读入数据
Wire.beginTransmission(8); ((M == 0)||(M == 1)||(M == 2))$ b发送到设备#8
$ b Wire.beginTransmission(8); //发送到设备#8 ******************************************* **********************
else
Wire.beginTransmission(7); //传输到设备#7 ******************************************* **********************
M ++;
if(M == 5)
M = 0;
String a =;
a = floatToString(test,incomingData,3,5,true);
for(i = 0; a [i]!='\0'; ++ i); //字符串的长度
Wire.write(i);
Wire.write(floatToString(test,incomingData,3,5,true)); //发送一个字节
Wire.endTransmission(); //停止发送
}
}
我想要要在盾牌上打印的数据,但是我要用与主人相同的方式连接所有的奴隶。为此,我有两个问题:
1-我用来打印值的全局数据总是打印为0,而不是给出实际值;
2-所有Shields打印相同的东西:例如,我在第一个盾中打印hello,并在第二个盾中打印hi,但bouth正在打印同样的事情(你好或嗨)。
使用第一个奴隶的代码是:
#include< LCD16x2.h>
#include< Wire.h>
LCD16x2 lcd;
int按钮;
int sensorPin = A0; //选择电位器的输入引脚
int sensorValue = 0; //变量来存储来自传感器的值
float numOut;
int comp = 1;
String wordd =;
int M = 0;
void setup()
{
Wire.begin(8); //加入地址为#8
的i2c总线Wire.onReceive(receiveEvent); //注册事件
Serial.begin(9600); //开始序列输出
}
void loop()
{
delay(500);
}
//当从master接收到数据时执行的函数
//此函数注册为事件,请参阅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); //打印整数
}
提前致谢!!
我已经问过这个问题,但我想我已经得到了答案。一个全局变量必须用void来设置,void loop也是如此:
类型YourGlobalVariable;
void setup()
{
}
void loop()
{
}
所以,这正是我已经做过的。它不适合我的原因,这是我使用这个函数的原因:
void receiveEvent(int howMany){ }
我不知道它的属性是什么让它不能用于全局变量,但它的工作原理就像我说的那样。
谢谢大家
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
}
}
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- The global data I'm using to print value is always printed as 0, and not giving the real value;
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
}
Thank you in advance !!
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.
Thank you all
这篇关于全局变量arduino的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!