-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer_fix.ino
More file actions
189 lines (155 loc) · 4.92 KB
/
buffer_fix.ino
File metadata and controls
189 lines (155 loc) · 4.92 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* serious shit */
//Random compiler stuff
//int serialData;
//#include <SoftwareSerial.h>
//SoftwareSerial impSerial(5, 7); // RX on 5, TX on 7
// pin numbers
const int x = A3; //white
const int y = A2; //red
const int z = A1; //black
//int light = A5;//fix later please ***************************************************************
//int lightVal = 0;
int serialData = 0;
// constants (could be changed)
const int buffsize = 20;
const int numcap = 500;
// thresholds to be tinkered with
const int threshold = 150;
const int closeEnoughThreshold = 10; // TOTALLY MADE THIS UP
// are we reading?
boolean reading = false;
// iterator
int numloop = 0;
// holds the data we have collected
int buffers[3][buffsize];
// holding extremas
int mins[] = {0, 0, 0};
int maxs[] = {0, 0, 0};
// not really sure...
int oldValues[] = {0, 0, 0};
// shifts buffer and queues a new element
void shiftBuff(int front, int buffer[]) {
for (int i = buffsize - 1; i > 0; i--) {
buffer[i] = buffer[i - 1];
}
buffer[0] = front;
}
// takes weighted average of a buffer
int weightedAvg(int buffer[]) {
double weighted = 0;
int j = buffsize;
for (int i = 0; i < buffsize; i++) {
weighted = weighted + (buffer[i] * j);
j--;
}
return (int) (weighted / 210);
}
// find the Reimenn Sum
int riemSum(int a, int b) {
return (a + b) / 2;
}
// if val is more extreme, it becomes the number one champion
void updateExtrema(int val, int axis) {
if (val > maxs[axis]) maxs[axis] = val;
if (val < mins[axis]) mins[axis] = val;
}
// if we've reached a severity of movements, return true
boolean reachedThresh(int axis) {
Serial.print("here");
return maxs[axis] - mins[axis] >= threshold;
}
/*int encodeGesture(int x[]){
return 1 * (x[0] + 1) + 10 * (x[1] + 1) + 100 * (x[2] + 1);
}*/
// to be done later.
void sendToImp(int data[]){
// TODO @Max: implement me
Serial.print(data[0]);
Serial.print(",");
Serial.print(data[1]);
Serial.print(",");
Serial.print(data[2]);
Serial.println("");
// Send data from the hardware serial
/*if (Serial.available())
impSerial.write(encodeGesture(data)); // to the software serial*/
}
// check if we've almost reached a maximum
boolean closeEnough(int axis) {
return (maxs[axis] - mins[axis]) >= threshold - closeEnoughThreshold;
}
//boolean litUp() {
//return lightVal < 50;
//}
void setup() {
Serial.begin(9600);
// Open the hardware serial port
//Serial.begin(19200);
// set the data rate for the SoftwareSerial port
//impSerial.begin(19200);
}
void loop() {
//lightVal = analogRead(light);
//if (litUp()) {
numloop = 0;
//}
if (0 < Serial.available()) {
serialData = Serial.read();
if (serialData == 98) {
reading = true;
}
else if (serialData == 101) {
reading = false;
// Serial.println("END");
}
}
if (reading && numloop < numcap) {
int trap[3];
int new_data[] = { analogRead(x), analogRead(y), analogRead(z) };
for (int i = 0; i < 3; i++) {
int old_value = weightedAvg(buffers[i]);
shiftBuff(new_data[i], buffers[i]);
trap[i] = riemSum(old_value, weightedAvg(buffers[i]));
Serial.print("trap ");
Serial.print(trap[i]);
// reset when 10
if (numloop == 10) {
mins[i] = trap[i];
maxs[i] = trap[i];
} else if (numloop > 10) {
int dir[] = { 0, 0, 0 };
dir[i] = trap[i] - oldValues[i] > 0 ? 1 :
trap[i] - oldValues[i] < 0 ? -1 :
0 ;
updateExtrema(trap[i], i);
// if we reached a threshold...
if (reachedThresh(i)) {
Serial.print("here");
for (int j = 0; j < 3; j++) {
if (closeEnough(j)) dir[j] = trap[j] - oldValues[j] > 0 ? 1 :
trap[j] - oldValues[j] < 0 ? -1 :
0 ;
}
// send to the interwebs
sendToImp(dir);
for (int j = 0; j < 3; j++) {
if (dir[j] == 1) {
mins[j] = trap[j];
} else {
maxs[j] = trap[j];
}
}
}
}
}
//give to csv file
if (numloop < numcap - 10 && numloop > 10) {
// Set laggging values
oldValues[0] = trap[0];
oldValues[1] = trap[1];
oldValues[2] = trap[2];
}
numloop++;
delay(10);
}
}