2

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);
  }
}
1

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.

6
  • Can I handle it using xBeeSerial.end, like xBeeSerial.begin?
    – Enes Orhan
    Nov 17 '20 at 12:11
  • No. You have to construct a proper syntax and flow of information. Just like English. At the moment you're sending the contents of a book all on one line with no punctuation and no paragraphs. Do it properly. Don't try and cut corners. You will just end up even more frustrated.
    – Majenko
    Nov 17 '20 at 12:13
  • I am newbie in working with Arduino. It's really hard for me. Also, i am not good at English, sorry about that. Can you make improvements directly to the code?
    – Enes Orhan
    Nov 17 '20 at 12:19
  • I have told you explicitly what to change. I'm not going to program it for you too.
    – Majenko
    Nov 17 '20 at 12:21
  • if(xBeeSerial.available()){ d1=xBeeSerial.readStringUntil('\n'); Serial.println(d1); }
    – Enes Orhan
    Nov 17 '20 at 12:34
1

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);
  }  
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.