#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
const byte ROWS = 4;
const byte COLS = 4;
char Keys [ROWS] [COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowpins[ROWS] = {2,3,4,5};
byte colpins[COLS] = {6,7,8,9};
Keypad mykeypad = Keypad(makeKeymap(Keys), rowpins, colpins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 20, 4);
char products[4][7] = {"CHIPS", "WATER", "COKE", "COOKIE"};
char command[4][2] = {"1", "2", "3", "4"};
Servo ServoChips;
Servo ServoWater;
Servo ServoCoke;
Servo ServoCookie;
void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.backlight();
ServoChips.attach(10);
ServoWater.attach(11);
ServoCoke.attach(12);
ServoCookie.attach(13);
ServoChips.write(0);
ServoWater.write(0);
ServoCoke.write(0);
ServoCookie.write(0);
}
void loop() {
list();
char keyData = mykeypad.getKey();
if (keyData) { // check if a key is pressed
if (keyData == command[0][0]) {
Chips();
}
else if (keyData == command[1][0]) {
Water();
}
else if (keyData == command[2][0]) {
Coke();
}
else if (keyData == command[3][0]) {
Cookie();
}
else {
none();
}
}
}
void list(){
lcd.setCursor(0, 0);
lcd.print(products[0]);
lcd.setCursor(8, 0);
lcd.print("= ");
lcd.print(command[0]);
lcd.setCursor(0, 1);
lcd.print(products[1]);
lcd.setCursor(8, 1);
lcd.print("= ");
lcd.print(command[1]);
lcd.setCursor(0, 2);
lcd.print(products[2]);
lcd.setCursor(8, 2);
lcd.print("= ");
lcd.print(command[2]);
lcd.setCursor(0, 3);
lcd.print(products[3]);
lcd.setCursor(8, 3);
lcd.print("= ");
lcd.print(command[3]);
}
void Chips() {
lcd.clear();
lcd.print(products[0]);
lcd.print(" dispensing...");
delay(500);
ServoChips.write(180);
delay(1000);
ServoChips.write(0);
lcd.clear();
}
void Water() {
lcd.clear();
lcd.print(products[1]);
lcd.print(" dispensing...");
delay(500);
ServoWater.write(180);
delay(1000);
ServoWater.write(0);
lcd.clear();
}
void Coke() {
lcd.clear();
lcd.print(products[2]);
lcd.print(" dispensing...");
delay(500);
ServoCoke.write(180);
delay(1000);
ServoCoke.write(0);
lcd.clear();
}
void Cookie() {
lcd.clear();
lcd.print(products[3]);
lcd.print(" dispensing...");
delay(500);
ServoCookie.write(180);
delay(1000);
ServoCookie.write(0);
lcd.clear();
}
void none() {
lcd.clear();
lcd.print("No such product!");
delay(1500);
lcd.clear();
}Coke
Chips
Water
Cookie
Arduino Vending Machine