Hi!
I have a little problem receiving data over serial.
I never used the Serial.read() function before so i made a simple circuit in Tinkercad to test this for my use.
The circuit is just 2 Arduino Uno's:
GND -> GND
TX -> D10
RX -> D11
Transmitter:
void setup()
{
Serial.begin(9600);
pinMode(12, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
Serial.println("§1.1$");
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(1000); // Wait for 1000 millisecond(s)
}
Receiver:
#include <SoftwareSerial.h>
boolean espRead = false;
boolean stringReady = false;
String readString;
String charString;
SoftwareSerial ESPSerial(10,11);
void setup()
{
Serial.begin(19200);
pinMode(LED_BUILTIN, OUTPUT);
ESPSerial.begin(9600);
}
void loop(){
while (ESPSerial.available()){
delay(10);
if (ESPSerial.available()>0){
char c = ESPSerial.read();
charString = c;
//Serial.println(charString); // TEST
if (charString == "$"){
espRead = false;
stringReady = true;
}else if (charString == "§"){
espRead = true;
readString = "";
}
if (espRead == true){
readString += c;
}
}
}
if (stringReady == true){
Serial.println(readString);
stringReady = false;
}
}
The Transmitter prints "§1.1$" every 2 seconds to serial monitor. If i run the code, it's printing a "A" with cedil on top befor the expected signs. Where did this come from? ^^
2)
The Receiver should read the incomming signs so i can work with it. But the serial monitor dont't shows anything. If i uncomment the "TEST" line in Receiver code it just shows me strange signs but not the expected "§1.1$".
The §$ signs are just for recognizing start and end of the sended code. The point is also just vor seperating.
I also tried it an other way with Serial.readString or Serial.readStringUntil but can't get it to work.
Please, can anyone help me "repairing" my code? I don't want a copy-paste-answer because i want to learn how to use the Serial.read function.
Thank you all and a happy new year.