Created
June 29, 2011 19:12
-
-
Save karplus/1054649 to your computer and use it in GitHub Desktop.
Accelerometer test code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Accelerometer (ADXL335) test | |
// Kevin Karplus | |
// 29 June 2011 | |
void setup() | |
{ | |
analogReference(EXTERNAL); // essential if you are going to connect to AREF | |
Serial.begin(115200); | |
Serial.println("setup"); | |
} | |
// I used wires to connect the breakout board to | |
// three analong inputs, 3.3v, and ground. | |
// I also connected the AREF input of the Arduino to 3.3v | |
const int Xpin=2; | |
const int Ypin=1; | |
const int Zpin=0; | |
const int num_to_sum=1000; | |
const float xscale = 0.0098/num_to_sum; | |
const float yscale = 0.0097/num_to_sum; | |
const float zscale = 0.0099/num_to_sum; | |
const int zero_x = 500; | |
const int zero_y = 506; | |
const int zero_z = 511; | |
long xsum,ysum,zsum; | |
void loop() | |
{ | |
unsigned long startTime=millis(); | |
xsum = ysum = zsum = 0; | |
for (int i=num_to_sum; i>0; i--) | |
{ | |
xsum += analogRead(Xpin) -zero_x; | |
ysum += analogRead(Ypin) -zero_y; | |
zsum += analogRead(Zpin) -zero_z; | |
} | |
Serial.print(millis()-startTime); | |
float x=xscale*xsum; | |
float y=yscale*ysum; | |
float z=zscale*zsum; | |
Serial.print(" X="); | |
Serial.print(x); | |
Serial.print(" Y="); | |
Serial.print(y); | |
Serial.print(" Z="); | |
Serial.print(z); | |
Serial.print(" total="); | |
Serial.println(sqrt(x*x+y*y+z*z)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment