Go Down

Topic: aruindo to read my xbee accelerometer data (Read 1 time) previous topic - next topic

Rian

am trying to read my accelerometer data though wireless connection . my receiver connected to arduino and i recive this in the serial monitor :
7E 00 0E 83 00 01 3B 00 01 0E 00 01 FB 01 F9 02 70 C9
which is right
7E START packet  , 00 0E is my packet length  , 83 frame id  , 00 01 source id  , 3B RSSI  , 00 OPTIONS ,01 TOTAL NUMBER OF SAMPLE  , 0E 00 HEADER

01 FB 01 F9 02 70       this is my data i need     01 FB = X       01 F9 = Y        02 70 = Z          C9 = check sum 


am trying to read only my data and discard the other bytes . but how can i assign the vale for X , Y and Z

this is my code i would really appreciate your contribution to help me
am new in coding
thanks :)

Code: [Select]
void readPacket(){  // if serial has 18 bytes which what we need int he buffer my every packets contains 18 bytes
  if (Serial.available() >= 18)  {
   // FIND MY START BIT
    if (Serial.read()==0x7E)
    // read the value we do not need out of the buffer
   for(int i =0 ; i< 11 ; i++);
   
    byte discard = Serial.read();
   

// down here is the problem am trying to read for X Y Z am not sure if this is right ?? i need help
 
  for(int d=12 ; d<=18 ; d=d+2);
    byte[(12)*256]+(13)]  = Serial.read( ,x);
    byte[(14)*256]+(15)]  = Serial.read( ,y);
    byte [(16)*256]+(17)] =Serial.read(  ,z);
   
Serial.print(, x );
Serial.print( , Y );
Serial.print( , Z);



pYro_65

Just a quick run down on statements,

Code: [Select]

int a = 4;

if( a > 3 )
 a++;  //Only this line is part of the above if
 a++; //This will run weather or not the if does anything.

if( a > 4 ){
   a++;  //Both these are part of the same if
   a++;
}


for( int b = 0 ; b < 5 ; ++b );
 a++; //Due to semicolon on for statement, this a++ is not part of it and will only run once when the loop is finished.

for( int b = 0 ; b < 5 ; ++b )
 a++; //This is part of the loop
 a++; //This is not

for( int b = 0 ; b < 5 ; ++b ){
 a++; //These two instructions are part of the loop
 a++;
}

//This is completely wrong
   byte[(12)*256]+(13)]  = Serial.read( ,x);
   byte[(14)*256]+(15)]  = Serial.read( ,y);
   byte [(16)*256]+(17)] =Serial.read(  ,z);


the '[' and  ']' brackets dont match, and byte is a data type, not a variable.

Serial.read( ,x);
Functions cannot have empty paramaters. ( Serial.read doesn't take any paramaters ).

Read this: http://www.cplusplus.com/doc/tutorial/

Rian

okay thanks but how can i fix it

byte[(12)*256]+(13)]  = Serial.read( ,x);


in this i mean that my 12th and 13th byte are my x value in my packet fram ,

[(12)*256]+(13)]  this is because i have MSB , LSB to be added together for my ADC .

can you help please ?
thanks

pYro_65

#3
Mar 08, 2012, 03:26 am Last Edit: Mar 08, 2012, 03:31 am by pYro_65 Reason: 1
Quote
byte[(12)*256]+(13)]  = Serial.read( ,x);


That mess has nothing to fix, it is not code.

Here is a re-write of what you were doing, kind of.
Code: [Select]
void ReadPacket( void )
 {
   if( Serial.available() >= 18 ){
     
     if (Serial.read()==0x7E ){
       
       for(int i =0 ; i< 11 ; i++)
         Serial.read();
       
       int i_Data[ 3 ]; //3 elements for x, y, z
       
       for(int i =0 ; i< 3 ; i++)
         i_Data[ i ] = makeWord( Serial.read(), Serial.read() );
       
       /*
         i_Data[ 0 ] contains X    
         i_Data[ 1 ] contains Y
         i_Data[ 2 ] contains Z      
       */
       Serial.print( "X: " );
       Serial.println( i_Data[ 0 ] );
       Serial.print( "Y: " );
       Serial.println( i_Data[ 1 ] );
       Serial.print( "Z: " );
       Serial.println( i_Data[ 2 ] );
     }
   }
   return;
 }


This won't help you unless you know what you were doing wrong. I suggest you read my first post and that link before you confuse yourself further.

Rian

yes i agree am totally wrong but really coding is not my thing , am sorry if i wrote stupid code and i wanted you to explain .
however i did another aproch and am getting data from my serial port but they r not correct .
can you check this code please
Code: [Select]
void setup(){
  Serial.begin(9600);
  delay(100);
  Serial.print("reading.. ");
  Serial.println();
}



void loop() {
 
  readPacket();
}

void readPacket(){ 
  // if serial has 18 bytes which what we need int he buffer my every packets contains 18 bytes
  if (Serial.available() >= 18)  {
   // FIND MY START BIT
    if (Serial.read()==0x7E)
    // read the value we do not need out of the buffer
   for(int i =0 ; i< 11 ; i++)
   
    byte discard = Serial.read();
   
   //which contains x value for my 12th byte and 13 byte
   if(int i=12 )
   {
    int analogHigh  = Serial.read( );
    int analogLow = Serial.read();
    x = analogLow + (analogHigh * 256);
    Serial.println(x, DEC);
   
   }

  else
    // he is for my y value for my 14th and 15 byte
    if (int i=14) {
    int analogHigh  = Serial.read( );
    int analogLow = Serial.read();
    y = analogLow + (analogHigh * 256);
    Serial.println(y, DEC);
    Serial.flash ();
    }
  else
    // here is for my z value
    if (int i=16){
    int analogHigh  = Serial.read( );
    int analogLow = Serial.read();
    z = analogLow + (analogHigh * 256);
    Serial.println(z, DEC);
    Serial.flash ();
    }
  }



  }



Rian

ops am really sorry about Serial.flash .
i meant
Code: [Select]
Serial.Flush

your feedback is important thanks mate for helping me

Go Up