-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVenderoo.ino
More file actions
436 lines (363 loc) · 12.5 KB
/
Copy pathVenderoo.ino
File metadata and controls
436 lines (363 loc) · 12.5 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// # Global Constants
#define N_SNACKS 8
#define TAU 6.2831853071
#define NUM_REVS 1.03 //it is better for it to turn too much than too little
#define IDLE_DURATION_BEFORE_RESET 600000 //10 minutes
#define password1 '1' //the password is not 1234. This is for demo purposes.
#define password2 '2'
#define password3 '3'
#define password4 '4'
double durationsOfRevolution[]={ //how long it takes different springs to turn
3350,
3343,
3342,
3520,
3071,
3173,
3070,
3300
};
const String snackNames[]={
"Dried Mangos",
"Icecream Sandwich",
"Mushroom Jerky",
"Seaweed",
"Ruffles",
"Goldfish",
"Pringles",
"Oreos"
};
const double snackPrices[]={
2.00,
5.00,
5.00,
1.00,
1.00,
1.00,
2.00,
1.00
};
// # Logic
double moneyInserted=0;
int selectedSnackIndex=-1;
unsigned long buttonLastPressed=0;
// # Liquid Crystal Display
#include <LiquidCrystal_I2C.h> //from github.com/johnrickman/LiquidCrystal_I2C
LiquidCrystal_I2C lcd(0x27, 20, 4); //20x4 display from amazon.com/gp/product/B01DKETWO2
// Pin 9–13
// # Keypad
#include <Keypad.h> //Mark Stanley's Keypad library
const uint8_t N_ROWS=4;
const uint8_t N_COLS=3;
char keys[N_ROWS][N_COLS]={
{ '1', '2', '3' },
{ '4', '5', '6' },
{ '7', '8', '9' },
{ '*', '0', '#' }
};
uint8_t rowPins[N_ROWS]={ 9, 8, 7, 6 };
uint8_t colPins[N_COLS]={ 5, 4, 3 };
Keypad keypad(makeKeymap(keys), rowPins, colPins, N_ROWS, N_COLS);
// # Dollar Acceptor
// Each pulse is configured to be one dollar. From https://github.com/mudmin/AnotherMaker/blob/master/arduino-powered-cash-reader/cash-reader-with-lcd.c
#define dollarPin 15 //blue pin. Purple pin is connected to ground
// It is HIGH when normal. When $ inserted, it beeps LOW for ~100 millis per dollar
uint8_t pulse=HIGH;
uint8_t prevPulse=HIGH;
// # DC Motor
const uint8_t motorPins[N_SNACKS]={ 23, 25, 27, 29, 31, 33, 35, 37 }; //23–37 odd numbers
void turnOnce(uint8_t motorI) { //turns the transistor controlling the spring motor just long enough for one revolution
Serial.print("Turning motor #"); Serial.println(motorI);
turnInRadians(motorI, TAU);
}
void turnInRadians(uint8_t motorI, double radians) {
double durationOfRevolution=durationsOfRevolution[motorI];
double delayPerRadian=durationOfRevolution/TAU;
turnInMS(motorI, delayPerRadian*radians);
}
void turnInMS(uint8_t motorI, double milliseconds) { //turn in milliseconds
const int transistorPin=motorPins[motorI];
digitalWrite(transistorPin, HIGH);
delay(milliseconds);
digitalWrite(transistorPin, LOW);
}
void startTurningMotor(uint8_t motorI) {
const int transistorPin=motorPins[motorI];
digitalWrite(transistorPin, HIGH);
}
void stopTurningMotor(uint8_t motorI) {
const int transistorPin=motorPins[motorI];
digitalWrite(transistorPin, LOW);
}
// # Log
#include "log.hpp"
void setup() {
Serial.begin(9600);
while (!Serial);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
// Bill Acceptor
pinMode(dollarPin, INPUT_PULLUP);
// DC Motor
for (uint8_t motorPin : motorPins)
pinMode(motorPin, OUTPUT);
// Log
initializeLog();
addLog("turned on");
for (int i=0; i<8; i++) //better that it goes too much than too little so that a student gets 2 snacks instead of 0
durationsOfRevolution[i]*=NUM_REVS;
}
void loop() {
// Keypad
char customKey=keypad.getKey(); //get value if keypad pressed
if (customKey) { //key pressed
buttonLastPressed=millis();
if (customKey=='0') { //press 0 to reset
reset();
displayBalance();
return;
}
if (customKey=='#') { //press # for control panel
Serial.println("Please enter the password to run an action");
if (!waitForNextKey(password1)) return;
if (!waitForNextKey(password2)) return;
if (!waitForNextKey(password3)) return;
if (!waitForNextKey(password4)) return;
controlPanel();
return;
}
int tempSelectedSnackIndex=int(customKey)-int('1'); //offset from 1
// customKey - one-based indexing
// selectedSnackIndex - zero-based indexing
// Show message
lcd.clear();
lcd.setCursor(0, 0);
if (tempSelectedSnackIndex>N_SNACKS-1 || tempSelectedSnackIndex<0) {
Serial.print(tempSelectedSnackIndex);
Serial.println(" is out of range");
addLog("out of range");
lcd.print("Out of range"); //validate customKey in range
return;
}
selectedSnackIndex=tempSelectedSnackIndex;
Serial.print("snackNames[selectedSnackIndex] and selectedSnackIndex: ");
Serial.print(snackNames[selectedSnackIndex]);
Serial.println(selectedSnackIndex);
String snackName=snackNames[selectedSnackIndex];
char message[50]; //buffer
sprintf(message, "%s (#%c)", snackName.c_str(), customKey);
lcd.print(message);
lcd.setCursor(0, 1);
lcd.print("costs $");
lcd.print(snackPrices[selectedSnackIndex]);
displayBalance();
Serial.println(message);
addLog("Selected ", snackName);
if (moneyInserted==0) {
lcd.setCursor(0, 2);
double snackPrice=snackPrices[selectedSnackIndex];
if (snackPrice==1.0)
lcd.print("Insert $1 bill");
else if (snackPrice==2.0 || snackPrice==3.0 || snackPrice==4.0)
lcd.print("Insert $1 bills");
else if (snackPrice>=5.0)
lcd.print("Insert $1 or $5 bill");
}
}
// Bill Acceptor
prevPulse=pulse;
pulse=digitalRead(dollarPin);
if (pulse==HIGH && prevPulse==LOW) {
buttonLastPressed=millis();
moneyInserted++;
displayBalance();
addLog("Inserted ", String(moneyInserted));
}
// Check if balance is high enough to buy a snack
if (moneyInserted!=0 && selectedSnackIndex!=-1) { //snack selected and money in
if (moneyInserted>=snackPrices[selectedSnackIndex]) { //check if your balance affords the price of the snack
turnOnce(selectedSnackIndex);
char message[60];
sprintf(message, "Bought %s (#%d)", snackNames[selectedSnackIndex].c_str(), selectedSnackIndex);
addLog(message);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Thank you. Enjoy the");
lcd.setCursor(0, 1);
lcd.print(snackNames[selectedSnackIndex]);
lcd.print("!");
moneyInserted=0;
selectedSnackIndex=-1;
delay(3000);
lcd.clear();
}
}
// Time out when money inserted and idled for too long
if (moneyInserted!=0 || selectedSnackIndex!=-1) { //snack selected or money in
if (millis()-buttonLastPressed>IDLE_DURATION_BEFORE_RESET) { //idle duration
Serial.println("Been inactive for too long");
moneyInserted=0;
reset();
}
}
}
// Utils
void reset() {
selectedSnackIndex=-1;
lcd.clear();
lcd.setCursor(0, 0);
}
void displayBalance() {
if (moneyInserted==0) return; //don't show a balance of 0
char message[50];
sprintf(message, "You inserted $%d", (int)moneyInserted);
lcd.setCursor(0, 2);
lcd.print(message);
}
bool waitForNextKey(char correctKey) { //2 seconds to press the key
char pressedKey;
for (uint8_t i=0; i<20; i++) {
delay(100);
pressedKey=keypad.getKey();
if (pressedKey==correctKey) {
Serial.print("Correct key: ");
Serial.println(correctKey);
return true;
}
}
Serial.println("Aborting because pressed wrong key");
return false;
}
void displayMode(char modeName[]) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("---Control Panel---");
lcd.setCursor(0, 1);
lcd.print(modeName);
lcd.print(" Mode");
}
int keyToMotorI(char key) {
switch (key) {
case '1': return 0;
case '2': return 1;
case '3': return 2;
case '4': return 3;
case '5': return 4;
case '6': return 5;
case '7': return 6;
case '8': return 7;
default: return -1;
}
}
void controlPanel() {
// 0 for print log
// 1–8 for turn a specific motor
// one rotation in NORMAL mode
// 1/10 rotation in MILLI mode
// 1/100 rotation in MICRO mode
// * for turn all motors
// # for exiting control panel
// 9 for change mode
// # for exit
displayMode("Normal");
enum Mode {
NORMAL,
DECI,
MILLI,
MICRO,
TEST_DURATION
};
Mode mode=NORMAL;
bool shiftPressed=false; //when pressed, small movements to motors
while (true) {
char pressedKey=keypad.getKey(); //get value if keypad pressed
delay(50);
if (mode==TEST_DURATION) {
int motorI=keyToMotorI(pressedKey);
if (motorI!=-1) {
Serial.println("Testing");
lcd.setCursor(0, 1);
lcd.print("Turned for ");
const int incrementAmount=100;
int milliseconds=0;
startTurningMotor(motorI);
long long startedTurningAt=millis();
lcd.setCursor(0, 2);
lcd.print("Press any key");
lcd.setCursor(0, 3);
lcd.print("to stop");
while (true) {
// Stopped when any key pressed
char pressedKey=keypad.getKey();
if (pressedKey) {
long long stoppedTurningAt=millis();
int duration=(int)(stoppedTurningAt-startedTurningAt);
displayMode("Test Duration");
lcd.setCursor(0, 2);
lcd.print("Finished");
lcd.setCursor(0, 3);
lcd.print(duration);
lcd.print(" ms");
stopTurningMotor(motorI);
break;
}
delay(20);
}
continue;
}
}
// Mode determines move amount
double moveAmount;
switch (mode) {
case NORMAL: moveAmount=TAU; break;
case DECI: moveAmount=TAU/4; break;
case MILLI: moveAmount=TAU/10; break;
case MICRO: moveAmount=TAU/100; break;
default: moveAmount=TAU; break;
}
// Perform action
switch (pressedKey) {
// Turn motor
case '1': turnInRadians(0, moveAmount); break;
case '2': turnInRadians(1, moveAmount); break;
case '3': turnInRadians(2, moveAmount); break;
case '4': turnInRadians(3, moveAmount); break;
case '5': turnInRadians(4, moveAmount); break;
case '6': turnInRadians(5, moveAmount); break;
case '7': turnInRadians(6, moveAmount); break;
case '8': turnInRadians(7, moveAmount); break;
// Print log to serial port if computer is connected
case '0':
Serial.println("Print log");
printLog();
break;
// Turn all springs
case '*':
Serial.print("Turning all springs from 1 to ");
Serial.println(N_SNACKS);
for (uint8_t i=0; i<N_SNACKS; i++)
turnOnce(i);
break;
// Change mode
case '9':
switch (mode) {
case NORMAL: mode=DECI; displayMode("Deci"); break;
case DECI: mode=MILLI; displayMode("Milli"); break;
case MILLI: mode=MICRO; displayMode("Micro"); break;
case MICRO: mode=TEST_DURATION; displayMode("Test Duration"); break;
case TEST_DURATION: mode=NORMAL; displayMode("Normal"); break;
}
break;
case '#':
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Exiting");
delay(600);
lcd.clear();
lcd.setCursor(0, 0);
return;
}
}
}