Members-Only Vending Machine with Customer ID and Automatic Payment
Please keep in mind that this is a prototype, and I'm making an official version to install in my school cafeteria.
Components and supplies
1
Arduino Uno Rev3
1
4GB or Larger Micro SD Card
1
I2C LCD 20x4
4
DS04-NFC 360 degree continuously rotating DC servo
1
SD card module
1
RFID MFRC-522 Module
1
Arduino Mega 2560 Rev3
4
Push Button
Tools and machines
1
Glue Gun and Hot Glue
1
metal string
1
Acrylic
Apps and platforms
1
Arduino IDE
Project description
Code
Code to upload to Uno
cpp
Reading the UID and sending it to Mega
1#include <SPI.h> 2#include <MFRC522.h> 3 4int RST_PIN = 9; 5int SS_PIN = 10; 6 7String UID; 8 9MFRC522 mfrc522(SS_PIN, RST_PIN); 10 11void setup() { 12 Serial.begin(9600); 13 SPI.begin(); 14 mfrc522.PCD_Init(); 15 Serial.write(","); 16} 17 18void loop() { 19 if (!mfrc522.PICC_IsNewCardPresent()) { 20 return; 21 } 22 else if (!mfrc522.PICC_ReadCardSerial()) { 23 return; 24 } 25 for (int i=0; i < mfrc522.uid.size; i++) { 26 UID += (mfrc522.uid.uidByte[i] < 0x10 ? "0" : ""); 27 UID += String(mfrc522.uid.uidByte[i], HEX); 28 } 29 for (int i=0; i < UID.length(); i++) { 30 Serial.write(char(UID[i])); 31 } 32 UID = ""; 33 delay(5000); 34}
Code to upload to Mega
cpp
AFTER receiving the UID from Uno
1#include <Servo.h> 2#include <LiquidCrystal_I2C.h> 3#include <SPI.h> 4#include <SD.h> 5 6int CS_PIN = 53; 7int redButton = 22; 8int yellowButton = 24; 9int greenButton = 26; 10int blueButton = 28; 11 12LiquidCrystal_I2C lcd(0x27, 20, 4); 13 14int redServoPin = 8; 15int yellowServoPin = 9; 16int greenServoPin = 10; 17int blueServoPin = 11; 18 19bool redButtonPressed = 0; 20bool yellowButtonPressed = 0; 21bool greenButtonPressed = 0; 22bool blueButtonPressed = 0; 23 24Servo myServo; 25char data; 26String UID; 27File myFile; 28File prevFile; 29bool commStart; 30String checkUID; 31double preTime; 32bool servoTurned = 0; 33 34void setup() { 35 SPI.begin(); 36 Serial.begin(9600); 37 lcd.init(); 38 lcd.backlight(); 39 pinMode(redServoPin, OUTPUT); 40 pinMode(yellowServoPin, OUTPUT); 41 pinMode(greenServoPin, OUTPUT); 42 pinMode(blueServoPin, OUTPUT); 43 if (!SD.begin(CS_PIN)) { 44 Serial.println("Initialization failed!"); 45 while (1); 46 } 47 Serial.println("Initialization succeeded!"); 48} 49 50void loop() { 51 servoTurned = 0; 52 if (Serial.available()) { 53 data = Serial.read(); 54 UID += data; 55 Serial.println(data); 56 } 57 if (UID.length() == 8) { 58 if (UID.lastIndexOf(",") >= 0) { 59 UID = UID.substring(UID.lastIndexOf(",")+1); 60 for (int i=0; i<=UID.lastIndexOf(","); i++) { 61 data = Serial.read(); 62 UID += data; 63 } 64 } 65 Serial.end(); 66 Serial.begin(9600); 67 Serial.print("UID: "); 68 Serial.println(UID); 69 checkUID = CheckUID(UID); 70 if (checkUID == "not registered") { 71 Serial.println("void loop: not registered"); 72 UID =""; 73 return; 74 } 75 else if (checkUID == "recharge") { 76 Serial.println("void loop: recharge"); 77 UID =""; 78 return; 79 } 80 lcd.clear(); 81 lcd.print("Choose a snack..."); 82 preTime = millis(); 83 Serial.println(preTime); 84 while (millis() - preTime < 10000) { 85 Serial.println(millis() - preTime); 86 if (digitalRead(redButton)) { 87 myServo.attach(redServoPin); 88 ServoAction("Red"); 89 servoTurned = 1; 90 lcd.clear(); 91 break; 92 } 93 else if (digitalRead(yellowButton)) { 94 myServo.attach(yellowServoPin); 95 ServoAction("Yellow"); 96 servoTurned = 1; 97 lcd.clear(); 98 break; 99 } 100 else if (digitalRead(greenButton)) { 101 myServo.attach(greenServoPin); 102 ServoAction("Green"); 103 servoTurned = 1; 104 lcd.clear(); 105 break; 106 } 107 else if (digitalRead(blueButton)) { 108 myServo.attach(blueServoPin); 109 ServoAction("Blue"); 110 servoTurned = 1; 111 lcd.clear(); 112 break; 113 } 114 else { 115 continue; 116 } 117 } 118 lcd.clear(); 119 if (servoTurned == 0) { 120 lcd.println("Paused too long."); 121 delay(1000); 122 lcd.clear(); 123 lcd.println("Have a nice day!"); 124 delay(2000); 125 lcd.clear(); 126 UID = ""; 127 return; 128 } 129 DecreaseCount(checkUID); 130 UID = ""; 131 return; 132 } 133} 134 135String CheckUID(String UID) { 136 Serial.println(UID); 137 myFile = SD.open("SA_cards.csv"); 138 String contents = ""; 139 bool uidExists = 0; 140 if (myFile) { 141 Serial.println(myFile.available()); 142 while (myFile.available()) { 143 char character = (char)myFile.read(); 144 contents += character; 145 if (character == '\n') { 146 Serial.println(contents.substring(contents.lastIndexOf(',') + 1).length()); 147 Serial.println(UID); 148 if (contents.substring(contents.lastIndexOf(',') + 1, contents.lastIndexOf(',') + 9).equals(UID)) { 149 uidExists = 1; 150 break; 151 } 152 else { 153 contents = ""; 154 } 155 } 156 } 157 Serial.println("While loop ended"); 158 if (uidExists == false) { 159 lcd.clear(); 160 lcd.setCursor(0,0); 161 lcd.print("Not a registered"); 162 lcd.setCursor(0,2); 163 lcd.print("card"); 164 delay(2000); 165 lcd.setCursor(0,1); 166 lcd.clear(); 167 return "not registered"; 168 } 169 myFile.close(); 170 Serial.println(contents); 171 } 172 else { 173 lcd.clear(); 174 lcd.print("error opening SA_cards.csv"); 175 } 176 char items[][10] = {}; 177 int startIndex = 0; 178 int endIndex = 0; 179 int itemNum = -1; 180 while (true) { 181 endIndex += 1; 182 if (contents.charAt(endIndex) == '\n' || contents.charAt(endIndex) == ',') { 183 itemNum += 1; 184 contents.substring(startIndex, endIndex).toCharArray(items[itemNum],sizeof(items[itemNum])); 185 Serial.println(String(items[itemNum])); 186 startIndex = endIndex + 1; 187 } 188 if (endIndex==contents.length()-1) { 189 Serial.println("Items setting successful"); 190 break; 191 } 192 } 193 Serial.println(itemNum); 194 for (int i=0; i <= itemNum; i++) { 195 Serial.println("New item:"); 196 Serial.println(String(items[i])); 197 } 198 199 String name = items[0]; 200 int payCount = String(items[2]).toInt(); 201 Serial.print("payCount: "); 202 Serial.println(payCount); 203 lcd.clear(); 204 lcd.print("Hi " + name); 205 delay(2000); 206 if (payCount > 0) { 207 return contents; 208 } 209 else { 210 lcd.clear(); 211 lcd.print("Recharge your card"); 212 delay(2000); 213 lcd.clear(); 214 return "recharge"; 215 } 216} 217 218void ServoAction(String color) { 219 Serial.print(color); 220 Serial.println(" pressed"); 221 lcd.clear(); 222 lcd.print(color); 223 lcd.print(" pressed"); 224 225 myServo.write(60); 226 delay(1830); 227 myServo.detach(); 228 delay(500); 229 lcd.clear(); 230} 231 232void DecreaseCount(String contents) { 233 Serial.begin(9600); 234 prevFile = SD.open("SA_cards.csv"); 235 String prevFileContents = ""; 236 if (prevFile) { 237 while (prevFile.available()) { 238 char data = (char)prevFile.read(); 239 prevFileContents += data; 240 } 241 } 242 prevFile.close(); 243 Serial.println(prevFileContents); 244 int startIndex = 0; 245 int endIndex = 0; 246 while (true) { 247 endIndex += 1; 248 if (prevFileContents.charAt(endIndex) == '\n') { 249 String piece = prevFileContents.substring(startIndex, endIndex+1); 250 if (piece == contents) { 251 piece = piece.substring(0, piece.length()-11); 252 int payCount = piece.substring(piece.lastIndexOf(',')+1).toInt(); 253 payCount--; 254 piece = piece.substring(0, piece.lastIndexOf(',')); 255 contents = piece + ',' + payCount + ',' + contents.substring(contents.length()-10); 256 prevFileContents = prevFileContents.substring(0, prevFileContents.substring(0, endIndex).lastIndexOf('\n')+1) + contents + prevFileContents.substring(endIndex+1); 257 break; 258 } 259 startIndex = endIndex + 1; 260 endIndex += 1; 261 } 262 } 263 Serial.println("test"); 264 Serial.println(prevFileContents); 265 myFile = SD.open("SA_cards.csv", FILE_WRITE | O_TRUNC); 266 myFile.print(prevFileContents); 267 myFile.close(); 268}
Comments
Only logged in users can leave comments