내 프로젝트에서 Arduino Uno를 사용하고 있습니다. 하나는 마스터이고 다른 하나는 슬레이브입니다. I2C를 사용하여 마스터에서 슬레이브로 데이터를 보냅니다. 나는 float을 보낼 필요가 있지만, I2C는 char 만 보낼 수 있기 때문에 float을 String으로 변환 한 다음 문자로 문자를 보내고 Slave에서 조합해야합니다.I2C 및 LCD를 사용하는 arduino 전역 변수
내가 가진 문제는 전역 변수처럼 Float 수신 및 assumbling을 포함하는 변수 (슬레이브에서)를 선언하고 있으며이를 슬레이브의 코드에서 사용해야합니다. 내 문제는 항상 0으로 인쇄됩니다, 그리고 그것을 dosn't 올바른 값을 제공합니다.
내가 사용하는 코드는 다음과 같습니다
#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=0; // The Global Variable
int comp=1 ;
String wordd = "";
void setup()
{
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
lcd.lcdGoToXY(1,1);
lcd.lcdClear();
lcd.lcdWrite("EG ");
lcd.lcdGoToXY(7,1);
lcd.lcdWrite(numOut,3);
}
void loop()
{
}
// 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
}
나는 내 코드에서 사용할 수있는 전역 변수 "numOut"의 결과를 가지고하는 방법을 알고 있어야합니다.
미리 감사드립니다. !!
'float numOut = 0;'을'volatile float numOut = 0;'으로 변경하고'String wordd = "";'를'volatile String wordd = "";'로 변경하여 부적절하게 최적화되지 않도록하십시오. – MikeCAT
너무 효과적이지 않습니다./!! –
[Global variable arduino] (http://stackoverflow.com/questions/37648796/global-variable-arduino)의 가능한 복제본 ** 질문을 다시 게시하지 마십시오! ** – Olaf