|
1 | 1 | #if 1 // Change to 0 to disable this code (must enable ONE user*.cpp only!) |
2 | 2 |
|
| 3 | +#include "globals.h" |
| 4 | + |
3 | 5 | // This file provides a crude way to "drop in" user code to the eyes, |
4 | 6 | // allowing concurrent operations without having to maintain a bunch of |
5 | 7 | // special derivatives of the eye code (which is still undergoing a lot |
|
9 | 11 | // User globals can go here, recommend declaring as static, e.g.: |
10 | 12 | // static int foo = 42; |
11 | 13 |
|
| 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 | + |
12 | 31 | // Called once near the end of the setup() function. If your code requires |
13 | 32 | // a lot of time to initialize, make periodic calls to yield() to keep the |
14 | 33 | // USB mass storage filesystem alive. |
15 | 34 | void user_setup(void) { |
| 35 | + end_millis = millis() + sampleWindow; |
16 | 36 | } |
17 | 37 |
|
18 | 38 | // Called periodically during eye animation. This is invoked in the |
@@ -64,6 +84,60 @@ void user_loop(void) { |
64 | 84 | } |
65 | 85 | } |
66 | 86 | */ |
| 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 | + } |
67 | 141 | } |
68 | 142 |
|
69 | 143 | #endif // 0 |
0 commit comments