Skip to content

Commit a9532fc

Browse files
committed
bug fixes, combined accelerometer tests into seperate file
1 parent 7afdb41 commit a9532fc

File tree

3 files changed

+260
-35
lines changed

3 files changed

+260
-35
lines changed

arduino/accelerometer.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
int convertToReading(int maxScale, float g);
2+
3+
float getMaxG();
4+
5+
void testAccelerometerReading();
6+
7+
void testConvertToReading();
8+
9+
void testGetMaxG();
10+

arduino/accelerometerTests.ino

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "accelerometer.h"
2+
3+
void testAccelerometerReading() {
4+
Serial.println("Testing accelerometer reading...");
5+
int16_t x, y, z;
6+
int resultX, resultY, resultZ = 0;
7+
8+
for (int i = 0; i < 10; i++) {
9+
xl.readAxes(x, y, z);
10+
if (x) {
11+
resultX = 1;
12+
}
13+
if (y) {
14+
resultY = 1;
15+
}
16+
if (z) {
17+
resultZ = 1;
18+
}
19+
}
20+
21+
if (resultX && resultY && resultZ) {
22+
Serial.println("passed");
23+
} else {
24+
Serial.println("FAILED");
25+
}
26+
}
27+
28+
void testConvertToReading() {
29+
int seed = analogRead(7); //uses stray voltage from unused pin to get "true" random number
30+
randomSeed(seed);
31+
bool passed = true;
32+
33+
Serial.println("Testing convertToReading()...");
34+
35+
for(int i = 0; i < 10; i++) {
36+
int rand = random();
37+
float g = xl.convertToG(scale, rand);
38+
int reading = convertToReading(scale, g);
39+
if (reading != rand) {
40+
passed = false;
41+
}
42+
}
43+
44+
if (passed) {
45+
Serial.println("passed");
46+
} else {
47+
Serial.println("FAILED");
48+
}
49+
}
50+
51+
void testGetMaxG() { //assumes device is at rest, may fail if device is currently accelerating/decelerating
52+
Serial.println("Testing getMaxG()...");
53+
54+
float avgMaxG = 0;
55+
int numTests = 10;
56+
57+
for(int i = 0; i < numTests; i++) {
58+
while(!(xl.newXData() || xl.newYData() || xl.newZData())){}
59+
avgMaxG += getMaxG();
60+
}
61+
62+
avgMaxG /= numTests;
63+
64+
if (avgMaxG < 5.0 && avgMaxG >= 0.0) {
65+
Serial.println("passed");
66+
} else {
67+
Serial.println("FAILED");
68+
}
69+
}

0 commit comments

Comments
 (0)