good day
I have a problem with this code, I tried for differents ways but don´t work
help me please
CODE MASTER
#include <Wire.h>
void setup() {
Wire.begin(D1,D2);
Serial.begin(9600); // start serial for output
}
void loop() {
Wire.requestFrom(2, 20); // request 20 bytes from slave device #2
String string, string1, string2;
do // slave may send less than requested
{
char c = Wire.read(); // receive a byte as character
string = string + c; //Keep saving whatever is comming
string1 = string.substring(0, 8); //slpit String from 0 to 8
string2 = string.substring(9); // Split from 9 to the End of String
} while (Wire.available());
Serial.print("Recieving float as String...");
Serial.println();
Serial.println();
Serial.print("Full String: ");
Serial.print(string);
Serial.println();
Serial.print(" string 1: " );
Serial.print(string1);
Serial.println();
Serial.print(" string 2: " );
Serial.print(string2);
Serial.println("");
delay(1000);
Serial.println("");
Serial.println("");
}
CODE SLAVE
#include<Wire.h>
float x_lat = 32.12345;
float y_lng = -106.123456;
char wy[7];
char ix[8];
void setup() {
Wire.begin(2);
Wire.onRequest(Request);
}
void loop() {
}
void Request()
{
if (x_lat > y_lng)
{
//Converting float value to char
//The format (float, bytes, numbers of numbers after the decimal, char variable)
dtostrf(x_lat, 8, 5, ix);
Wire.write(ix); // appx 8 bytes
Serial.print(ix);
Wire.write(",");
Serial.print(",");
dtostrf(y_lng, 11, 6, wy);
delay(1000);
Wire.write(wy); // appx 11 bytes
Serial.print(wy);
Wire.write("\n");
Serial.println();
}
else
{}
}
and I recive in a master port this
""
Recieving float as String...
Full String: ⸮
string 1: ⸮
string 2:
""
thanks for your help