-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino.ino
More file actions
353 lines (303 loc) · 9.48 KB
/
Arduino.ino
File metadata and controls
353 lines (303 loc) · 9.48 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
#include <HX711_ADC.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
// ---------------------- LCD ----------------------
LiquidCrystal_I2C lcd(0x27, 16, 2);
// ---------------------- Pins ----------------------
const int acceptRejectServoPin = 10;
const int coinServoPin = 6;
const int startSaveButtonPin = 2;
const int claimButtonPin = 11;
const int cancelButtonPin = 3;
const int irPin = 7;
// ---------------------- Servos ----------------------
Servo acceptRejectServo;
Servo coinServo;
// ---------------------- QR Scanner ----------------------
SoftwareSerial qrSerial(8, 9); // RX=8, TX=9 for QR scanner
// ---------------------- HX711 ----------------------
HX711_ADC LoadCell(12, 13);
float calibrationFactor = 1000.0;
// ---------------------- CAPACITIVE + INDUCTIVE ----------------------
const int inductivePin = 4;
const int SIG = A0;
const int DIG = A2;
// ---------------------- State ----------------------
String qrData = "";
bool collecting = false;
bool waitingForQR_Save = false;
bool waitingForQR_Claim = false;
bool waitingForStable = false;
unsigned long stableStartTime = 0;
const unsigned long stabilizationTime = 3000UL;
int bottleCount = 0;
bool proceedPrinted = false;
// ---------------- SENSOR BLOCK SETTINGS ----------------
const unsigned long inductiveWaitTime = 3000;
const unsigned long stableDelay = 200;
const unsigned long weightStabilization = 2000;
bool metalState = false;
// ---------------------- LCD Helper ----------------------
void lcdPrint(String l1, String l2 = "") {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(l1);
lcd.setCursor(0, 1);
lcd.print(l2);
}
// ---------------------- Buttons ----------------------
bool buttonPressed(int pin) {
if (digitalRead(pin) == LOW) {
delay(30);
return digitalRead(pin) == LOW;
}
return false;
}
// ---------------------- QR Extract ----------------------
String extractID(String raw) {
raw.trim();
int pos = raw.lastIndexOf('=');
if (pos != -1 && pos + 1 < raw.length())
return raw.substring(pos + 1);
return raw;
}
// Non-blocking QR scanner
bool scanQRCodeOnce() {
while (qrSerial.available()) {
char c = qrSerial.read();
if (c == '\n' || c == '\r') {
if (qrData.length() > 0) {
qrData.trim();
qrData = extractID(qrData);
Serial.print("QR_CODE:");
Serial.println(qrData);
lcdPrint("QR Received", qrData);
qrData = "";
return true;
}
} else {
qrData += c;
}
}
return false;
}
// ---------------------- Servo Reward ----------------------
void dispenseMoves(int moves, bool waitBefore = false) {
if (moves <= 0) return;
if (waitBefore) {
lcdPrint("Processing...");
delay(2000);
}
for (int i = 0; i < moves; i++) {
coinServo.write(180);
delay(400);
coinServo.write(0);
delay(400);
}
lcdPrint("Reward", "Dispensed!");
delay(1000);
}
// ---------------------- ESP32 Messages ----------------------
void handleSerialFromESP() {
while (Serial.available()) {
String line = Serial.readStringUntil('\n');
line.trim();
if (line.startsWith("DISPENSE:")) {
int moves = line.substring(9).toInt();
dispenseMoves(moves);
}
// Corrected CLAIM_MOVES handling
else if (line.startsWith("CLAIM_MOVES:")) {
// Extract the number of moves from the line, starting after "CLAIM_MOVES:"
int moves = line.substring(12).toInt();
if (moves == 0) {
Serial.println("Insufficient points!");
lcdPrint("Need more", "points"); // Updated LCD message for clarity
delay(2000);
return;
}
// If moves > 0 (5, 6, 7, 8, 9, or 10):
Serial.print("Servo moves for redemption: "); // Corrected Serial Output
Serial.println(moves);
lcdPrint("Redeeming", "rewards..."); // Corrected LCD message
// This is the function that physically moves the servo 'moves' times
dispenseMoves(moves, true);
}
}
}
// ---------------------- Setup ----------------------
void setup() {
pinMode(startSaveButtonPin, INPUT_PULLUP);
pinMode(claimButtonPin, INPUT_PULLUP);
pinMode(cancelButtonPin, INPUT_PULLUP);
pinMode(irPin, INPUT_PULLUP);
pinMode(inductivePin, INPUT);
pinMode(DIG, INPUT);
acceptRejectServo.attach(acceptRejectServoPin);
coinServo.attach(coinServoPin);
acceptRejectServo.write(90);
coinServo.write(0);
Serial.begin(9600);
qrSerial.begin(9600);
lcd.init();
lcd.backlight();
lcdPrint("Initializing...");
LoadCell.begin();
LoadCell.start(2000);
LoadCell.setCalFactor(calibrationFactor);
lcdPrint("System Ready");
Serial.println("System Ready");
}
// ==================================================
// WEIGHT SENSOR PROCESS
// ==================================================
void runWeightProcess() {
Serial.println("⚡ Stabilizing weight...");
unsigned long startTime = millis();
float stableWeight = 0;
int samples = 0;
while (millis() - startTime < weightStabilization) {
LoadCell.update();
float w = LoadCell.getData();
stableWeight += w;
samples++;
delay(100);
}
stableWeight /= samples;
Serial.print("Final Weight: ");
Serial.print(stableWeight, 2);
Serial.println(" g");
if (stableWeight >= 35 && stableWeight <= 80) {
Serial.println("✅ Bottle accepted");
lcdPrint("Bottle OK", "Count: " + String(++bottleCount));
acceptRejectServo.write(0);
delay(600);
acceptRejectServo.write(90);
}
else if (stableWeight < 34) {
Serial.println("❌ Too light, rejected");
lcdPrint("Rejected", "Too Light");
acceptRejectServo.write(180);
delay(600);
acceptRejectServo.write(90);
}
else {
Serial.println("⚠️ Too heavy or invalid");
lcdPrint("Rejected", "Too Heavy");
acceptRejectServo.write(180);
delay(600);
acceptRejectServo.write(90);
}
Serial.println("Waiting for object removal...");
while (LoadCell.getData() > 5) LoadCell.update();
Serial.println("🟡 Object removed. Ready.");
}
// ---------------------- Loop ----------------------
void loop() {
LoadCell.update();
handleSerialFromESP();
// -------- CANCEL --------
if (buttonPressed(cancelButtonPin)) {
if (!collecting && !waitingForQR_Save && !waitingForQR_Claim) {
Serial.println("No transactions yet");
lcdPrint("No Transaction", "Yet");
delay(2000);
lcdPrint("System Ready");
return;
}
Serial.println("Cancelling Process...");
lcdPrint("Cancelling", "Process...");
delay(2000);
collecting = false;
waitingForStable = false;
waitingForQR_Save = false;
waitingForQR_Claim = false;
qrData = "";
bottleCount = 0;
proceedPrinted = false;
lcdPrint("System Ready");
delay(300);
}
// -------- START / SAVE --------
if (!waitingForQR_Claim) {
if (buttonPressed(startSaveButtonPin) && !collecting) {
collecting = true;
bottleCount = 0;
Serial.println("START");
lcdPrint("Insert bottles");
delay(300);
}
else if (buttonPressed(startSaveButtonPin) && collecting && !waitingForQR_Save) {
qrData = "";
waitingForQR_Save = true;
Serial.print("TOTAL_BOTTLES:");
Serial.println(bottleCount);
Serial.println("SAVE");
lcdPrint("Scan QR", "to Save");
delay(300);
}
}
// ---------------------- BOTTLE DETECTION ----------------------
if (collecting && !waitingForQR_Save) {
if (digitalRead(irPin) == LOW) {
Serial.println("Object Detected");
lcdPrint("Object Detected", "");
delay(100);
metalState = false;
unsigned long startCheck = millis();
unsigned long lastHighTime = millis();
while (millis() - startCheck < inductiveWaitTime) {
int reading = digitalRead(inductivePin);
unsigned long t = millis();
if (reading == HIGH) lastHighTime = t;
if (!metalState && (t - lastHighTime) >= stableDelay) {
Serial.println("Rejected, object is metal");
lcdPrint("REJECTED", "Metal");
acceptRejectServo.write(180);
delay(600);
acceptRejectServo.write(90);
return;
}
}
Serial.println("No Metal – Proceed to Weighing...");
lcdPrint("Checking Weight");
runWeightProcess();
delay(500);
}
}
///------------ QR SAVE -------------
if (waitingForQR_Save && scanQRCodeOnce()) {
waitingForQR_Save = false;
collecting = false;
// RESET ALL IMPORTANT VALUES
qrData = "";
bottleCount = 0;
proceedPrinted = false;
lcdPrint("Saving...");
delay(3000);
Serial.println("System Ready");
lcdPrint("System Ready");
}
// -------- QR CLAIM --------
if (waitingForQR_Claim && scanQRCodeOnce()) {
waitingForQR_Claim = false;
// RESET ALL IMPORTANT VALUES
qrData = "";
proceedPrinted = false;
lcdPrint("Processing...");
delay(1500);
Serial.println("System Ready");
lcdPrint("System Ready");
}
// -------- CLAIM BUTTON --------
if (!collecting && !waitingForQR_Save) {
if (buttonPressed(claimButtonPin) && !waitingForQR_Claim) {
qrData = "";
waitingForQR_Claim = true;
Serial.println("CLAIM");
lcdPrint("Scan QR", "to Claim");
delay(300);
}
}
}