Preston Stephens
/
Mini_Project_Day_8_Level
First
Diff: acc.cpp
- Revision:
- 6:459d1eb2cc23
- Parent:
- 5:98ad3701ec24
diff -r 98ad3701ec24 -r 459d1eb2cc23 acc.cpp --- a/acc.cpp Thu Jan 14 22:03:54 2016 +0000 +++ b/acc.cpp Thu Jan 14 22:52:20 2016 +0000 @@ -9,8 +9,7 @@ Serial pc(USBTX, USBRX); char buffer[6]; //raw filler data int16_t data[3]; //16bit twos-compliment integer data -float x,y,z; //set up floats for values to display - + void acc_init(void){ cs=1; //initially off acc.format(8,3); //8bit, mode 3 @@ -26,6 +25,7 @@ } float acc_sense(char axis){ + float x,y; cs=0; acc.write(0x80|0x40|0x32); //RW bit high, MB bit high, address to ask for a reading for (int i=0;i<=5;i++){buffer[i]=acc.write(0x00);} //read 6bytes @@ -34,8 +34,10 @@ data[1]=buffer[3]<<8|buffer[2]; //y combine MSB and LSB data[2]=buffer[5]<<8|buffer[4]; //z - x=atan(data[0],data[2]); - y=atan(data[1],data[2]); + x=(atan2((double)data[0],(double)data[2])*180/3.1416); //convert the signal into an angle, to be passed out as x,y + y=(atan2((double)data[1],(double)data[2])*180/3.1416); + +// pc.printf("Angle x = %1.2f \t Angle y = %1.2f \n\r", x,y); if(axis==1) return x; //if asked for x, give x if(axis==2) return y; //if asked for y, give y