#### irangathathsara

##### Newbie level 5

int Acc_X;

int Acc_Y;

int Acc_Z;

int Acc_REF;

int diff_X;

int diff_Y;

int diff_Z;

double acceleration_X;

double acceleration_Y;

double acceleration_Z;

double force_vector;

double rotate_X;

double rotate_Y;

double rotate_Z;

double force_vector_calculation()

{

acceleration_X= Acc_X*0.001;

acceleration_Y= Acc_Y*0.001;

acceleration_Z= Acc_Z*0.001;

force_vector=sqrt(pow(acceleration_X,2)+pow(acceleration_Y,2)+pow(acceleration_Z,2));

return force_vector;

}

int angle_X()

{

rotate_X=acos(acceleration_X/force_vector);

return rotate_X;

}

int angle_Y()

{

rotate_Y=acos(acceleration_Y/force_vector);

return rotate_Y;

}

int angle_Z()

{

rotate_Z=acos(acceleration_Z/force_vector);

return rotate_Z;

}

void main()

{

trisa=0xff;

ADCON1=0b00000001;

UsART_Init(9600);

while(1)

{

Acc_X=Adc_read(1);

Acc_Y=Adc_read(2);

Acc_Z=Adc_read(3);

Acc_REF=Adc_Read(4);

delay_ms(5);

force_vector_calculation();

angle_X();

angle_Y();

angle_Z();

Usart_Write( rotate_X);

Usart_Write( rotate_Y);

Usart_Write( rotate_Z);

}