Array/While results to variable

hey guys,

super easy question and it is doing my head in, how do I get the result out of while loop into a float (or is that wrong? ) variable so I can use it easily in the rest of my code for comparison? I can Serial.println(t) the result I want from (t), but for the life of me cannot make (t) = a declared variable :frowning:

int ReadVoltage_Pack1() {
  Wire.requestFrom(8, 5);    // request 5 bytes from slave device #8
  //gathers data coming from slave
  int i = 0; //counter for each bite as it arrives
  while (Wire.available()) {
    t[i] = Wire.read(); // every character that arrives it put in order in the empty array "t"
    i = i + 1;
  }

  Serial.println(t);   //shows the data in the array t

}

thanks!

if t is a char array once you have the string you could use atof() to convert it to a float

Don't forget to terminate the character array so that it becomes a C style string (lowercase S)

    t[i] = Wire.read(); // every character that arrives it put in order in the empty array "t"
    i = i + 1;
    t[i] = '\0';

but make sure that the t array has enough elements to accommodate the characters received plus the terminator

horace:
if t is a char array once you have the string you could use atof() to convert it to a float

awesome, thanks dude! That gets me where I wanted :slight_smile:

UKHeliBob:
Don't forget to terminate the character array so that it becomes a C style string (lowercase S)

    t[i] = Wire.read(); // every character that arrives it put in order in the empty array "t"

i = i + 1;
    t[i] = '\0';



but make sure that the t array has enough elements to accommodate the characters received plus the terminator

thanks for this, struggling to understand it but will see if I can figure out, re the terminate stuff.

For the enough elements, is this related to how many bytes/char the slave is sending? so you ensure the array is getting all the data properly?

For the enough elements, is this related to how many bytes/char the slave is sending? so you ensure the array is getting all the data properly?

Yes, if you are receiving say 10 bytes you need an array with 11 elements. You should declare the array large enough to receive the largest number of bytes that you will ever get