1
\$\begingroup\$

I am trying the code in this datasheet on an Arduino mega2560 with mpu6050 IMU,

I have made some changes in the variables and I do not get errors so far. and the code doesn't give any output, can some show me a place with wrong code work?

I get mpu6050 range accX and accY -2^15 to + 2^15. I do not post code for mpu6050 since it works. I need to serial print and see how it displacement changes in this code. in positionxy() function I wanted to print

 Sample_X =  (unsigned char)accX/129;
 Sample_Y =  (unsigned char)accY/129;     
 Serial.print ( Sample_X );  Serial.print ("\t"); 
 Serial.println ( Sample_Y ); Serial.print ("\t"); 

and it gives nothing, i think the issue is with the variable types and i cannot figure it out. The part of the code as follows,

//all varibles 
double accX, accY, accZ;
unsigned char  Sample_X;
unsigned char  Sample_Y;
unsigned char  Sample_Z;
unsigned char  Sensor_Data[8];
unsigned char  countx,county ;
signed int  accelerationx[2], accelerationy[2];
signed long  velocityx[2], velocityy[2];
signed long  positionX[2];
signed long  positionY[2];
signed long  positionZ[2]; 
unsigned char  directionxy;
unsigned long  sstatex,sstatey;


void setup(){
 *****mpu 6050 config is done here and works aacX and accY updates with 
 current accelration value  *****
}
void loop(){
do{
Calibrate();
movement_end_check();
positionxy();  
}while(1);
}

void Calibrate() 
{
************see datasheet **********   
}


void positionxy()  
{
unsigned char count2 ;
count2=0;

do{
Sample_X =  (unsigned char)accX/129; // this line is changed 
Sample_Y =  (unsigned char)accY/129; // this line is changed 

below serial print always gives zero

Serial.print ( Sample_X );  Serial.print ("\t"); 
Serial.println ( Sample_Y ); Serial.print ("\t"); 

accelerationx[1]=accelerationx[1] + Sample_X;  
accelerationy[1]=accelerationy[1] + Sample_Y;  


************see datasheet **********   
}
movement_end_check(){
************see datasheet **********   

}
\$\endgroup\$
  • 1
    \$\begingroup\$ Try printing accX and accY instead of the divided value. \$\endgroup\$ – Russell Borogove Apr 16 '18 at 18:19
  • \$\begingroup\$ @Russell Borogove Still same issue, I divided it since unsigned char can hold only 0-255, and I get -2^15 to +2^15 for the accX and accY \$\endgroup\$ – oppo Apr 16 '18 at 18:38
4
\$\begingroup\$

The conversion to unsigned char has precedence over division. So it first converts then divides afterwards. Use parantheses to override:

Sample_X = (unsigned char)(accX/129)

\$\endgroup\$
  • \$\begingroup\$ Sample_X gets the value update, but it does not change the value of acceleration, get same value all the time ant suggestions? i used Serial.print ( (int8_t) accelerationx ); Serial.print ("\t"); Serial.println ( (int8_t) accelerationy ); Serial.print ("\t"); \$\endgroup\$ – oppo Apr 16 '18 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.