pmcuser:
I think I am doing okay for slave that using dtostrf. but i am having problem to extract it in master side.
wire.read line gives me error "invalid conversion from “int” to “char*”.
Why it saying int? Would you please tell me commond lines to have float in x1? Thanks
With
char* S1;
you defined S1 as a pointer to a char. Which means S1 is now a integer-value that contains the adress of a char. (or it is a reserved space for an adress, but not yet assigned a value to it)
While this is sufficient to reference the start of a string variable, it is not enough as you have not defined the maximum size of the character string. (or else filling it with too large strings might cause buffer overruns) For that your would need to define it with:
char S1[9]=""; // reserve 8 characters for the float-string + 1 binary zero as termination marker. And make it empty.
The above reserves the space for the 8-character string variable, and assigns it an empty value.
Then with (your code)
...
S1 = Wire.read();
...
you assigned a byte value (the type that Wire.read() returns) to S1. A pointer to a memory location is larger in size than a single byte, so the compiler doesn’t allow this. It doesn’t match sizes.
Writing it as:
S1[0]= Wire.read();
would have compiled succesfully. But S1[1], pointing to the second character of the string, would still be susceptible for buffer overruns. (There are plenty of warnings though, about other code in other libraries. And defining S1 as char S1[9]=“”; would have solved those warnings.)
Use the above change only to see which individual bytes are sent from your slave program. (Atleast I think it will, as I don’t have my own slave sending it to test) It is not enough to get a propper float value into x1. But should help to learn what is needed further. The sent bytes need to be concatenated into a string somehow, one after the other. Now you are just replacing them one OVER the other.
My suggestion for void Passdata():
void passdata()
{
// make sure the following is already declared globally:
// char S1[9]="";
Wire.requestFrom(30, 8);
int index=0; // character position index;
while (Wire.available());
{
S1[index] = Wire.read();
Serial.print("Data byte received= ");
Serial.println(S1[index]);
index++; // increment character index
}
S1[9]=0; // place zero-termination marker in string to make sure.
Serial.print("Full string received: ");
Serial.println(S1);
x1 = atof(S1);
Serial.print("Full float (converted 8 characte string): ");
Serial.println(x1);
delay(200);
}
I hope this works as desired. As said, I can’t test the result.