Hi, I need to communicate 3 slaves (Bluno Nano) with a master (Arduino Nano). I tried with I2C but somewhy it's fails (I can't send either request messages), so I tried with Serial communication.
The problem is that I can't read from master the correct character. I send just "-050" but the Master arduino just receive random characters. If I remove lines or delays, I can get the "-050", but the problem is that only works with one arduino.
If I add the same code for the second arduino, I will get randoms characters again.
This is my Slave sketch:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("-050");
delay(1000);
}
This is my working loop function on Master:
void loop() {
Slave1.flush();
Slave1.listen();
char buffer1[4] = "";
int pos1 = 0;
while(Slave1.available() && pos1 < 4) {
buffer1[pos1] = Slave1.read();
Serial.print(buffer1[pos1]);
pos1++;
}
StrToInt(buffer1, &RSSI1);
Serial.println(RSSI1);
}
This is my unworking loop function on Master:
void loop() {
Slave1.flush();
Slave1.listen();
char buffer1[4] = "";
int pos1 = 0;
while(Slave1.available() && pos1 < 4) {
buffer1[pos1] = Slave1.read();
Serial.print(buffer1[pos1]);
pos1++;
}
StrToInt(buffer1, &RSSI1);
Serial.println(RSSI1);
Slave2.flush();
Slave2.listen();
char buffer2[4] = "";
int pos2 = 0;
while(Slave2.available() && pos2 < 4) {
buffer2[pos2] = Slave2.read();
Serial.print(buffer2[pos2]);
pos2++;
}
StrToInt(buffer2, &RSSI2);
Serial.println(RSSI2);
}
And this is my actual complete sketch for Master:
#include <SoftwareSerial.h>
SoftwareSerial Slave1(11, 12);
SoftwareSerial Slave2(9, 10);
void setup() {
Serial.begin(9600);
Slave1.begin(9600);
Slave2.begin(9600);
}
int RSSI1 = 0;
int RSSI2 = 0;
void loop() {
Slave1.flush();
Slave1.listen();
char buffer1[4] = "";
int pos1 = 0;
while(Slave1.available() && pos1 < 4) {
buffer1[pos1] = Slave1.read();
Serial.print(buffer1[pos1]);
pos1++;
}
StrToInt(buffer1, &RSSI1);
Serial.println(RSSI1);
Slave2.flush();
Slave2.listen();
char buffer2[4] = "";
int pos2 = 0;
while(Slave2.available() && pos2 < 4) {
buffer2[pos2] = Slave2.read();
Serial.print(buffer2[pos2]);
pos2++;
}
StrToInt(buffer2, &RSSI2);
Serial.println(RSSI2);
}
void StrToInt(String input, int* output) {
int tmp = 0;
bool isNeg = false;
for(int i = 0; i < input.length(); i++) {
if(input [i] == '-') {
isNeg = true;
} else {
int num = input[i] - '0';
if(num >= 0 && num <= 10) {
if(tmp == 0) {
tmp = num;
} else {
tmp = (tmp * 10) + num;
}
}
}
}
if(isNeg)
tmp = tmp * -1;
if(tmp != 0)
*output = tmp;
}
The output from serial could be either random characters (either or not english/spanish characters) or just nothing.
Note: I did a research, but I think that I used the wrongs tags on google, because I didn't find any information about this. Also, I tried with baud rate of 300 and 115200, both with the same result.
Note2: I will be very grateful if you can help me with fixing the SW or I2C (Bluno Nano is an Arduino Uno, and has its SDA and SCL in A4 and A5. The Arduino nano has the same pins in the same order)