Hello,
I am not a native english speaker and a complete beginner in regards to arduino, so please understand any weird phrasings.
I am trying to send data from an Arduino Nano Clone to an Arduino Uno Clone .
So the next step was to send the data using SoftwareSerial, but it didn't work as planned:
example Serial Monitor for Nano to Uno:
Transmitter:
-> Sent data: 23.34
-> 2334
-> 1E
-> 9
Receiver:
-> Received data: 7168
-> 71.68
-> 0
-> 1C
-> Received data: 57344
-> 573.44
-> 0
-> E0
When I use the same code with two Arduino Nano clones (same type), everything works as intended.
Which Nano am I using?
LGT-RF-Nano (using MiniCore Board Manager / Atmega328)
Which Uno am I using?
Seems to be the original one from Arduino itself.
Assumption: Problem narrowed down
Nano clone - works with a baud rate of 9600 in the code and 2400 in the serial monitor
Uno clone - works with a baud rate of 9600 in both the code and the serial monitor
I know that SoftwareSerial usually requires the same baud rate on both devices, but I do not know how to proceed from this point on...
Transmitter:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
double dataToSend = 23.34;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() {
// Break down the int into low and high bytes
uint16_t dataToSend1 = dataToSend* 100;
uint8_t highByte = (dataToSend1 >> 8) & 0xFF; // Extract high byte
uint8_t lowByte = dataToSend1 & 0xFF; // Extract low byte
// Send the low and high bytes over SoftwareSerial
//mySerial.write(&highByte, sizeof(highByte));
//mySerial.write(&lowByte, sizeof(lowByte));
mySerial.write(highByte);
mySerial.write(lowByte);
Serial.print("Sent data: ");
Serial.println(dataToSend);
Serial.println(dataToSend1);
Serial.println(lowByte, HEX);
Serial.println(highByte, HEX);
delay(1000);
}
Receiver:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() {
if (mySerial.available() >= 2) { // Ensure at least 2 bytes are available
// Read low and high bytes
uint8_t highByte = mySerial.read();
while (!mySerial.available()) {}
uint8_t lowByte = mySerial.read();
// Combine bytes into an int
uint16_t receivedData2 = ((uint16_t)highByte << 8) | lowByte;
// Convert the integer back to the original float value
double receivedData1 = receivedData2 / 100.0;
Serial.print("Received data: ");
Serial.println(receivedData2);
Serial.println(receivedData1);
Serial.println(lowByte, HEX);
Serial.println(highByte, HEX);
// delay(1000);
}
}
If anything is missing, please tell me.