-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSlideBar_Firmware.ino
More file actions
300 lines (267 loc) · 6.56 KB
/
SlideBar_Firmware.ino
File metadata and controls
300 lines (267 loc) · 6.56 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <ResponsiveAnalogRead.h>
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
int dataNumber = 0; // new for this version
int motorOut1 = 3;
int motorOut2 = 2;
int motorOut3 = 9;
int motorOut4 = 10;
int motorEnable = 6;
String ID = "l1n117";
//String ID = "m1n11";
int parts;
boolean newPart;
boolean powerSlideEnabled;
ResponsiveAnalogRead analog(A6, true, 0.01);
void setup() {
pinMode(motorOut1, OUTPUT);
pinMode(motorOut2, OUTPUT);
pinMode(motorOut3, OUTPUT);
pinMode(motorOut4, OUTPUT);
pinMode(motorEnable, OUTPUT);
Serial.begin(115200);
Serial.print("");
}
void loop() {
recvWithEndMarker();
showNewNumber();
WriteBack();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = ']';
char rc;
if (Serial.available() > 0) {
rc = Serial.read();
// Serial.println(rc);
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewNumber() {
if (newData == true) {
dataNumber = 0; // new for this version
dataNumber = atoi(receivedChars); // new for this version
// Serial.print("This just in ...");
// Serial.println(receivedChars);
// Serial.print("Data as Number ... "); // new for this version
// Serial.println(dataNumber); // new for this version
if (dataNumber == 2424){
Serial.println(ID);
}
if (dataNumber > 6000 && dataNumber < 7000) {
vibrate(dataNumber - 6000);
}
// if (temp > 5000 && temp < 5500){
// scrollUp(temp - 5000, 0);
// }
// if (temp > 5500 && temp < 6000){
// scrollDown(temp - 5500, 0);
// }
if (dataNumber > 3000 && dataNumber < 4000) {
bumpLeft(dataNumber - 3000);
}
if (dataNumber > 4000 && dataNumber < 5000) {
bumpRight(dataNumber - 4000);
}
if (dataNumber == 2000) {
newPart = false;
parts = 0;
}
if (dataNumber > 2000 && dataNumber < 2051) {
parts = dataNumber - 2000;
newPart = true;
} else {
if (dataNumber < 1025) {
if (dataNumber > -1) {
MoveToNumber(dataNumber, 8);
}
}
}
newData = false;
}
AvoidParts();
}
void MoveToNumber(int goal, int threshold) {
analog.update();
int val = analog.getValue();
boolean toggle = true;
while (abs(val - goal) > threshold + 50 && toggle && !Serial.available() > 0) {
analog.update();
val = analog.getValue();
printBack(val);
if (val > goal) {
goRight();
} else {
goLeft();
}
toggle = calculateNewValue(goal);
}
while (abs(val - goal) > threshold + 20 && toggle && !Serial.available() > 0) {
analog.update();
val = analog.getValue();
printBack(val);
if (val > goal) {
pwmGoRight(230);
} else {
pwmGoLeft(230);
}
toggle = calculateNewValue(goal);
}
while (abs(val - goal) > threshold && toggle && !Serial.available() > 0) {
analog.update();
val = analog.getValue();
printBack(val);
if (val > goal) {
pwmGoRight(200);
} else {
pwmGoLeft(200);
}
toggle = calculateNewValue(goal);
}
stop();
}
boolean calculateNewValue(int given) {
if (dataNumber > 0 && dataNumber < 1023) {
if (dataNumber != given) {
return false;
}
}
return true;
}
int sensorValue = 0;
void AvoidParts() {
if (newPart == true) {
int partSize = 1040 / parts;
int difference = 450 / parts;
analog.update();
sensorValue = analog.getValue();
for (int i = 0; i < parts + 1; i++) {
if (abs(analogRead(A6) - partSize * i) < difference) {
if (analogRead(A6) + 1 < partSize * i) {
while (analogRead(A6) + 1 < partSize * i && abs(analogRead(A6) - partSize * i) < difference) {
pwmGoRight(230);
WriteBack();
}
} else if (analogRead(A6) + 1 > partSize * i) {
while (analogRead(A6) + 1 > partSize * i && abs(analogRead(A6) - partSize * i) < difference) {
pwmGoLeft(230);
WriteBack();
}
}
delay(2);
stop();
}
}
}
}
long previousMillis = 0;
long previouspreMillis = 0;
long previousSlideMillis = 0;
long interval = 4;
int previousSensorValue = -1;
void WriteBack() {
long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
analog.update();
sensorValue = analog.getValue();
// sensorValue = map(sensorValue, 0, 780, 0, 1024);
if (previousSensorValue != sensorValue) {
powerSlide(previousSensorValue - sensorValue);
previousSensorValue = sensorValue;
printBack(sensorValue);
previouspreMillis = currentMillis;
previousSlideMillis = currentMillis;
}
}
if (currentMillis - previouspreMillis > interval * 200) {
previouspreMillis = currentMillis;
// Serial.print(" {SB1}");
}
if (currentMillis - previousSlideMillis > interval * 20) {
previousSlideMillis = currentMillis;
if (powerSlideEnabled) {
stop();
}
}
}
void printBack(int value) {
Serial.println(value);
// Serial.print("|");
// if (value < 1000) {
// Serial.print("0");
// }
// if (value < 100) {
// Serial.print("0");
// }
// if (value < 10) {
// Serial.print("0");
// }
// Serial.print(value);
// Serial.print(">");
}
void powerSlide(int amount) {
if (powerSlideEnabled) {
if (amount > 2) {
pwmGoRight(100);
}
if (amount < -2) {
pwmGoLeft(100);
}
}
}
void pwmGoLeft(int pwm) {
analogWrite(motorEnable, pwm);
digitalWrite(motorOut1, LOW);
digitalWrite(motorOut2, HIGH);
digitalWrite(motorOut3, LOW);
digitalWrite(motorOut4, HIGH);
}
void goRight() {
pwmGoRight(255);
}
void pwmGoRight(int pwm) {
analogWrite(motorEnable, pwm);
digitalWrite(motorOut1, HIGH);
digitalWrite(motorOut2, LOW);
digitalWrite(motorOut3, HIGH);
digitalWrite(motorOut4, LOW);
}
void goLeft() {
pwmGoLeft(255);
}
void stop() {
digitalWrite(motorEnable, LOW);
digitalWrite(motorOut1, HIGH);
digitalWrite(motorOut2, HIGH);
digitalWrite(motorOut3, HIGH);
digitalWrite(motorOut4, HIGH);
}
void bumpRight(int amount) {
goRight();
delay(amount);
stop();
}
void bumpLeft(int amount) {
goLeft();
delay(amount);
stop();
}
void vibrate(int amount) {
for (int i = 0; i < amount; i++) {
bumpLeft(15);
bumpRight(15);
}
}