int pwrPin=18; // not initialized in case not used
int gndPin=19; // "
int xPin=A1; // variable to set x pin
int yPin=A2; // y pin
int zPin=A3; // z pin
// self test pin, build does not use
long x, y, z; // hold raw x,y,z reading from sensor
int xAngle, yAngle, zAngle; // hold angle conversions
const int samples = 10; // number of samples for error
int readingX[samples]; // array of x samples
int readingY[samples]; // y
int readingZ[samples]; // z
int neutral = 3; // variable that will hold the deadspace for quadrants
int sensorMax = 470; // holds specification for max sensor range
int sensorMin = 550; // holds specification for min sensor range
// not sure if this will work because the pwrPin and gndPin were not initialized and since this
// only runs once........
void setup()
{
Serial.begin(9600);
pinMode(pwrPin, OUTPUT);
digitalWrite(pwrPin, HIGH);
pinMode(gndPin, OUTPUT);
digitalWrite(gndPin, LOW);
}
void loop(){
// read in (x) samples into the array
for (int i=0; i<samples; i++){
readingX = analogRead(xPin);
_ readingY = analogRead(yPin);_
_ readingZ = analogRead(zPin);
* }
// sum up all the array for averaging*
* for (int i=0; i<samples; i++){
x = x + readingX;
y = y + readingY;
z = z + readingZ;
}
// spit out the averages*
* x = x/samples;
y = y/samples;
z = z/samples;
}
// returns the quadrant that the accelerometer is indicating, quadrants start in top right and go counter clockwise*
int getTiltQuadrant(){
* if(x>=neutral && y>=neutral)return 1;
if(x>-neutral && x<neutral && y>neutral)return 2;
if(x<=neutral && y>=neutral)return 3;
if(x<=-neutral && y>-neutral && y<neutral) return 4;
if(x<-neutral && y<=-neutral) return 5;
if(x>-neutral && x<neutral && y<-neutral) return 6;
if(x>=neutral && y<=neutral) return 7;
if(x>=neutral && y>-neutral && y<neutral) return 8;
}
int getQuadrantTiltAngle(){
if (x>0 && y>0) return atan(x/y);
if (x>0 && y<0) return atan((x/y)+180);
if (x<0 && y<0) return atan((x/y)+180);
if (x<0 && y>0) return atan((x/y)+360);
}
// get the angle of the x in relation to plane*
int getXAngle(){
* xAngle = map(x, sensorMin, sensorMax, -90, 90);
Serial.print(xAngle);
return xAngle;
}
// y axis*
int getYAngle(){
* yAngle = map(y, sensorMin, sensorMax, -90, 90);
Serial.print(yAngle);
return yAngle;*_
}
// z axis
int getZAngle(){
* zAngle = map(z, 662, 355, -90, 90);*
* Serial.print(zAngle);*
* return zAngle;*
}
// setters and getters section
int getX(){
* return x;*
}
int getY(){
* return y;*
}
int getZ(){
* return z;*
}
void setXPin(int pin){
* xPin = 3;*
}
void setYPin(int pin){
* yPin = 2;*
}
void setZPin(int pin){
* zPin = 1;*
}
void setPwrPin(int pin){
* pwrPin = 18;*
}
void setGndPin(int pin){
* gndPin =19;*
}
void setNeutral(int neutral){
* neutral = neutral;*
}
void setSensorMax(int maxS){
* sensorMax = maxS;*
}
void setSenorMin(int minS){
* sensorMin = minS;*
}
* // print the sensor values:*