-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3 Axis Accelerometer
More file actions
133 lines (118 loc) · 5.56 KB
/
3 Axis Accelerometer
File metadata and controls
133 lines (118 loc) · 5.56 KB
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
/*
3 Axis Accelerometer Smoothing
20 samples to keep track of. The higher the number, the
more the readings will be smoothed, but the slower the output will respond to
the input. Using a constant rather than a normal variable lets us use this
value to determine the size of the readings array.
when changes to the Avergae Serial data is sent to the COM port to be read and LED changes state
Hardware Setup
Arduino UNO
LED on digital pins A5, A6, A7
Freetronics AM3x (XC4226)Accelerometer Module 3 Axis +/- 1.5g, +/-6g
3 Axis Accelerometer on analog pins x = 0, Y = 1, Z = 2, 3.3v, GND
*/
// X Axis
const int numReadings = 20;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = A0; // LED Pinout
int x = 0; // Average Data
// Y Axis
const int numReadings1 = 10;
int readings1[numReadings1]; // the readings from the analog input
int readIndex1 = 0; // the index of the current reading
int total1 = 0; // the running total
int average1 = 0; // the average
int inputPin1 = A1; // LED Pinout
int y = 0; // Avergae data
//Z Axis
const int numReadings2 = 10;
int readings2[numReadings2]; // the readings from the analog input
int readIndex2 = 0; // the index of the current reading
int total2 = 0; // the running total
int average2 = 0; // the average
int inputPin2 = A2; // LED Pinout
int z = 0; // Average data
int p = 0; // Note change to data value to send data to Serial
void setup()
{
Serial.begin(115200); // initialize serial com with computer
for (int thisReading = 0; thisReading < numReadings; thisReading++) // initialize all the readings to 0
{
readings[thisReading] = 0;
}
}
void loop()
{
// X Axis
total = total - readings[readIndex]; // subtract the last reading
readings[readIndex] = analogRead(inputPin); // read from the sensor
total = total + readings[readIndex]; // add the reading to the total
readIndex = readIndex + 1; // advance to the next position in the array
if (readIndex >= numReadings) // if we're at the end of the array...
{
readIndex = 0; // ...wrap around to the beginning:
}
average = total / numReadings; // calculate the average:
// Y Axis
total1 = total1 - readings1[readIndex1]; // subtract the last reading:
readings1[readIndex1] = analogRead(inputPin1); // read from the sensor:
total1 = total1 + readings1[readIndex1]; // add the reading to the total
readIndex1 = readIndex1 + 1; // advance to the next position in the array:
if (readIndex1 >= numReadings1) // if we're at the end of the array...
{
readIndex1 = 0; // ...wrap around to the beginning:
}
average1 = total1 / numReadings1; // calculate the average:
// Z Axis
total2 = total2 - readings2[readIndex2]; // subtract the last reading:
readings2[readIndex2] = analogRead(inputPin2); // read from the sensor:
total2 = total2 + readings2[readIndex2]; // add the reading to the total:
readIndex2 = readIndex2 + 1; // advance to the next position in the array:
if (readIndex2 >= numReadings2) // if we're at the end of the array...
{
readIndex2 = 0; // ...wrap around to the beginning:
}
average2 = total2 / numReadings2; // calculate the average:
if (x == average) // X Axis LED high/low change in data
{
digitalWrite(5, LOW); // turn LED A5 off
}
else
{
digitalWrite(5, HIGH); // turn LED A5 on
p = 1; // change to average data
}
if (y == average1) // Y Axis LED high/low change in data
{
digitalWrite(6, LOW); // turn LED A6 off
}
else
{ digitalWrite(6, HIGH); // turn LED A6 off
p = 1; // change to average data
}
if (z == average2) // Z Axis LED high/low change in data
{
digitalWrite(7, LOW); // turn LED A7 off
}
else
{ digitalWrite(7, HIGH); // turn LED A7 off
p = 1; // change to average data
}
if (p==1) // send data to serial if p==1, if p==0 send no data
{
Serial.print(" X "); // send X, Y, Z data to Serial port
Serial.print(average);
Serial.print(" Y ");
Serial.print(average1);
Serial.print(" Z ");
Serial.println(average2);
}
x = average; // store new x average
y = average1; // store new y average
z = average2; // store new z average
p = 0; // reset P to 0
delay(200); // Delay time
}