Skip to content

Commit cfb187f

Browse files
committed
Add ear code
1 parent deb0c50 commit cfb187f

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

M4_Eyes/user.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#if 1 // Change to 0 to disable this code (must enable ONE user*.cpp only!)
22

3+
#include "globals.h"
4+
35
// This file provides a crude way to "drop in" user code to the eyes,
46
// allowing concurrent operations without having to maintain a bunch of
57
// special derivatives of the eye code (which is still undergoing a lot
@@ -9,10 +11,28 @@
911
// User globals can go here, recommend declaring as static, e.g.:
1012
// static int foo = 42;
1113

14+
const int sampleWindow = 400; // Sample window width in mS (50 mS = 20Hz)
15+
16+
unsigned long end_millis= 0; // Start of sample window
17+
18+
unsigned int left_max = 0;
19+
unsigned int left_min = 1024;
20+
21+
unsigned int right_max = 0;
22+
unsigned int right_min = 1024;
23+
24+
unsigned int sample_count = 0;
25+
unsigned int sample;
26+
unsigned int peakToPeak;
27+
double volts;
28+
29+
30+
1231
// Called once near the end of the setup() function. If your code requires
1332
// a lot of time to initialize, make periodic calls to yield() to keep the
1433
// USB mass storage filesystem alive.
1534
void user_setup(void) {
35+
end_millis = millis() + sampleWindow;
1636
}
1737

1838
// Called periodically during eye animation. This is invoked in the
@@ -64,6 +84,60 @@ void user_loop(void) {
6484
}
6585
}
6686
*/
87+
88+
89+
if (millis() >= end_millis) {
90+
// process the current sampling
91+
if (left_max > left_min && right_max > right_min) {
92+
93+
// process left
94+
peakToPeak = left_max - left_min; // max - min = peak-peak amplitude
95+
volts = (peakToPeak * 5.0) / 1024; // convert to volts
96+
Serial.print("Left: ");
97+
Serial.println(volts);
98+
if (volts > 1.0) {
99+
Serial.println("I hear you on the left");
100+
}
101+
// process right
102+
peakToPeak = right_max - right_min; // max - min = peak-peak amplitude
103+
volts = (peakToPeak * 5.0) / 1024; // convert to volts
104+
Serial.print("Right ");
105+
Serial.println(volts);
106+
if (volts > 1.0) {
107+
Serial.println("I hear you on the right");
108+
}
109+
} else {
110+
Serial.print("Not enough sampling: ");
111+
Serial.println(sample_count);
112+
}
113+
// and reset for the next sampling
114+
right_max = left_max = 0;
115+
right_min = left_min = 1024;
116+
end_millis = millis() + sampleWindow;
117+
sample_count = 0;
118+
} else {
119+
for (int i = 5; i > 0; i--) {
120+
sample_count++;
121+
// sample left
122+
sample = analogRead(2);
123+
if (sample < 1024) { // toss out spurious readings
124+
if (sample > left_max) {
125+
left_max = sample; // save just the max levels
126+
} else if (sample < left_min) {
127+
left_min = sample; // save just the min levels
128+
}
129+
}
130+
// sample right
131+
sample = analogRead(3);
132+
if (sample < 1024) { // toss out spurious readings
133+
if (sample > right_max) {
134+
right_max = sample; // save just the max levels
135+
} else if (sample < right_min) {
136+
right_min = sample; // save just the min levels
137+
}
138+
}
139+
}
140+
}
67141
}
68142

69143
#endif // 0

0 commit comments

Comments
 (0)