Pages: [1]   Go Down
Author Topic: help needed serial communication of Xbee  (Read 567 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);    

Serial.begin(9600);
}

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;
  
  Serial.print("L");
 Serial.print(accelerationX);
 Serial.print(",");
 Serial.print(accelerationY);
 Serial.print(",");  
 Serial.println("H");
 delay (1000);
}
 

and here is the reciever code.

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

Serial.begin(9600);
}
void loop()
{
   while(Serial.available() > 0)
   {
     //Serial.println("ready");
       char aChar = Serial.read();
Serial.println(aChar);
       if(aChar == 'L)
       {
     Serial.println("haakje");
           started = true;
           index = 0;
           inData[index] = ',';
       }
       else if(aChar == 'H')
       {
           ended = true;
       }
       else if(started)
       {
           inData[index] = aChar;
           index++;
           inData[index] = ',';
          }
   }
  

   if(started && ended)
   {
       // Convert the string to an integer
       int inInt = atoi(inData);
          Serial.println(inInt);
       // 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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46204
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
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.
Logged

Pages: [1]   Go Up
Jump to: