HI all,
i’ve one arduino nano master reader and 2 arduino slave sender.
The slave 1 work fine but slave 2 don’t work, i will want the strings slave 1 + strings slave 2 in arduino master reader.
Please can you help me ?
Sorry for my english…
Alfred
Arduino Master code reader
// Arduino Maitre qui reçoit IDE 1.8.0 mini
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop() {
Wire.requestFrom(2, 27); // Demande 26 octets à l'esclave #2
String ChaineComplete, Chaine1, Chaine2, Chaine3;
do // L'esclave peut envoyer moins que demandé
{
char c = Wire.read(); // Recevoir un octet en tant que caractere
ChaineComplete = ChaineComplete + c; // 32.12345,-106.123460,3458,0 Longueur 27 caracters y compris la virgule
// Scinder les valeurs sans les virgules
Chaine1 = ChaineComplete.substring(0, 8); // Scinder la chaine de 0 a 8 la virgule est a 9
Chaine2 = ChaineComplete.substring(10, 19); // Scinder de 10 a 19 la virgule est a 20
Chaine3 = ChaineComplete.substring(21); // Scinder de 20 jusqu'a la fin de la chaine
} while (Wire.available());
// Serial.print("Reception des variables float en tant que chaine...");
// Serial.println();
// Serial.print("Chaine complete: "); // 32.12345,-106.123460,3458,0 soit 27 octets = 27 caracteres
// Serial.print(ChaineComplete);
// Serial.println();
//
// Serial.print("Chaine 1: " ); // 32.12345 soit 8 octets = 8 caracteres
Serial.print(Chaine1);
// Serial.println();
//
// Serial.print("Chaine 2: " ); // -106.123460 soit 11 octets = 11 caracteres
Serial.print(Chaine2);
// Serial.println();
//
// Serial.print("Chaine 3: " ); // 3458,0 soit 6 octets = 6 caracteres
// Serial.println(Chaine3);
//
// Serial.print("Float 4: " );
// Serial.println(Chaine3.toFloat()); // 3458.00
// Serial.print("int 5: " );
Serial.println(Chaine3.toInt()); // 3458
}
Arduino slave 1 sender
// Arduino Esclave 1 qui envois IDE 1.8.0 mini
#include<Wire.h>
float x_lat = 32.12345;
//float y_lng = -106.123456;
float y_lng = -16.1234;
//float xyz_lng = 3458.0;
float xyz_lng = 0.0;
// Ordre et nombre de colonnes des variables utilisés dans le tableau
char wy[0];
char ix[1];
char xyz[2];
const int potPin = A0;
void setup()
{
Wire.begin(1);
Wire.onRequest(Request);
pinMode(potPin, INPUT);
}
void loop()
{
xyz_lng = analogRead(potPin);
}
void Request()
{
// if (x_lat > y_lng)
// {
// Convertion des valeurs float en chaine et parametrage des valeurs a envoyer
// Format (float, bytes, numbers of numbers after the decimal, char variable)
dtostrf(x_lat, 8, 5, ix); // 32.12345 Longueur 8 caracters dont 5 caracteres apres le point chaine ix
Wire.write(ix); // Approx 8 octets = 8 caracteres
Wire.write(",");
dtostrf(y_lng, 11, 6, wy); // -106.123456 Longueur 11 caracters dont 6 caracteres apres le point chaine wy
Wire.write(wy); // Approx. 11 octets = 11 caracteres
Wire.write(",");
dtostrf(xyz_lng, 6, 1, xyz); // 3458,0 Longueur 6 caracters dont 1 caracteres apres le point chaine xyz
Wire.write(xyz); // Approx. 11 octets = 11 caracteres
Wire.write("\n"); // Nouvelle ligne de chaine
// }
// else
// {}
}
Arduino slave 2 sender
// Arduino Esclave 2 qui envois IDE 1.8.0 mini
#include<Wire.h>
float x_lat = 47.56954;
//float y_lng = -106.123456;
float y_lng = -55.7851;
//float xyz_lng = 3458.0;
float xyz_lng = 0.0;
// Ordre et nombre de colonnes des variables utilisés dans le tableau
char wy[0];
char ix[1];
char xyz[2];
const int potPin = A0;
void setup()
{
Wire.begin(2);
Wire.onRequest(Request);
pinMode(potPin, INPUT);
}
void loop()
{
xyz_lng = analogRead(potPin);
}
void Request()
{
// if (x_lat > y_lng)
// {
// Convertion des valeurs float en chaine et parametrage des valeurs a envoyer
// Format (float, bytes, numbers of numbers after the decimal, char variable)
dtostrf(x_lat, 8, 5, ix); // 32.12345 Longueur 8 caracters dont 5 caracteres apres le point chaine ix
Wire.write(ix); // Approx 8 octets = 8 caracteres
Wire.write(",");
dtostrf(y_lng, 11, 6, wy); // -106.123456 Longueur 11 caracters dont 6 caracteres apres le point chaine wy
Wire.write(wy); // Approx. 11 octets = 11 caracteres
Wire.write(",");
dtostrf(xyz_lng, 6, 1, xyz); // 3458,0 Longueur 6 caracters dont 1 caracteres apres le point chaine xyz
Wire.write(xyz); // Approx. 11 octets = 11 caracteres
Wire.write("\n"); // Nouvelle ligne de chaine
// }
// else
// {}
}