# Rabbit Ears on a Cap

## Step 4: Testing of Accelerometer

Now, the most difficult part, testing of the accelerometer.This was the first time we were working with accelerometers hence the tremendous amount of time spent experimenting and trying to understand the values.

We found a great website that helped provide us with a basic understanding of accelerometers as well as a great code.

This is the code they provided and what we used.
=================================================

//////////////////////////////////////////////////////////////////
//Simple code for the ADXL335, prints calculated orientation via serial
//////////////////////////////////////////////////////////////////

const int xPin = 0;
const int yPin = 1;
const int zPin = 2;

//The minimum and maximum values that came from
//the accelerometer while standing still
//You very well may need to change these
int minVal = 265;
int maxVal = 402;

//to hold the caculated values
double x;
double y;
double z;

void setup(){
Serial.begin(9600);
}

void loop(){

//read the analog values from the accelerometer

//convert read values to degrees -90 to 90 - Needed for atan2
int xAng = map(xRead, minVal, maxVal, -90, 90);
int yAng = map(yRead, minVal, maxVal, -90, 90);
int zAng = map(zRead, minVal, maxVal, -90, 90);

//Caculate 360deg values like so: atan2(-yAng, -zAng)
//atan2 outputs the value of -π to π (radians)
//We are then converting the radians to degrees
x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI);
y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI);
z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);

//Output the caculations
Serial.print("x: ");
Serial.print(x);
Serial.print(" | y: ");
Serial.print(y);
Serial.print(" | z: ");
Serial.println(z);

delay(100);//just here to slow down the serial output - Easier to read
=======================================================
Remove these ads by Signing Up