Xbee recive-sent data confuse

Hello, i am making a robot where i will control it via xbee from away controller. I used 2 mega and 2 Xbee-S1.

I configured both xbee and both program and now i test by sending a pwm value (0-255) by X-CTU’s terminal and all worked fine.

The problem/question is , i want to send a pwm value(0-255) , a string value ( “F0” or “B0”) and an int value (1 or 2 or 3).
So basicly 3 differend variables.

How can i recieve/send each variable; By sending 1 variable all time is easy and i made it… but sending 3 different values confuse me.

How can i recieve/send each variable; By sending 1 variable all time is easy and i made it.. but sending 3 different values confuse me.

Show us the easy code, then. Sending three values, separated by some delimiter, and parsed on the other end is easy, too. But, what are we building on?

Thats the code for testing, i send pwm value via terminal :

int pwm_var1 = 10; //pwm pin
int pwm_var2 = 9;  // pwm pin
int s1=7;          //pin for H-bridge
int s2=5;          //pin for H-bridge
int i=0;           // variable for pwm


void setup(){
	Serial.begin(9600);
  
        pinMode(pwm_var1, OUTPUT);
        pinMode(pwm_var2, OUTPUT);
        pinMode(s1, OUTPUT);
        pinMode(s2, OUTPUT);
}

void loop(){
  
   if(Serial.available() > 0){
 	 
   i=Serial.read();
   if(i>=0 && i<=255)
   {
        digitalWrite(s1, LOW);
        digitalWrite(s2, HIGH);
        analogWrite(pwm_var1, 0);
        analogWrite(pwm_var2, i);
  }
 
 }
}

if now i want to send 3 differend values, how can i read them 1 by 1 in the row ?

[number] [space] [string] [space] [number] [new line] On the receiving arduino, keep reading until you get the [new line]. Then split the info into 3 pieces.

You mean send " 240 BO 2" and how i will make it 3 difference pieces? Also what you mean new line ;

Im a bit confused :/

You mean send " 240 BO 2" and how i will make it 3 difference pieces? Also what you mean new line ;

You could send all the data as ASCII data, as you show here, or by using a mix of binary and ASCII as liudr suggests. My preference is for all ASCII, since the binary value might be the same as the delimiter, so extracting the data is more difficult.

As for extracting the 3 pieces, strtok() works great.

As for the new line, println() rather than print().

I am a bit confused, lets say that i have the 3 variables.I will need to merge them into 1 string and then send it to the other arduino. Strtok() will need to make the extract .I tried to figure it how and cant.

I think that i can send between data something like that “pwm’|‘mode’|'dir” .

#include <string.h>

int pwm=231;                      // will take values 0-255                     
char mode='F0';                // will take values "FO" or "BO"
int dir=2;                               // will take values 1 or 2 or 3
char *info;                            // the whole info merged  
char delims = "|";                // variable for extract

void setup()
{
  Serial.begin(9600);
}

void loop()
{
                    //  Serial.println(pwm + "delims" + mode + "delims" + dir);
}

How can i merge those 3 variables into 1 to send it ;

I made this test programm for sending the string with whole info. Any tips to make it better;
About the programm on other arduino, i am confused how to use the Strtok()

#include <string.h>
String info ;  // the whole info merged  
int pwm=231;     // will take values 0-255
char mode='F0';  // will take values "FO" or "BO"
int dir=2;       // will take values 1 or 2 or 3
String delims = "|";  // variable for extract


void setup()
{
  Serial.begin(9600);
}

void loop()
{
      info=pwm+delims+mode+delims+dir; // info="231|F0|2"
}

I made this test programm for sending the string with whole info. Any tips to make it better;

That is NOT using a string. It is using a String, which is not even close to the same thing.

sprintf() is you friend. String is not.

Thanks Paul for helping, i read about that fuction and write this programm:

#include <string.h>
#include <stdio.h>
char info[30] ;  // the whole info merged  
int pwm=231;     // will take values 0-255
char mode='F0';  // will take values "FO" or "BO"
int dir=2;       // will take values 1 or 2 or 3
int n;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
    n=sprintf (info,"%d|%d|%d", pwm, mode, dir); // info="231|F0|2"
    Serial.println(info);
    delay(4000);
}
char mode='F0';  // will take values "FO" or "BO"

Which [u]ONE[/u] key did you press to [u]get[/u] the ONE character in the single quotes?

PaulS: char mode='F0';  // will take values "FO" or "BO"

Which [u]ONE[/u] key did you press to [u]get[/u] the ONE character in the single quotes?

Explain a bit more what you mean

Single quotes are used to define a single character. A single character can be typed by pressing one key. My keyboard doesn't have a single key labeled 'F0'. Does yours?

Yeah but for using " " i need to do like this ;

char *mode="F0";  // will take values "FO" or "BO"

Yeah but for using " " i need to do like this ;

Yes. So, what's the problem? Aside from using the wrong format specifier for the string in sprintf()?

SO I GUESS THE PROGRAMM FOR SEND THE INFO MUST BE LIKE THIS AFTER FIX THE SINGLE QUOTES PROBLEM U SAID.

aLSO WHAT YOU MEAN "the wrong format specifier for the string in sprintf()?"

    n=sprintf (info,"%d|%d|%d", pwm, mode, dir); // info="231|F0|2"

mode is a string. %d is the format specifier for int. The format specifier for string is %s (lower case s).

PaulS:

    n=sprintf (info,"%d|%d|%d", pwm, mode, dir); // info="231|F0|2"

mode is a string. %d is the format specifier for int. The format specifier for string is %s (lower case s).

yes i check the format on this site, http://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm

for sending programm maybe be like this:

#include <string.h>
#include <stdio.h>
char info[30] ;  // the whole info merged  
int pwm=231;     // will take values 0-255
char *mode="F0";  // will take values "FO" or "BO"
int dir=2;       // will take values 1 or 2 or 3
int n;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
    n=sprintf (info,"%d|%s|%d", pwm, *mode, dir); // info="231|F0|2"
    Serial.println(info);
    delay(4000);
}

and receiving:

#include <SoftwareSerial.h>
#include <string.h>
#include <stdio.h>

char *strings[30];
char *info;
char *pwm;
char *mode;
char *dir;
int pwm_int;
int dir_int;
int i = 0;
char delims[] = "|";


void setup()
{
  Serial.begin(9600);
}

void loop()
{
 if(Serial.available() > 0)
 {
   *info=Serial.read();          // lets say info that comes is: "231|F0|2"
   strings[i] = strtok(info, delims );
   pwm=strings[i];              // pwm="231" 
   strings[++i] = strtok( NULL, delims ); 
   mode=strings[i];            //   mode="F0" 
   strings[++i] = strtok( NULL, delims ); 
   dir=strings[i];            // dir = "2"
   
   pwm_int=atoi(pwm);      //convert to int
   dir_int=atoi(dir);      //conbert to int

  }
}
char *strings[30];

No. You do not need (or want) and array of pointers to strings. You want an array of chars.

   *info=Serial.read();          // lets say info that comes is: "231|F0|2"

You can say that, but it isn't true. Serial.read() returns ONE character. You need to add an end delimiter to what is sent, and read and store data (in an array of chars, not an array of pointers) until that end delimiter arrives. Then you can use the data.