Go Down

Topic: help needed serial communication of Xbee (Read 1 time) previous topic - next topic


Hi ,I been trying to send my memsic 2125 accelerometer value X and Y from transmitter xbee to reciver  xbee and its getting the value.i am sending value as string so it will start reading every character in between < and >  which example < -32,-56 > but i need to change this value from character to int.I have tried but still not able to conver it to int .....this is my school project and i am not even in programming field ..so anyhelp would be great and appreciated....

bottom is my transmitter code ...

const int xpin = 11; // select the input pin for the sensor
const int ypin = 12; // select the input pin for the sensor

void setup(){

 pinMode(xpin, INPUT);    
 pinMode(ypin, INPUT);    


void loop () {
  // variables to read the pulse widths:
 int pulseX, pulseY;
 // variables to contain the resulting accelerations
 int accelerationX, accelerationY;
 // read pulse from x- and y-axes:
 pulseX = pulseIn(xpin,HIGH);  
 pulseY = pulseIn(ypin,HIGH);
 // convert the pulse width into acceleration
 // accelerationX and accelerationY are in milli-g's:
 // earth's gravity is 1000 milli-g's, or 1g.
 accelerationX = ((pulseX / 10) - 500) * 8;
 accelerationY = ((pulseY / 10) - 500) * 8;
delay (1000);

and here is the reciever code.

char inData[10];
int index;
boolean started = false;
boolean ended = false;
void setup(){

void loop()
  while(Serial.available() > 0)
      char aChar = Serial.read();
      if(aChar == 'L)
          started = true;
          index = 0;
          inData[index] = ',';
      else if(aChar == 'H')
          ended = true;
      else if(started)
          inData[index] = aChar;
          inData[index] = ',';

  if(started && ended)
      // Convert the string to an integer
      int inInt = atoi(inData);
      // Use the value

      // Get ready for the next time
      started = false;
      ended = false;

      index = 0;
      inData[index] = ',';

i didnt really understood the reciever code..
based on the recieved value i have  to trun the car motors.
and i am getting value on transmitter side like

L -32 ,-45 H

-32 is my x value of transmitter and -45 is my Y value of transmitter.
and as i move my accelerometer from -X to +x the value can go up to
-700 to +700.


i am sending value as string so it will start reading every character in between < and >  which example < -32,-56 > but i need to change this value from character to int.

On the sender, you are sending a string like L-32,-45,H. Why you put that , after the second value, I do not know. Why you are using L and H instead of < and > (or other non-letter symbols), I do not know.

Then, on the receiver, you are putting ',' in the array, where you should be putting '\0'. Why?

Each character that is received is looked at to determine if it should be added to the array. The start of packet marker is not. A comma is put in it's place, instead. The flag that says that the start of the packet was received is set to true.

The characters up to the end of packet markers ARE added to the end of the array, followed be another comma.

When the end of the packet is detected, the flag that indicates that the end of packet marker was received is set to true. There should be a break; statement after this, but it is missing.

You end up with "-32,-45,," in the array. Now you need to parse it. The inData array does not contain a single number, AND is not NULL terminated. So, it can not be passed to atoi(). You will need to read up on either sscanf or strtok AND you need to NULL terminate the array.

this is my school project and i am not even in programming field

What field are you in?

Not Communications, I hope, unless English is not your native language. If it isn't, forget about this last paragraph. You English is better than my ______ (whatever you native language is).

While you didn't state what your problem was, I hope I have given you enough information to solve it.
The art of getting good answers lies in asking good questions.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131