全局变量arduino
我使用I2C将一个主Arduino与4个从Arduinos通信,并在每个Arduino从机上安装一个Shield OULIMEX Shield LCD 16x2。 我使用I2C将数据从主设备发送到从设备。因此,我在主程序中使用以下代码:全局变量arduino,arduino,serial-port,global-variables,arduino-uno,i2c,Arduino,Serial Port,Global Variables,Arduino Uno,I2c,我使用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
#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
}
}
我想把数据打印在盾牌上,但我用同样的方式连接所有的奴隶和主奴隶。为此,我有两个问题:
1-我用于打印值的全局数据始终打印为0,而不提供实际值
2-所有的盾牌都打印相同的东西:例如,我在第一个盾牌中打印hello,在第二个盾牌中打印hi,但是bouth正在打印相同的东西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
}
提前谢谢你 我认为这是因为主盾中的程序结构不好 此块选择从属,但在第一行您已经选择了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]!='\0'; ++i); // length of the string
最后写下字节的序号
然后是整根绳子
因为++i,所以应该得到0或1
如果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]!='\0'; ++i) // length of the string
Wire.write(a[i]); // write one byte
Wire.endTransmission(); // stop transmitting
M++;
if (M==5) M=0;
}
让我知道这是否有效。我认为这是由于主屏蔽中的程序结构不好造成的 此块选择从属,但在第一行您已经选择了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]!='\0'; ++i); // length of the string
最后写下字节的序号
然后是整根绳子
因为++i,所以应该得到0或1
如果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]!='\0'; ++i) // length of the string
Wire.write(a[i]); // write one byte
Wire.endTransmission(); // stop transmitting
M++;
if (M==5) M=0;
}
让我知道这是否有效。我已经问过这个问题,但我想我已经找到了答案。在进行void设置之前,必须对全局变量进行dicaled,void循环也必须如此:
type YourGlobalVariable;
void setup()
{
}
void loop()
{
}
所以,我已经做到了。它不适用于我的原因是我使用了此功能:
void receiveEvent(int howMany) {}
我真的不知道它的什么属性让它不适用于全局变量,但它已经像我说的那样工作了
谢谢大家我已经问了这个问题,但我想我已经找到了答案。在进行void设置之前,必须对全局变量进行dicaled,void循环也必须如此:
type YourGlobalVariable;
void setup()
{
}
void loop()
{
}
所以,我已经做到了。它不适用于我的原因是我使用了此功能:
void receiveEvent(int howMany) {}
我真的不知道它的什么属性让它不适用于全局变量,但它已经像我说的那样工作了
谢谢大家谢谢您提供的详细信息。你说得对,我再加一条线;。然而,该程序运行良好,将数据从主程序发送到从程序,我正在使用串行端口将从程序上的数据打印到Matlab,它看起来是正确的。因此,我认为在主机上编写代码没有问题,但只有当我尝试在LCD上打印代码时,才会出现问题。我正在发送字符串的长度。writei;要在从属int x=Wire.read;中包含它;。谢谢你的任何其他建议或问题谢谢你的详细信息。你说得对,我再加一条线;。然而,该程序运行良好,将数据从主程序发送到从程序,我正在使用串行端口将从程序上的数据打印到Matlab,它看起来是正确的。因此,我认为在主机上编写代码没有问题,但只有当我尝试在LCD上打印代码时,才会出现问题。我正在发送字符串的长度。writei;要在从属int x=Wire.read;中包含它;。谢谢你的任何其他建议或问题