here is the safe lock with Arduino
Circuit Schematic
Arduino Code
#define CLK 9 // Pin for CLK
#define DT 10 // Pin for DT
#define SW 11 // Pin for SW
#define Red_LED A2 // Pin for Red LED
#define Green_LED A3 // Pin for Green LED
#define buttonPin A4 // Pin for the button
#define Buzzer A5 // Pin for the buzzer
// Define Connections to 74HC595 Shift Register
const int dataPin = 6; // DS pin
const int latchPin = 7; // ST_CP pin
const int clockPin = 8; // SH_CP pin
// Define Connections to BCD
int a1 = 2;
int a2 = 5;
int a3 = 4;
int a4 = 3;
// Patterns for characters 0,1,2,3,4,5,6,7,8,9,
int datArray[16] = {B11111010, B01100000, B11011100, B11110100, B01100110, B10110110, B10111110, B11100000, B11111110, B11110110};
bool P_Mode = false;
int counter = 0;
int currentStateCLK;
int lastStateCLK;
int a = 0, b = 0, c = 0;
int p1 = 2, p2 = 4, p3 = 6;
int i = 0, Try = 3;
String currentDir = "";
bool flagbtn = true, flagclk = true;
bool Start = false, Unlock = false, state = true;
unsigned long prevmls, prevmlsG;
void setup() {
// Setup Serial Monitor
Serial.begin(9600);
pinMode(a1, OUTPUT);
pinMode(a2, OUTPUT);
pinMode(a3, OUTPUT);
pinMode(a4, OUTPUT);
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
pinMode(SW, INPUT_PULLUP);
pinMode(buttonPin, INPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(Red_LED, OUTPUT);
pinMode(Green_LED, OUTPUT);
pinMode(Buzzer, OUTPUT);
// Read the initial state of CLK
lastStateCLK = digitalRead(CLK);
Turn_OFF_Display();
}
void loop() {
Read_button(); // Check button status
read_encoder(); // Read rotary encoder
Update_display(a, b, c); // Update the display based on the selected digits
Update_LEDs(); // Update the LEDs
}
//------------------------------------------------Status LEDs----------------------------
void Update_LEDs()
{
if (Start == true)
{ if (P_Mode == false) {
if (Unlock == true)
{ digitalWrite(Green_LED, HIGH);
digitalWrite(Red_LED, LOW);
}
else
{ digitalWrite(Red_LED, HIGH);
digitalWrite(Green_LED, LOW);
}
}
else if (P_Mode == true)
{ digitalWrite(Red_LED, LOW);
if (millis() - prevmlsG > 500)
{
state = !state;
digitalWrite(Green_LED, state);
prevmlsG = millis();
}
}
}
}
//---------------------------------------------Reading Button--------------------------
void Read_button()
{
if (digitalRead(buttonPin) == HIGH && flagbtn == true)
{ flagbtn = false;
Start = !Start;
prevmls = millis();
if (Start == true)
{
for (int j = 0; j < 10; j++) // Display numbers 0-9 on the 7-segment display
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, datArray[j]);
shiftOut(dataPin, clockPin, MSBFIRST, datArray[j]);
digitalWrite(latchPin, HIGH);
disp1(j);
delay(50);
}
}
}
else if (digitalRead(buttonPin) == LOW && flagbtn == false)
{ flagbtn = true;
if (millis() - prevmls > 2000)
{ Start = true;
P_Mode = !P_Mode;
if (P_Mode == true)
Serial.println("Password Mode Activated!");
else
{ Serial.println("Password Mode Deactivated!");
p1 = a;
p2 = b;
p3 = c;
Serial.println((String)"\nNew Password Saved as: " + p1 + p2 + p3 + "\n");
a = 0; b = 0; c = 0;
}
}
}
}
//-------------------------------------------------Encoder---------------------------------------
void read_encoder()
{ if (Start == true)
{
currentStateCLK = digitalRead(CLK);
if (currentStateCLK != lastStateCLK && currentStateCLK == 1) {
if (digitalRead(DT) != currentStateCLK) {
// Encoder is rotating CW so increment
counter ++;
if (counter > 9)
counter = 0;
currentDir = "CW";
}
else {
// Encoder is rotating CCW so increment
counter --;
currentDir = "CCW";
if (counter < 0)
counter = 9;
}
if (i == 0)
a = counter;
if (i == 1)
b = counter;
if (i == 2)
c = counter;
// Serial.print("Direction: "); Serial.print(currentDir);
// Serial.print(" | Counter: "); Serial.println(counter);
}
// Remember last CLK state
lastStateCLK = currentStateCLK;
//If we detect LOW signal, button is pressed
if (digitalRead(SW) == LOW && flagclk == true) {
flagclk = false;
Serial.println("Button pressed!");
i++;
if (i > 2)
{ i = 0;
if (P_Mode == false) {
if (p1 == a && p2 == b && p3 == c)
{
Unlock = true;
Serial.println("Correct Password!");
a = 0; b = 0; c = 0;
digitalWrite(Buzzer, HIGH);
delay(500);
digitalWrite(Buzzer, LOW);
delay(500);
}
else
{ digitalWrite(Red_LED, HIGH);
digitalWrite(Green_LED, LOW);
Serial.println("Wrong Password, Try Again!");
for (int j = 0; j < 2; j++) {
digitalWrite(Buzzer, HIGH);
delay(300);
digitalWrite(Buzzer, LOW);
delay(300);
}
a = 0; b = 0; c = 0;
Try--;
Update_display(Try, Try, Try);
delay(1500);
if (Try == 0)
{
for (int k = 0; k < 10; k++)
{
digitalWrite(Buzzer, HIGH);
delay(200);
digitalWrite(Buzzer, LOW);
delay(200);
}
Start = false;
digitalWrite(Red_LED, LOW);
digitalWrite(Green_LED, LOW);
}
}
}
}
delay(500);
}
else if (digitalRead(SW) == HIGH && flagclk == false) {
flagclk = true;
}
}
}
void disp1(int num) { //this method entails making 0-9 combinations using the BCD pins by sending different outputs to turn each segment on or off.
if (num == 0) //0000
{
digitalWrite(a1, LOW);
digitalWrite(a2, LOW);
digitalWrite(a3, LOW);
digitalWrite(a4, LOW);
}
if (num == 1) //0001
{
digitalWrite(a1, HIGH);
digitalWrite(a2, LOW);
digitalWrite(a3, LOW);
digitalWrite(a4, LOW);
}
if (num == 2) //0010
{
digitalWrite(a1, LOW);//0
digitalWrite(a2, HIGH);//1
digitalWrite(a3, LOW);//0
digitalWrite(a4, LOW);//0
}
if (num == 3) //0011
{
digitalWrite(a1, HIGH);//1
digitalWrite(a2, HIGH);//1
digitalWrite(a3, LOW);//0
digitalWrite(a4, LOW);//0
}
if (num == 4) //0100
{
digitalWrite(a1, LOW);//0
digitalWrite(a2, LOW);//0
digitalWrite(a3, HIGH);//1
digitalWrite(a4, LOW);//0
}
if (num == 5) //0101
{
digitalWrite(a1, HIGH);//1
digitalWrite(a2, LOW);//0
digitalWrite(a3, HIGH);//1
digitalWrite(a4, LOW);//0
}
if (num == 6) //0110
{
digitalWrite(a1, LOW);//0
digitalWrite(a2, HIGH);//1
digitalWrite(a3, HIGH);//1
digitalWrite(a4, LOW);//0
}
if (num == 7) //0111
{
digitalWrite(a1, HIGH);//1
digitalWrite(a2, HIGH);//1
digitalWrite(a3, HIGH);//1
digitalWrite(a4, LOW);//0
}
if (num == 8) //1000
{
digitalWrite(a1, LOW);//0
digitalWrite(a2, LOW);//0
digitalWrite(a3, LOW);//0
digitalWrite(a4, HIGH);//1
}
if (num == 9) //1001
{
digitalWrite(a1, HIGH);//1
digitalWrite(a2, LOW);//0
digitalWrite(a3, LOW);//0
digitalWrite(a4, HIGH);//1
}
}
//------------------------Function for turning of all the 7-Segments--------------------
void Turn_OFF_Display()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, datArray[0]);
shiftOut(dataPin, clockPin, MSBFIRST, datArray[0]);
digitalWrite(latchPin, HIGH);
disp1(0);
}
//-------------------------------------------Functino to update 7-Segments
void Update_display(int x, int y, int z)
{ if (Start == true) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, datArray[x]);
shiftOut(dataPin, clockPin, MSBFIRST, datArray[y]);
digitalWrite(latchPin, HIGH);
disp1(z);
}
}
Tha was nice idea