Separating several sensor values

I´m starting a project were 2 Arduinos comunicate through wireless modules (XBee Pro).


Arduino #1 has two potenciometers and sends their values
Arduino #2 recieves the values like this:

100,230
100,231
100,233
100,234
...

Now I need to separate this values and assign each one to a variable.
But I´m stuck here.. any help would be apreciated!!

Use the C library

You can use C library functions to do this. If you have ‘sscanf’, then you can do it in one go. If not, then you can use ‘atoi’ twice. Let’s say your line of ASCII text is in a character array called ‘lin’ and you want two integers ‘pot1’ and ‘pot2’:

char lin[32];
int pot1, pot2;

Then, with ‘sscanf’:

sscanf (lin, “%d,%d”, &pot1, &pot2);

or with ‘atoi’:

char *p;
pot1 = atoi (lin);
p = strchr (lin, ‘,’);
p++;
pot2 = atoi §;

Note the use of ‘strchr’ to find the comma separating the two values.

Hope that’s given you some ideas!

 

Yeah, the C standard

Yeah, the C standard libraries are available to the Arduino, but be aware that importing them will add to your code size. But if you’re not yet close to the 14k limit then it shouldn’t be a problem.

Dan

I seen that ‘atoi’ and

I seen that ‘atoi’ and ‘strchr’ is frequently used in the arduino C platform, as ‘sscanf’ seems to be not used at all.

in my loop function I have this:

void loop() {
if (mySerial.available()) {
Serial.println((char)mySerial.read());
}
}

This prints to the output window the potenciometers values, now I can´t see where should I place the code bit you posted…

Thanks for helping me!

for now code size won´t be

for now code size won´t be a problem (I guess)

can you tell what is the necessary library to use the ‘sscanf’ function? of the ‘atoi’ plus the ‘strchr’ ?

after doing some research on

after doing research on how to read ‘byte’ values and convert them to ‘int’, I came up to this solution. :slight_smile:

can´t wait to arrive home and try to control my bot´s motors with this :smiley:

 

sender arduino:

int analogValue2, analogValue5;
int val2, val5;

void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
}

void loop()
{
// read analog input
analogValue2 = analogRead(5);
analogValue5 = analogRead(2);

val2 = map(analogValue2, 0, 1023, 253, 0); //254 and 255 for SYNC
val5 = map(analogValue5, 0, 1023, 253, 0);

Serial.print(254, BYTE); //SYNC char
Serial.print(val2, BYTE);
Serial.print(255, BYTE); //SYNC char
Serial.print(val5, BYTE);

// pause for 100 milliseconds:
delay(100); //
}
 
 
 
 
 
 
 

 

reciever arduino:

#include <AFSoftSerial.h>
AFSoftSerial mySerial = AFSoftSerial(3, 2);

byte incomingByte;
byte sensor1, sensor2;

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void setup() {
Serial.begin(9600); // connect to the serial port
Serial.println(“Goodnight moon!”);

// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println(“Hello, world?”);

}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void loop() {

if (mySerial.available()) {
incomingByte = mySerial.read();

Serial.println(int(incomingByte));

if ((int(incomingByte) == 254)) {
sensor1 = mySerial.read();
Serial.print("Sensor 1 = ");
Serial.println(int(sensor1));
}

if ((int(incomingByte) == 255)) {
sensor2 = mySerial.read();
Serial.print("Sensor 2 = ");
Serial.println(int(sensor2));
}

}

}