Arduino Lock With Coin Cointer Bank

#include <Adafruit_Fingerprint.h>    //Libraries needed
#include <SoftwareSerial.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x27          //LCD i2c stuff
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7

int in2 = 4;

int But1=8;  // In order to add a push button I got the 5v from the pin 8 instead of "5v" arduino pin
int But2=9;

String Names[] = { "RAVEN", "Surtr", "Tech",}; //Those are the names affected to the fingertemplates IDs
                                                 //The first on which is Names[0] : Yassine has the ID 1 in the fingerprint sensor

SoftwareSerial mySerial(2, 3);                  //Fingerprint sensor wiring RX 3, TX 2           
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); //LCD declaring

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);                    //Fingerprint sensor declaring

void setup() 
{
  pinMode(in2, OUTPUT);
  digitalWrite(in2, HIGH);
  pinMode(But1, OUTPUT);    //Push button stuff, I made the pin 8 permanent LOW level
  digitalWrite(But1,LOW);
  pinMode(But2,INPUT_PULLUP);     //And I read the button state on pin 9
 
  Serial.begin(9600);               //Serial begin incase you need to see something on the serial monitor
  finger.begin(57600);              //Sensor baude rate
  lcd.begin (16,2);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home();
  finger.getTemplateCount();        //Counts the number of templates stored in the sensor flash memory
  delay(2000);
  introScreen();
}

void loop()                     
{
  int result = getFingerprintIDez();  //This function keeps looping and waiting for a fingerprint to be put on the sensor
  //delay(50);
  bool Button1=digitalRead(But2);  //Reading the state of the pushbutton
  if(Button1==LOW or result >= 0){
    //If the button is pressed it opens the doorlock as the button is meant to be inside
    OpenDoor();
    introScreen();
  }
}

void introScreen()
{
    lcd.clear();
    lcd.print("Place finger");
}

//Main function taken from the "fingerprint" example and modified
//Only the modifications are commented
int getFingerprintIDez() {
  uint8_t p = finger.getImage();        //Image scanning
  if (p != FINGERPRINT_OK)  return -1; 

  p = finger.image2Tz();               //Converting
  if (p != FINGERPRINT_OK)  return -1;

  lcd.clear();                     //And here we write a message or take an action for the denied template
  p = finger.fingerFastSearch();     //Looking for matches in the internal memory
  if (p != FINGERPRINT_OK){          //if the searching fails it means that the template isn't registered
    lcd.print("Access denied");
    delay(2000);
    introScreen();
    return -1;
  }
            //If we found a match we proceed in the function
 
  lcd.print("Welcome");                  //Printing a message for the recognized template
  lcd.setCursor(2,1);
  lcd.print(Names[finger.fingerID-1]); //Then print the name we gave it and the -1 is to remove the shift as the ID starts from "1" but the array from "0"
  return finger.fingerID;
}

/*The two following functions depends on your locking system
 *mine has a DC motor that should turn in both ways for opening and closing
 *that's why I'm using H bridge, you could have a solenoid with a transistor
 *then you'll need only opening sequence but controlling the transistor
 *as the closing is spring loaded...
 */


void OpenDoor(){
  digitalWrite(in2, LOW); // turn on motor
  delay(5000);
  digitalWrite(in2, HIGH); // turn off motor
}

Hi hoping someone can Help me How Can I make this type of project ..
The coinslot SIGNAL pin has 5V default voltage , if coin is inserted it will goes to zero after
1 second then back to 5v..

The code is just for the SOLENOID LOCK FINGER PRINT.
But how can I make a code that will retain the value of how much coin was put in the coin slot, value of coin is Php. 5.00 .

If the system will start maybe it will just display the total
then if I try to input finger print it will display "WELCOME" .

Hoping someone can guide me

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>

LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified module

const int coi100n  = A0;    //input from ir sensor
const int coi200n = A1;
const int coi500n  = A2; 

int ir100,ir200,ir500 = 0;
int cnt100r,cnt200r,cnt500r,cnttotal = 0;
void setup() {
  Serial.begin(9600); 
 pinMode(coi100n , INPUT);
  pinMode(coi200n ,INPUT);
  pinMode(coi500n,INPUT);
  
 
 lcd.setBacklightPin(3,POSITIVE);
 lcd.setBacklight(LOW); // You can turn the backlight off by setting it to LOW instead of HIGH
 lcd.begin(16, 2);
lcd.write(EEPROM.read(5));
}

void loop() {
 ir100 =digitalRead(coi100n);  //read the state of coi100n and store it as ir100
  ir200 =digitalRead(coi200n); //read the state of coi100n and store it as ir200
  ir500=digitalRead(coi500n);  //read the state of coi100n and store it as ir500
  
 if(ir100 ==LOW){cnt100r+=100;  delay(100);}
 if(ir200 ==LOW){cnt200r+=200;  delay(100);}
 if(ir500 ==LOW){cnt500r+=500;  delay(100);}
 
 cnttotal =cnt100r+cnt200r+cnt500r;
 
EEPROM.write(5,cnttotal);
Serial.println(EEPROM.read(5)); 


lcd.setCursor(0,0);
lcd.print("TOTAL:");
lcd.setCursor(0,1);
lcd.print(cnttotal);
}

how can I sink this code?

if coin is inserted it will goes to zero after 1 second then back to 5v

⇒ seems you want to track a FALLING front. Do you have bouncing?
add one every time you detect a transition matching the expected signal

A state machine is usually a practical coding architecture for these type of program

1 Like

The first problem I see is EEPROM.write() and EEPROM.read(). They only read and write one byte so you will lose the top 8 bits of "cnttotal". Try EEPROM.put() and EEPROM.get() instead. They will write and read both bytes of an integer.

The 'int' can only count up to 32867 so that is less than 400 coins when you are counting 100 at a time. I recommend that you count by 1 (instead of 100, 200, and 500) and multiply later.

    cnt100r += 1;
    cnt200r += 1;
    cnt500r += 1;
    cnttotal = cnt100r * 100ul + cnt200r * 200ul + cnt500r * 500ul;

I just get it from scracth on youtube sir , Im new in this coding of eeprom, can I use INPUT_PULLUP ? because the sensor pin of the
Coin slot has 5v. And I want to put it in pin 9 in arduino.

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#define coinSlot 6


LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified module

int coinCount = 0;
int coinSlotSignal;
int requiredCoins = 1;
boolean coinInserted = false;


void setup() {
  Serial.begin(9600); 
 pinMode(coinSlot, INPUT_PULLUP);

 lcd.setBacklightPin(3,POSITIVE);
 lcd.setBacklight(LOW); // You can turn the backlight off by setting it to LOW instead of HIGH
 lcd.begin(16, 2);
lcd.write(EEPROM.read(5));
}

void loop() {
  while(!coinInserted){
  coinSlotSignal = digitalRead(coinSlot);
  if(coinSlotSignal < 1) {
    coinCount += 1;
    coinInserted = true;
EEPROM.write(0,coinCount);
Serial.println(EEPROM.read(0)); 


lcd.setCursor(0,0);
lcd.print("TOTAL:");
lcd.setCursor(0,1);
lcd.print(coinCount);
}
  }
  
while(coinInserted) {
  coinSlotSignal = digitalRead(coinSlot);
  if(coinSlotSignal >0) {
    coinInserted = false;
  
  }
}
if(coinCount >= requiredCoins) {
  delay(1500);
}
}

I made it work with this code sir can please check if I have some error or unussed code to delete