Arduino Asked by Enes Orhan on February 7, 2021
I get two 10-bit values from analog pins. These two values in a string, ";" combine with.
for ex: 515; 510
I am sending it to another arduino via Serial.print. I cannot display this string expression on other arduino’s serial monitor. But when I press the reset button of Coordinator arduino, there are a few values. Can you help me?
My Coordinator Code:
#include <SoftwareSerial.h>
#define xBeeRxPin 10
#define xBeeTxPin 9
int deger3;
int deger4;
SoftwareSerial xBeeSerial(xBeeRxPin, xBeeTxPin);
void setup() {
//Serial.begin(9600);
xBeeSerial.begin(38400);
}void loop() {
deger3 = analogRead(A0);
deger4 = analogRead(A2);
String deger5 = " " + (String)deger3 + ";" + (String)deger4 ;
Serial.println("string ifade: " + deger5);
xBeeSerial.print(deger5);
}
My Receiver Code:
#include <SoftwareSerial.h>
#define xBeeRxPin 10
#define xBeeTxPin 9
SoftwareSerial xBeeSerial(xBeeRxPin, xBeeTxPin);
String d1;
void setup() {
xBeeSerial.begin(38400);
Serial.begin(38400);
}
void loop() {
if(xBeeSerial.available()){
d1=xBeeSerial.readString();
Serial.print(d1);
}
}
I obtained the values I wanted and performed the separation. I am sending a list of joystick positions via the serial connection to the arduino in the following format
515;520
Which would be parsed as:
x=515
y=520
MyCoordinator:
#include <SoftwareSerial.h>
#define xBeeRxPin 10
#define xBeeTxPin 9
int deger3;
int deger4;
SoftwareSerial xBeeSerial(xBeeRxPin, xBeeTxPin);
void setup() {
Serial.begin(38400);
}void loop() {
xBeeSerial.begin(38400);
deger3 = analogRead(A0);
deger4 = analogRead(A2);
String deger5 = (String)deger3 + ";" + (String)deger4 ;
Serial.println("string ifade: " + deger5);
xBeeSerial.println(deger5);
delay(200);
}
MyReceiver:
#include <SoftwareSerial.h>
#define xBeeRxPin 10
#define xBeeTxPin 9
SoftwareSerial xBeeSerial(xBeeRxPin, xBeeTxPin);
String d1,d2,d3;
void setup() {
xBeeSerial.begin(38400);
Serial.begin(38400);
}
void loop() {
if(xBeeSerial.available()>0){
d1=xBeeSerial.readStringUntil('n');
//Serial.print(d1);
d2 = d1.substring(0,d1.indexOf(59));
d3 = d1.substring(d1.indexOf(59)+1);
Serial.println("x ekseni: " + d2);
Serial.println("y ekseni: " + d3);
}
}
Answered by Enes Orhan on February 7, 2021
There is a fundamental problem with your communication strategy, and that is you have nothing to tell the receiver "This is the end of a message".
You are sending a constant stream of
515; 510 515; 510 515; 510 515; 510 515; 510 515; 510 515; 510 515; 510 515; 510 515; 510 515; 510 515; 510 515; 510 515; 510.....
and the receiver really doesn't know what to make of that.
This is because you are using the function .readString()
at the receiver. This will blindly receive characters and store them in the target string up until the point those characters stop arriving. And that happens only after a break of at least 1 second in transmission. That never comes. So never ends. That is, until you reset the sender and it sees that break in transmission.
It's a very crude function and I'd never recommend anyone ever using it for anything.
Instead you need to break your transmission up into separate lines with .println()
or appending n
to the string. Then you can use .readStringUntil('n')
to get each line in turn and display it.
Also, on a side note, you really shouldn't be appending all your data to a string then sending the string, that's completely redundant. Just send each individual part of the data as separate prints. All you're achieving with all that String manipulation is to fragment your heap and cause your code to be unstable.
Answered by Majenko on February 7, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP