//************** PIN Connection *****************
//Relay: IN --> D4
//LCD: SCL --> SCL, SDA --> SDA, GND --> GND, VCC --> 5V
//Keypad: D6 - D13 of UNO
//Coin Acceptor: DC12V --> +ve Power Jack, COIN --> Signal, GND --> -ve Power Jack
#include <Wire.h>
#include <Arduino.h>
//Include LCD display library for I2C
#include <LiquidCrystal_I2C.h>
//Include Keypad library
#include <Keypad.h>
const int coin = 2;
boolean insert = false;
volatile int pulse = 0;
float total = 0.00;
float cointdetect = 0;
const int Password_Length = 7;
// Character to hold password input
String Data;
// Password
String PassWord = "1234567";
// Pin connected from Relay to UNO
int LOCK = 4;
// Counter for character entries
byte data_count = 0;
// Character to hold key input
char customKey;
// Constants for row and column sizes
const byte ROWS = 4;
const byte COLS = 4;
// Array to represent keys on keypad
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Connections to Arduino pin
byte rowPins[ROWS] = {10, 11, 12, 13};
byte colPins[COLS] = {6, 7, 8, 9};
// Create keypad object
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// Create LCD object : Use 0x27 If 0x3F Doesn't work
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(2), coinInterrupt, RISING);
delay(1000);
lcd.init();
lcd.backlight();
// Set LOCK as an OUTPUT pin
pinMode(LOCK, OUTPUT);
}
void loop()
{
//************** Coin Acceptor ***************
while(insert)
{
if (insert)
{
insert = false;
Serial.println("coin detected!");
delay(50);
}
delay (200);
}
if(pulse == 1)
cointdetect = 0.10;
if(pulse == 2)
cointdetect = 0.10;
if(pulse == 3)
cointdetect = 0.20;
if(pulse == 4)
cointdetect = 0.20;
if(pulse == 5)
cointdetect = 0.50;
if(pulse == 6)
cointdetect = 0.50;
if(pulse != 0)
total = total + cointdetect;
pulse = 0;
Serial.println(total);
lcd.setCursor(0, 0);
lcd.print("RM ");
lcd.print(total);
lcd.print(" ");
//****************** KeyPad ******************
lcd.setCursor(0, 2);
lcd.print("Enter Password:");
// Look for keypress
customKey = customKeypad.getKey();
if (customKey)
{
// Enter keypress into array and increment counter
Data += customKey;
lcd.setCursor(data_count, 3);
lcd.print(Data[data_count]);
data_count++;
}
// See if we have reached the password length
if (data_count == Password_Length)
{
lcd.clear();
lcd.setCursor(0, 2);
if (Data == PassWord)
{
// Correct Password
lcd.print("Correct");
total = 0.00;
// Turn on relay for 5 seconds
digitalWrite(LOCK, HIGH);
delay(3000);
digitalWrite(LOCK, LOW);
}
else
{
// Incorrect Password
lcd.print("Incorrect");
delay(1000);
}
// Clear data and LCD display
lcd.clear();
clearData();
}
}
//interrupt
void coinInterrupt()
{
pulse++ ;
insert = true;
}
void clearData()
{
//Reset data_count
data_count = 0;
//Reset Data
Data ="";
}