Arduino keypad stop responding after certain period of time

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;
}

Hello manikqki

The usage of the delay() function will inhibit the expected realtime behaivior of the sketch.

Use the millis() function to design and code a timer function to get rid of this.

Have a nice day and enjoy coding in C++.

I have changed it to millis but problem still exists. I have checked now with multimeter that arduino is only receiving 3.3 volt does it cause it to hang.

If it is a 3.3V Arduino, it will be fine.

If it is a 5V Arduino, yes, it could hang.

Its a arduino uno does it works on 5v or 3.3v.


here how it looks i have also used 2 arduino nano. 1 arduino nano to controll fingerprint sensor and other to control ultrasonic sensor and a 4 channel relay module these 3 arduino trigger the relay. here i have powered everything with a mobile charger and when i measured the voltage its about 3.3v. now i have separated the power for arduino uno and df player mini. its been 1 hour and it is working fine but i dont know if it will still hangs or not.

any kind of mobile charger will deliver 5V at the plug.
You have to specify exactly at which two points of which PCB you measured the voltage.
Arduino Unos work on 5V.

On your picture I can see that you use an arduino-clone not an original uno.
Where did you buy this arduino uno? Post a link. If it was not amazon but a more professional online-shop than amazon is this shop provides tecnical specs of the board.

The microcontrollerworld is not super-standardised like USB-devives
With microcontrollers you have to care about more details than just

"does the plug fit into the socket?"

best regards Stefan

The Uno is a 5V board; it will not work reliably on 3.3V.

You are powering with an external power supply; which voltage? I can not see if your relays are powered from the 5V from the Uno. If yes, an Arduino is not a power supply. A schematic / wiring diagram (photo of pencil/paper drawing is OK) is better than some photos.

1 Like

I think I’d like to see a wiring diagram - you have 3 Arduinos in there and a relay board !

I have to say what you have built is rather untidy and difficult to repair any faults .
It’s not related to the issue here - although modifying it isn’t going to be easy -
Start off by mounting the boards in a logical fashion , all the same way up, then wire logically using the same coloured wires for say the power supply , and so on.

It will help when your projects get complex or you want to go back to something a year later


here is the arduino i have used https://amzn.eu/d/1o50u9m
and this is the wiring diagram i have used in my lock.

typical amazon. Unimportant things like packet size and weight are mentioned but not really the operating-voltage. This is the reason why you should not buy electronic components at amazon. A customer asked for the operating-voltage and some person answered 5V.

The picture looks fancy but it takes additional time to analyse it.
A handdrawn schematic is way better.
If I should analyse it I would re-draw it by hand with straight-forward connection lines
instead of realistic pictures.

And you haven't specified what voltages are at the plug-sockets

best regards Stefan

It has the appearance that everything is powered from the arduino(s?) itself - which is where the issue probably lies .
These Fritzing diagrams are NOT schematics and not easy to read .

When this issue is solved spend some time on researching schematics too- I think there are some helpful tutorials on this site .

there are two input jacks for power each jack is fed 5v. there is a separate 5v power for arduino uno and rest everything is powered from another 5v jack. i have separate the power of arduino uno as it takes more power than other component. but i feel like when arduino has trouble communicating with the df player mini then the issue arrise it just hangs and doesnt respond to anything.

5V should go to the 5V pin, not to Vin. This applies to both the Nano and the Uno.

What is the specification of the power supplies? 5V/1A or 5V/2A or ...

okay so here are the changes i made:

  1. I have changed delay to millis
  2. i have changed strings to char to save memory space
  3. i have separated arduino uno power from rest of the circuit.
  4. i gave stable 5v supply to full circuit by using mobile charger (specically samsung charger)
  5. i have added a watchdog timer so that if code stuck somewhere then the whole controller will reset.
  6. i have connect power wires to 5v on arduino and removed from vin.
  7. now relay is connected separetly to the 5v supply.
  8. i have added a function so that when key 9 is pressed on the kepad than arduino will reset itself
  9. i have added a function that when df player does not respond than only 3 tries will be given and then the command get terminated so that df player do not cause arduino to stuck.

now i am waiting that if arduino will still freeze or not. currently everything is working fine but i dont know for how long.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.