-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccelerometer.cpp
133 lines (112 loc) · 3.09 KB
/
Accelerometer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef ACCELEROMETER_H
#include "Accelerometer.h"
#endif
#include <Arduino.h>
#include <Wire.h>
Accelerometer::Accelerometer():
tol(0.005), calibrationTime(1.50)
{
xVal=accel.x; yVal=accel.y; zVal=accel.z;
xMin = xVal; xMax = xVal;
yMin = yVal; yMax = yVal;
zMin = zVal; zMax = zVal;
}
void Accelerometer::init(){
accel.init();
Accelerometer();
}
void Accelerometer::read(){
accel.read();
xVal=accel.x; yVal=accel.y; zVal=accel.z;
}
bool Accelerometer::available(){
return accel.available();
}
double Accelerometer::getTol(){
return tol;
}
void Accelerometer::setTol(double value){
tol=value;
}
double Accelerometer::getCalibrationTime(){
return calibrationTime;
}
void Accelerometer::setCalibrationTime(double value){
calibrationTime=value;
}
void Accelerometer::getBaseLine(){
double baselineoffset = 0.10;
double clock = 0.00;
Serial.println("=============== GETTING BASELINE... ===============");
while ( clock < getCalibrationTime() ){
clock = millis()/1000.000;
// printDouble (clock, 1000);
accel.read();
// Calibrate X-axis
xVal = accel.x;
if ( xVal < xMin ) { xMin = xVal+baselineoffset; }
else if ( xVal > xMax ) { xMax = xVal-baselineoffset; }
// Calibrate Y-axis
yVal = accel.y;
if ( yVal < yMin ) { yMin = yVal+baselineoffset; }
else if ( yVal > yMax ) { yMax = yVal-baselineoffset; }
// Calibrate Z-axis
zVal = accel.z;
if ( zVal < zMin ) { zMin = zVal+baselineoffset; }
else if ( zVal > zMax ) { zMax = zVal-baselineoffset; }
}
Serial.println("=============== BASELINE READ COMPLETE ===============");
delay(400);
}
void Accelerometer::printDouble(double val, unsigned int precision){
Serial.print (int(val));
Serial.print(".");
unsigned int frac;
if(val >= 0)
frac = (val - int(val)) * precision;
else
frac = (int(val)- val ) * precision;
Serial.println(frac,DEC) ;
}
int Accelerometer::detect(){
double tolerance = getTol();
if ( xVal > (xMax + tolerance ) || xVal < (xMin - tolerance) ){
Serial.println("X Detected");
Serial.println();
printDouble(xVal,100);
return 1;
}
else if ( yVal > (yMax + tolerance ) || yVal < (yMin - tolerance) ){
Serial.println("Y Detected");
printDouble(yVal,100);
return 1;
}
else if ( zVal > (zMax + tolerance ) || zVal < (zMin - tolerance) ){
Serial.println("Z Detected");
printDouble(zVal,100);
return 1;
} else return 0;
}
void Accelerometer::printBoundaryValues(){
Serial.println("xMin\txMax\t|\tyMin\tyMax\t|\tzMin\tzMax");
Serial.print(xMin);
Serial.print("\t");
Serial.print(xMax);
Serial.print("\t \t");
Serial.print(yMin);
Serial.print("\t");
Serial.print(yMax);
Serial.print("\t \t");
Serial.print(zMin);
Serial.print("\t");
Serial.print(zMax);
Serial.print("\t");
}
void Accelerometer::printAccelValues(){
Serial.println("x\t|\ty\t|\tz");
Serial.print(xVal);
Serial.print("\t");
Serial.print(yVal);
Serial.print("\t");
Serial.print(yVal);
}