Here i have made a arduino door where it uses relay, 4*4 keypad and df player mini to play sound. when correct password is entered then a welcome sound is played and when incorrect password is enteres then a access denied sound is played. there is also is button when pressed for more than 10 seconds then door opened warning is played. but problem here is that i dont know weather arduino code stucks or df player stop responding but after certain period of time whole system stops and doesnt repond to any input from keypad or that door button.
here is the arduino code:
#include <Wire.h>
// Include LCD display library for I2C
// Include Keypad library
#include <Keypad.h>
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
static const uint8_t PIN_MP3_TX = 11; // Connects to module's RX
static const uint8_t PIN_MP3_RX = 12; // Connects to module's TX
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);
// Length of password + 1 for null character
#define Password_Length 4
// Character to hold password input
char Data[Password_Length];
// Password
char Master[Password_Length] = "045";
// Pin connected to lock relay input
int lockOutput = 10;
// 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;
const int wirePin = 4;
const int endPin = 5;
const int buttonPin = 2;
const int relayPin = 3;
// 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
byte rowPins[ROWS] = {9, 8, A5, A4};
byte colPins[COLS] = {A3, A2, A1, A0};
// Create keypad object
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
unsigned long previousMillis = 0; // Variable to store the time when the button was last pressed
const unsigned long interval = 10000;
// Create LCD object
DFRobotDFPlayerMini player;
void setup() {
pinMode(wirePin, INPUT);
pinMode(endPin, INPUT);
pinMode(buttonPin, INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH);
Serial.begin(9600);
pinMode(lockOutput, OUTPUT);
digitalWrite(lockOutput, HIGH);
softwareSerial.begin(9600);
if (player.begin(softwareSerial)) {
Serial.println("OK");
// Set volume to maximum (0 to 30).
player.volume(25);
// Play the first MP3 file on the SD card
}
}
void loop() {
static bool buttonPressed = false; // Flag to track button state
// Check button timer
if (digitalRead(buttonPin) == HIGH) {
previousMillis = millis(); // Update the previousMillis value
digitalWrite(relayPin, HIGH); // Deactivate the relay
buttonPressed = true; // Set buttonPressed flag
} else {
unsigned long currentMillis = millis(); // Get the current time
// Check if the button hasn't been pressed for 5 seconds
if (currentMillis - previousMillis >= interval && buttonPressed) {
digitalWrite(relayPin, LOW); // Activate the relay
player.play(3);
buttonPressed = false; // Reset buttonPressed flag
}
}
// Check for keypad input only if button is pressed
if (buttonPressed) {
customKey = customKeypad.getKey();
if (customKey != NO_KEY) {
// Enter keypress into array and increment counter
Data[data_count] = customKey;
data_count++;
// See if we have reached the password length
if (data_count == Password_Length - 1) {
if (strcmp(Data, Master) == 0) {
// Password is correct
digitalWrite(lockOutput, LOW);
player.play(2);
delay(7000);
digitalWrite(lockOutput, HIGH);
delay(1000);
} else {
player.play(1); // Password is incorrect
}
// Clear data and LCD display
clearData();
}
}
} else {
// Clear data if button is not pressed
clearData();
}
}
void clearData() {
// Go through array and clear data
while (data_count != 0) {
Data[data_count--] = 0;
}
return;
}