Components and supplies
Arduino UNO
RGB LCD Shield Kit, 16x2 Character Display
Grove - 12-Channel Capacitive Touch Keypad (ATtiny1616)
RFID reader (generic)
Project description
Code
Code for Vending Machine
arduino
Code
1/3// Include required libraries 2#include <MFRC522.h> 3#include <LiquidCrystal_I2C.h> 4#include 5 <Keypad.h> 6#include <Servo.h> 7#include <SPI.h> 8 9// Create instances 10LiquidCrystal_I2C 11 lcd(0x27, 16, 2); 12MFRC522 mfrc522(10, 9); // MFRC522 mfrc522(SS_PIN, RST_PIN) 13Servo 14 sg180; 15 16// Initialize Pins for led's, servo and buzzer 17// Blue LED is connected 18 to 5V 19constexpr uint8_t greenLed = 7; 20constexpr uint8_t redLed = 6; 21constexpr 22 uint8_t servoPin = 8; 23constexpr uint8_t buzzerPin = 5; 24 25char initial_password[4] 26 = {'1', '2', '3', '4'}; // Variable to store initial password 27String tagUID 28 = "B3 05 F6 16"; // String to store UID of tag. Change it with your tag's UID 29char 30 password[4]; // Variable to store users password 31boolean RFIDMode = true; // 32 boolean to change modes 33char key_pressed = 0; // Variable to store incoming keys 34uint8_t 35 i = 0; // Variable used for counter 36 37// defining how many rows and columns 38 our keypad have 39const byte rows = 4; 40const byte columns = 4; 41 42// Keypad 43 pin map 44char hexaKeys[rows][columns] = { 45 {'1', '2', '3', 'A'}, 46 {'4', 47 '5', '6', 'B'}, 48 {'7', '8', '9', 'C'}, 49 {'*', '0', '#', 'D'} 50}; 51 52// 53 Initializing pins for keypad 54byte row_pins[rows] = {A0, A1, A2, A3}; 55byte 56 column_pins[columns] = {2, 1, 0}; 57 58// Create instance for keypad 59Keypad 60 keypad_key = Keypad( makeKeymap(hexaKeys), row_pins, column_pins, rows, columns); 61 62void 63 setup() { 64 // Arduino Pin configuration 65 pinMode(buzzerPin, OUTPUT); 66 67 pinMode(redLed, OUTPUT); 68 pinMode(greenLed, OUTPUT); 69 70 sg180.attach(servoPin); 71 //Declare pin 8 for servo 72 sg180.write(0); // Set initial position at 90 degrees 73 74 75 lcd.init(); // LCD screen 76 lcd.backlight(); 77 SPI.begin(); // Init 78 SPI bus 79 mfrc522.PCD_Init(); // Init MFRC522 80 81 lcd.clear(); // Clear 82 LCD screen 83} 84 85void loop() { 86 // System will first look for mode 87 88 if (RFIDMode == true) { 89 lcd.setCursor(0, 0); 90 lcd.print("Jacksons 91 Vending"); 92 lcd.setCursor(0, 1); 93 lcd.print("$Scan/Swipe Card"); 94 95 96 // Look for new cards 97 if ( ! mfrc522.PICC_IsNewCardPresent()) { 98 return; 99 100 } 101 102 // Select one of the cards 103 if ( ! mfrc522.PICC_ReadCardSerial()) 104 { 105 return; 106 } 107 108 //Reading from the card 109 String tag 110 = ""; 111 for (byte j = 0; j < mfrc522.uid.size; j++) 112 { 113 tag.concat(String(mfrc522.uid.uidByte[j] 114 < 0x10 ? " 0" : " ")); 115 tag.concat(String(mfrc522.uid.uidByte[j], HEX)); 116 117 } 118 tag.toUpperCase(); 119 120 //Checking the card 121 if (tag.substring(1) 122 == tagUID) 123 { 124 // If UID of tag is matched. 125 lcd.clear(); 126 127 lcd.print("Please Wait..."); 128 delay(1000); 129 lcd.clear(); 130 131 lcd.print("Card Approved"); 132 digitalWrite(greenLed, HIGH); 133 delay(1000); 134 135 digitalWrite(greenLed, LOW); 136 137 lcd.clear(); 138 lcd.print("Enter 139 Password:"); 140 lcd.setCursor(0, 1); 141 RFIDMode = false; // Make RFID 142 mode false 143 } 144 145 else 146 { 147 // If UID of tag is not matched. 148 149 lcd.clear(); 150 lcd.print("Please Wait..."); 151 delay(1000); 152 153 lcd.setCursor(0, 0); 154 lcd.print("Card Declined!"); 155 lcd.setCursor(0, 156 1); 157 lcd.print("Access Denied"); 158 digitalWrite(buzzerPin, HIGH); 159 160 digitalWrite(redLed, HIGH); 161 delay(3000); 162 digitalWrite(buzzerPin, 163 LOW); 164 digitalWrite(redLed, LOW); 165 lcd.clear(); 166 } 167 } 168 169 170 // If RFID mode is false, it will look for keys from keypad 171 if (RFIDMode 172 == false) { 173 key_pressed = keypad_key.getKey(); // Storing keys 174 if 175 (key_pressed) 176 { 177 password[i++] = key_pressed; // Storing in password 178 variable 179 lcd.print("*"); 180 } 181 if (i == 4) // If 4 keys are 182 completed 183 { 184 delay(200); 185 if (!(strncmp(password, initial_password, 186 4))) // If password is matched 187 { 188 lcd.clear(); 189 lcd.setCursor(0, 190 0); 191 lcd.print("Dispensing"); 192 lcd.setCursor(0, 1); 193 lcd.print("Snack/Candy"); 194 195 sg180.write(1); // Dispensing 196 digitalWrite(greenLed, HIGH); 197 198 delay(3000); 199 digitalWrite(greenLed, LOW); 200 sg180.write(0); 201 // Dispenced 202 lcd.clear(); 203 delay(8000); 204 lcd.setCursor(0, 205 0); 206 lcd.print("Enjoy!"); 207 lcd.setCursor(0, 1); 208 lcd.print(":D"); 209 210 delay(2000); 211 lcd.clear(); 212 i = 0; 213 RFIDMode 214 = true; // Make RFID mode true 215 } 216 else // If password is not 217 matched 218 { 219 lcd.clear(); 220 lcd.setCursor(0, 0); 221 lcd.print("Wrong 222 Password"); 223 lcd.setCursor(0, 1); 224 lcd.print("Access Denied!"); 225 226 digitalWrite(buzzerPin, HIGH); 227 digitalWrite(redLed, HIGH); 228 229 delay(3000); 230 digitalWrite(buzzerPin, LOW); 231 digitalWrite(redLed, 232 LOW); 233 lcd.clear(); 234 i = 0; 235 RFIDMode = true; // 236 Make RFID mode true 237 } 238 } 239 } 240}
Code for Vending Machine
arduino
Code
1/3// Include required libraries 2#include <MFRC522.h> 3#include <LiquidCrystal_I2C.h> 4#include <Keypad.h> 5#include <Servo.h> 6#include <SPI.h> 7 8// Create instances 9LiquidCrystal_I2C lcd(0x27, 16, 2); 10MFRC522 mfrc522(10, 9); // MFRC522 mfrc522(SS_PIN, RST_PIN) 11Servo sg180; 12 13// Initialize Pins for led's, servo and buzzer 14// Blue LED is connected to 5V 15constexpr uint8_t greenLed = 7; 16constexpr uint8_t redLed = 6; 17constexpr uint8_t servoPin = 8; 18constexpr uint8_t buzzerPin = 5; 19 20char initial_password[4] = {'1', '2', '3', '4'}; // Variable to store initial password 21String tagUID = "B3 05 F6 16"; // String to store UID of tag. Change it with your tag's UID 22char password[4]; // Variable to store users password 23boolean RFIDMode = true; // boolean to change modes 24char key_pressed = 0; // Variable to store incoming keys 25uint8_t i = 0; // Variable used for counter 26 27// defining how many rows and columns our keypad have 28const byte rows = 4; 29const byte columns = 4; 30 31// Keypad pin map 32char hexaKeys[rows][columns] = { 33 {'1', '2', '3', 'A'}, 34 {'4', '5', '6', 'B'}, 35 {'7', '8', '9', 'C'}, 36 {'*', '0', '#', 'D'} 37}; 38 39// Initializing pins for keypad 40byte row_pins[rows] = {A0, A1, A2, A3}; 41byte column_pins[columns] = {2, 1, 0}; 42 43// Create instance for keypad 44Keypad keypad_key = Keypad( makeKeymap(hexaKeys), row_pins, column_pins, rows, columns); 45 46void setup() { 47 // Arduino Pin configuration 48 pinMode(buzzerPin, OUTPUT); 49 pinMode(redLed, OUTPUT); 50 pinMode(greenLed, OUTPUT); 51 52 sg180.attach(servoPin); //Declare pin 8 for servo 53 sg180.write(0); // Set initial position at 90 degrees 54 55 lcd.init(); // LCD screen 56 lcd.backlight(); 57 SPI.begin(); // Init SPI bus 58 mfrc522.PCD_Init(); // Init MFRC522 59 60 lcd.clear(); // Clear LCD screen 61} 62 63void loop() { 64 // System will first look for mode 65 if (RFIDMode == true) { 66 lcd.setCursor(0, 0); 67 lcd.print("Jacksons Vending"); 68 lcd.setCursor(0, 1); 69 lcd.print("$Scan/Swipe Card"); 70 71 // Look for new cards 72 if ( ! mfrc522.PICC_IsNewCardPresent()) { 73 return; 74 } 75 76 // Select one of the cards 77 if ( ! mfrc522.PICC_ReadCardSerial()) { 78 return; 79 } 80 81 //Reading from the card 82 String tag = ""; 83 for (byte j = 0; j < mfrc522.uid.size; j++) 84 { 85 tag.concat(String(mfrc522.uid.uidByte[j] < 0x10 ? " 0" : " ")); 86 tag.concat(String(mfrc522.uid.uidByte[j], HEX)); 87 } 88 tag.toUpperCase(); 89 90 //Checking the card 91 if (tag.substring(1) == tagUID) 92 { 93 // If UID of tag is matched. 94 lcd.clear(); 95 lcd.print("Please Wait..."); 96 delay(1000); 97 lcd.clear(); 98 lcd.print("Card Approved"); 99 digitalWrite(greenLed, HIGH); 100 delay(1000); 101 digitalWrite(greenLed, LOW); 102 103 lcd.clear(); 104 lcd.print("Enter Password:"); 105 lcd.setCursor(0, 1); 106 RFIDMode = false; // Make RFID mode false 107 } 108 109 else 110 { 111 // If UID of tag is not matched. 112 lcd.clear(); 113 lcd.print("Please Wait..."); 114 delay(1000); 115 lcd.setCursor(0, 0); 116 lcd.print("Card Declined!"); 117 lcd.setCursor(0, 1); 118 lcd.print("Access Denied"); 119 digitalWrite(buzzerPin, HIGH); 120 digitalWrite(redLed, HIGH); 121 delay(3000); 122 digitalWrite(buzzerPin, LOW); 123 digitalWrite(redLed, LOW); 124 lcd.clear(); 125 } 126 } 127 128 // If RFID mode is false, it will look for keys from keypad 129 if (RFIDMode == false) { 130 key_pressed = keypad_key.getKey(); // Storing keys 131 if (key_pressed) 132 { 133 password[i++] = key_pressed; // Storing in password variable 134 lcd.print("*"); 135 } 136 if (i == 4) // If 4 keys are completed 137 { 138 delay(200); 139 if (!(strncmp(password, initial_password, 4))) // If password is matched 140 { 141 lcd.clear(); 142 lcd.setCursor(0, 0); 143 lcd.print("Dispensing"); 144 lcd.setCursor(0, 1); 145 lcd.print("Snack/Candy"); 146 sg180.write(1); // Dispensing 147 digitalWrite(greenLed, HIGH); 148 delay(3000); 149 digitalWrite(greenLed, LOW); 150 sg180.write(0); // Dispenced 151 lcd.clear(); 152 delay(8000); 153 lcd.setCursor(0, 0); 154 lcd.print("Enjoy!"); 155 lcd.setCursor(0, 1); 156 lcd.print(":D"); 157 delay(2000); 158 lcd.clear(); 159 i = 0; 160 RFIDMode = true; // Make RFID mode true 161 } 162 else // If password is not matched 163 { 164 lcd.clear(); 165 lcd.setCursor(0, 0); 166 lcd.print("Wrong Password"); 167 lcd.setCursor(0, 1); 168 lcd.print("Access Denied!"); 169 digitalWrite(buzzerPin, HIGH); 170 digitalWrite(redLed, HIGH); 171 delay(3000); 172 digitalWrite(buzzerPin, LOW); 173 digitalWrite(redLed, LOW); 174 lcd.clear(); 175 i = 0; 176 RFIDMode = true; // Make RFID mode true 177 } 178 } 179 } 180}
Downloadable files
Vending Machine
The 8 pin block is where you connect RFID Module
Vending Machine
Comments
Only logged in users can leave comments
jacksonqw12
0 Followers
•0 Projects
1
0