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);
}
chararray. Then usestrtok()for splitting andatoi()for converting to integer. Here is the reference forstrtok(). 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 08:48strtok(). Arduino split comma separated serial stream – chrisl Nov 17 '20 at 08:48strtok(). 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