-2

I'm using Arduino Uno in my project : One is a Master and the other is a Slave. I send Data from the Master to the Slave using I2C. I need to send float, but because I2C can only send char, so I'm obligated to transform float into String, then send character by character, and assemble them in the Slave.

The problem I had is, I'm declaring the variable (in the Slave) that contain the Float receiving and assumbling from the Master like a global variable, and I need to use it in my code in the slave, but my problem is that it is always printed as 0, and it dosn't give the right value of it.

The code I'm using 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=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

 }

I need to know how to have the result of the global variable "numOut" to use it in my code.

Thank you in advance.!!

too honest for this site
  • 12,050
  • 4
  • 30
  • 52

1 Answers1

-1

Did you actually check if the data received is correct ? I don't know about the Arduino's strings but if your toFloat() fails it might return 0.

Check through your serial port if your wordd variable is correct, and as already mentioned global variables should be declared as volatile. EDIT: volatile is necessary here to ensure your data is synchronized between your different function calls (otherwise your program might keep the value inside a register although the "real" value has been updated through an interrupt). However as someone said volatile is NOT necessary to every global variable, but you won't be using semaphores/mutex... on an Arduino so stick witch volatile for the problem you posted.

EDIT: https://www.arduino.cc/en/Reference/StringToFloat "If no valid conversion could be performed because the string doesn't start with a digit, a zero is returned."

lesurp
  • 343
  • 4
  • 19
  • "global variables should be declared as `volatile`" is plain wrong as an absolute statement. And this is not an answer, but more a comment. – too honest for this site Jun 06 '16 at 14:15
  • Sorry could not comment to his answer. And you kinda need to use volatile for global variables ; at least on an Arduino, otherwise I don't see how you can make sure your data synch'ed. Gonna edit my answer though – lesurp Jun 06 '16 at 14:22
  • Yes, to see if my variable is correct or not, I send it directly to Matlab: Serial.println(numOut,3); and it is correct. – Nour Haidar Jun 06 '16 at 14:36
  • And your ' wordd ' variable is correct when you print it ? The first character is a digit, you use a dot as a delimiter (and not a comma) etc. ? The only reason I see for your problem (besides a hardware issue) would be that your toFloat() function fails. – lesurp Jun 06 '16 at 14:48
  • @pLesur: There is a reason you need some reps to comment. Yet that is allowance to break the rules and missuse an answer. If you really think all static variables need `volatile` (be it Arduino or not), you really should learn what that qualifier means. `volatile` is **not** an alternative or replacement for synchronisation mechanisms, not even for atomic variables! This attitude is **very, very** dangerous! – too honest for this site Jun 06 '16 at 16:33
  • @pLesur The code is correct. And variables are printed on Matlab correctly, there is no doubt about the results of the variables, I just have a problem in printing them in my LCD Shield (OULIMEX Shield LCD 16x2) – Nour Haidar Jun 06 '16 at 21:49