-
Notifications
You must be signed in to change notification settings - Fork 1
/
Accelerometer.cpp
62 lines (46 loc) · 1.61 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
#include "Accelerometer.h"
#include <stdlib.h>
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
// Accelerometer sensitivity. We divide all readings by this before adding.
// DO NOT MAKE ZERO OR YOU WILL END THE UNIVERSE!
const int Sensitivity = 8;
// Accelerometer deadzone. This means that small movements and thermal noise won't
// be listened to, but allows larger movements to still be counted. The larger this
// value, the larger a change in acceleration is required to register.
const int Deadzone = 10;
// Each cycle, reduce sparkle by this percentage, in addition to the 1 unit
// we decrease at anyway.
const int Decay = 1;
// Constructor.
Accelerometer::Accelerometer(pin_t input_pins[ACCEL_AXES]) {
_sparkle = 0;
// Set the hardware for input and take first values.
for (int i = 0; i < ACCEL_AXES; i++) {
_input_pins[i] = input_pins[i];
pinMode(_input_pins[i], INPUT);
_last_values[i] = analogRead(_input_pins[i]);
}
}
void Accelerometer::update() {
// Decay our sparkle from last iteration.
_sparkle = max(0, ((_sparkle * (100 - Decay) / 100) - 1));
int potential_sparkle = 0;
for (int i=0; i < ACCEL_AXES; i++) {
int val = analogRead(_input_pins[i]);
// Add our difference to our potential sparkle.
potential_sparkle += abs(val - _last_values[i]) / Sensitivity;
// And save this as the last reading.
_last_values[i] = val;
}
// Add our sparkle iff it's greater than our deadzone.
if (potential_sparkle > Deadzone) {
_sparkle += potential_sparkle;
}
}
int Accelerometer::sparkle() {
return _sparkle;
}