I am trying to send data between two arduinos , but all I get at the receiver side is garbage values.How should I go about doing this? These are the codes I used.
TRANSMITTER:
String sent = "I am 19 years old";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print(sent);
}
RECEIVER:
String recieve;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
recieve = Serial.read();
Serial.println(recieve);
delay(1);
}
NOTE: I have already used readString() function , but it didn't work.
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. The system in the 3rd example will be the most reliable.
Try this for sending
Serial.println("<hello world>)"
delay(1000);
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.
recieve = Serial.read();
Note that Serial.read() just reads a single character
Also note that loop() repeats incredibly fast. You need to include a delay() or you will overwhelm the serial system.