2

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

How would I split these values up, and convert them to an integer?

MyReceiver:

#include <SoftwareSerial.h>
#define xBeeRxPin 10
#define xBeeTxPin 9
SoftwareSerial xBeeSerial(xBeeRxPin, xBeeTxPin);
char val;

void setup() {
  xBeeSerial.begin(38400);
  Serial.begin(38400);
}
void loop() {
  
  if(xBeeSerial.available()>0){
   val = xBeeSerial.read();
   Serial.print(val);
  }
 
}

My Coordinator:

#include <SoftwareSerial.h>
#define xBeeRxPin 10
#define xBeeTxPin 9
int deger1;
int deger2;
SoftwareSerial xBeeSerial(xBeeRxPin, xBeeTxPin);
void setup() { 
  
   xBeeSerial.begin(38400);
}

void loop() {
  
  deger1 = analogRead(A0);
  deger2 = analogRead(A2);
  char buf[1000];
  const char *first = " ";
  const char *second = ";";
  char val[4];
  char deger[4];

  strcpy(buf,first);
  sprintf(val,"%d",deger1); 
  strcat(buf,val);
  strcat(buf,second);
  sprintf(deger,"%d",deger2);
  strcat(buf,deger);

  Serial.print(buf);
  xBeeSerial.write(buf);  
  delay(200);
}
5
  • 1
    Put all characters into a char array. Then use strtok() for splitting and atoi() for converting to integer. Here is the reference for strtok(). You can even open the online editor with the example there (click on "Edit and run") and play with it, until you understand, how it works. – chrisl Nov 17 '20 at 8:48
  • 5
    And you can look at my answer to this question. It already explains how to use strtok(). Arduino split comma separated serial stream – chrisl Nov 17 '20 at 8:48
  • I examined but i can not. Can you help me in the code selection above? – Enes Orhan Nov 17 '20 at 10:36
  • Where in the code above are you trying to split a string? I cannot know, where you want to do that. And what do you mean by "I can not"? What exactly is the problem? Have you looked at some tutorials on strtok(). Have you tried yourself in the online editor in the reference, that I linked to? Is my answer to the other question not clear enough? I need to know, where exactly the problem is, so that I'm not just writing down the same stuff as in my other answer, but helping you with the problem. – chrisl Nov 17 '20 at 10:54
  • I cannot directly transfer the data I get from xBee into char buffer[]. – Enes Orhan Nov 17 '20 at 13:14