Go Down

Topic: accllerometer reading data (Read 739 times) 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);

Nick Gammon

This won't work:

Code: [Select]
for(int i =0 ; i< 11 ; i++);
   
    byte discard = Serial.read()


That is doing nothing 11 times, and then reading a single byte. You mean, perhaps:

Code: [Select]

  for(int i =0 ; i< 11 ; i++)   
     Serial.read();


Note the absence of the semicolon on the "if" line.

Similar problem here:

Code: [Select]
  for(int d=12 ; d<=18 ; d=d+2);

Try reading this:

http://gammon.com.au/serial
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

pYro_65

This is an additional post to http://arduino.cc/forum/index.php/topic,95584.msg718044.html#msg718044

Rian

thanks alot for help :) i think i know my mistake now

Rian

hi i have changed my code to this but there is minor problem at the end but i can not figure it out . can anyone help ??

Code: [Select]
#include <XBee.h>
#include <SoftwareSerial.h>

#define num_analog_samples 1
int packet [18];

int analogSamples[num_analog_samples];
int i ;
int x = 0;
int y = 0;
int z = 0;



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();
   
   
   if(int i=12 )
   {
    int analogHigh  = Serial.read( );
    int analogLow = Serial.read();
    x = analogLow + (analogHigh * 256);
   }

  else
   
    if (int i=14) {
    int analogHigh  = Serial.read( );
    int analogLow = Serial.read();
    y = analogLow + (analogHigh * 256);
    }
  else
   
    if (int i=16){
    int analogHigh  = Serial.read( );
    int analogLow = Serial.read();
    z = analogLow + (analogHigh * 256);
    }
  }

  void loop() {
  // read the analog input on pin 0:
 
  // print it out in many formats:
  Serial.println(analogValue);       // print as an ASCII-encoded decimal
  Serial.println(x, DEC);  // print as an ASCII-encoded decimal
  Serial.println(y, DEC);  // print as an ASCII-encoded decimal
  Serial.println(z, DEC);  // print as an ASCII-encoded decimal

 
  delay(10);
}
 
  }
}


pYro_65

You have two void loop functions and an if that you need doesn't enclose its code in {}

Rian

okay i want to ask a question . to make my self clear .

if i have a packet and i know my start bit which is 7E , and i know where is my data after my start bit how can allocate it and print it in the serial port .

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

   //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);
   Serial.flush();


this is what am doing and am not sure this is coding mistake or i can do it like this i know it looks worng but after i discard the bytes i do not need i have 3 values i need to for x y and z
please explain

PaulS

Code: [Select]
   if(int i=12 )
You are creating a new variable, i, assigning it the value 12, and testing whether the result of that assignment is true. The result of the assignment is 12. You could simply have written
Code: [Select]
if(12)
and saved yourself a lot of typing.

Without knowing what you are trying to do here, it is impossible to help you get this straightened out.

Rian

am trying to read my packet which is sent wirelessly form xbee i connected Dout xbee receiver to my Rx arduino

my each packet fram contain 18 bytes  , but my sensor data i need in the packet fram is in the 12th to 16 bytes  after my start byte is found and the rest before that am discarding the bytes  because they are not valuable data .

so am referring to the 12 bytes and i want to print it to the serial port  ,,that is what i wanted to do . ??

PaulS

Based on your statement, the if test should be:
Code: [Select]
if(i == 12)

However, please explain to me how, at that point in the code, i can have any value other than 12.

In other words, why is there a test at all?

Rian

if( i ==12 )

i want this to refere to my 12th byte in the serial read  after i found my start bit

7E 00 0E 83 00 01 3B 00 01 0E 00 01 FB 01 F9 02 70 C9          this is my fame packet i receive

01 FB 01 F9 02 70  this is the data i need to read everytime i receive it the vale always changed . it is my accelerometer data


so in the " if " statement i was referring to they 12th byte   . the 12th and 13th is my X value = 01 FB

so i did Serial.read()  // for my 12th byte
           Serial.read()  // for my 13th byte

the code underneath is to add my 12th and 13th byte to convert it to decimal because they represent ADC value from 0 - 255

Code: [Select]

          int analogHigh  = Serial.read( );
          int analogLow = Serial.read();
    x = analogLow + (analogHigh * 256);


maybe this is completely wrong approach but this is what i want to do

Nick Gammon

Code: [Select]

  for(int i =0 ; i< 11 ; i++)
   
...

   if(int i=12 )


I can see what you are aiming at but first, there are two "i" variables here, not one. Second, as PaulS said, it must be 12, so why test it?

Slightly better would be:

Code: [Select]

  int i;
  for(i =0 ; i< 11 ; i++)
   
...

   if(i == 12 )


Note the ==, not just =.

But i must be 12 so you may as well throw the "if" away.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Rian

Thanks alot i have managed finally to make it works
this is my code
Code: [Select]
int x , y , z;
byte analogHigh, analogLow ;

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 =1 ; i<= 9 ; i++){
   
    if(i == 6){
      analogHigh  = Serial.read( );
      analogLow = Serial.read();
      x = (analogLow + (analogHigh * 256));
      Serial.print("x=");
      Serial.print(x, DEC);
      Serial.print(" , ");
    }
   
    else if(i == 7){
      analogHigh  = Serial.read( );
      analogLow = Serial.read();
      y = (analogLow + (analogHigh * 256));
      Serial.print("y=");
      Serial.print(y, DEC);
      Serial.print(" , ");
    }
   
    else if(i == 8){
      analogHigh  = Serial.read( );
      analogLow = Serial.read();
      z = (analogLow + (analogHigh * 256));
      Serial.print("z=");
      Serial.println(z, DEC);
    }
   
    else{
     byte discard = Serial.read();
     byte discard2 = Serial.read();
     
    }
   
}
  }
}




Go Up