Customkeypad not not working

Hii,
In my project buttons are connected in matrix format, and Iam using customKeypad to scan the matrix. But it is not working...ie, the pressed key is not seen in serial monitor. But arduino's transmitter pin is always on. My code is to press any key in matrix and corresponding audio will be played by using DF player mini. Because of the key press is not detected no audio output is there. Here Iam sharing a bit of code and Circuit diagram

void loop() {
  // Get key value if pressed
  char customKey = customKeypad.getKey();

Serial.println(customKey);  ///// this is not printing in serial monitor and bcoz of this transmitter led in arduino nano is always on

    if (customKey=='M')
    {
      catCount=1;
      
      Serial.println("mode"); 
     delay(500);
}

what do you get if nothing is pressed? 'cause that's what you're printing, mostly, since you only delay() if you press the right key.

Without seeing the rest of your code, we can't even tell if you've set up the custom keypad correctly.
Go to the IDE, press ctrl-T, press ctrl-shft-C
In a new message here, press ctrl-V and add any comments you want to below the code block.
Thanks

Not a true statement. If no key is pressed, .getKey() returns 0 or '\0' which you then print, followed by a newline (or CR+NL). You are doing this as fast as loop() is running so you are filling up the Tx buffer with more characters than you can send. That is why your Tx LED is constantly on.

Remove that statement or only print it once in a while

Here where? That's a portion of your code, not enough to see how you went wrong, and no circuit diagram (schematic).

a7

Before asking questions:


If your sketch was generated with an AI like ChatGPT or by someone else tell us in your post.


  • Questioners know what their hardware and software looks like but the volunteers here do not; you need to fully explain what’s not working, and how it is supposed to work.
  • Please read all the posting guidelines before asking your questions; follow these guidelines in your post.

Hardware

  • As always, show us a good schematic of your proposed circuit. Hand drawn schematics are acceptable.
  • Show us good images of your ‘actual’ wiring.
  • Give WEB links to your major components.

Software

  • For readability, place { and } on separate lines by themselves, place each line of code on a separate line.
  • In the Arduino IDE, use Ctrl T or CMD T to format your code, then copy the complete sketch.
  • Use the < CODE / > icon from the ‘posting menu’ to attach your copied sketch.

When you follow the posting guidelines, volunteers can be more effective.

And there will more of them willing to spend the time.

a7

1 Like
// Include the Keypad library
#include <Keypad.h>

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(3, 4); // RX, TX
DFRobotDFPlayerMini myDFPlayer;


// 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', '4'},
  {'5', '6', '7', '8'},
  {'9', 'a', 'b', 'c'},
  {'d', 'e', 'M', '0'}
};

// Connections to Arduino
byte rowPins[ROWS] = {12, 11, 10, 9};
byte colPins[COLS] = {8, 7, 6, 5};

// Create keypad object
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

int catCount=0;

void setup() {
  // Setup serial monitor
  Serial.begin(9600);

   mySoftwareSerial.begin(9600);
  
  myDFPlayer.begin(mySoftwareSerial);  //Use softwareSerial to communicate with mp3.
   
  myDFPlayer.volume(30);  //Set volume value. From 0 to 30


//  keypad.addEventListener(keypadEvent);  // Add an event listener.
  customKeypad.setHoldTime(500);               // Default is 1000mS
  customKeypad.setDebounceTime(250);           // Default is 50mS
}

void loop() {
  // Get key value if pressed
  char customKey = customKeypad.getKey();

    if (customKey=='M')
    {
      catCount=1;
      
     Serial.println("CAT pressed"); 
            cat1();
      }
}

void cat1()
{
  Serial.println("CAT_1");
  myDFPlayer.playFolder(15,1);
        delay(100);
  
  char customKey;
 

    while(true)
    {
      customKey=customKeypad.getKey();
      
    if(customKey=='M')
    {++catCount;
        if(catCount==4)
      catCount=1;
            cat2();
 
      }
    else if(customKey)
    {
    if(customKey=='1')
    {
      myDFPlayer.playFolder(1,1);
      delay(100);
      Serial.println("TVM_1");
      customKey=0;
      }
      else if(customKey=='2')
      {
        myDFPlayer.playFolder(1,2);
        delay(100);
        Serial.println("KLM_1");
        customKey=0;
        }
        else if(customKey=='3')
      {
        myDFPlayer.playFolder(1,3);
        delay(100);
        Serial.println("PTM_1");
        customKey=0;
        }
        else if(customKey=='4')
      {
        myDFPlayer.playFolder(1,4);
        delay(100);
        Serial.println("ALP_1");
        customKey=0;
        }
        else if(customKey=='5')
      {
        myDFPlayer.playFolder(1,5);
        delay(100);
        Serial.println("KTM_1");
        customKey=0;
        }
        else if(customKey=='6')
      {
        myDFPlayer.playFolder(1,6);
        delay(100);
        Serial.println("IDK_1");
        customKey=0;
        }
        else if(customKey=='7')
      {
        myDFPlayer.playFolder(1,7);
        delay(100);
        Serial.println("EKM_1");
        customKey=0;
        }
        else if(customKey=='8')
      {
        myDFPlayer.playFolder(1,8);
        delay(100);
        Serial.println("THR_1");
        customKey=0;
        }
        else if(customKey=='9')
      {
        myDFPlayer.playFolder(1,9);
        delay(100);
        Serial.println("PKD_1");
        customKey=0;
        }
        else if(customKey=='a')
      {
        myDFPlayer.playFolder(1,10);
        delay(100);
        Serial.println("MLP_1");
        customKey=0;
        }
        else if(customKey=='b')
      {
        myDFPlayer.playFolder(1,11);
        delay(100);
        Serial.println("KZD_1");
        customKey=0;
        }
        else if(customKey=='c')
      {
        myDFPlayer.playFolder(1,12);
        delay(100);
        Serial.println("WYD_1");
        customKey=0;
        }
        else if(customKey=='d')
      {
        myDFPlayer.playFolder(1,13);
        delay(100);
        Serial.println("KNR_1");
        customKey=0;
        }
        else if(customKey=='e')
      {
        myDFPlayer.playFolder(1,14);
        delay(100);
        Serial.println("KKD_1");
        customKey=0;
        }
}}}


void cat2()
{
  myDFPlayer.playFolder(15,2);
  delay(100);
    Serial.println("CAT_2");

     char customKey;

    while(true)
    {
        customKey=customKeypad.getKey();
      
    if(customKey=='M')
    {++catCount;
        if(catCount==4)
      catCount=1;

            cat3();
      
      }
    else if(customKey)
    {
    if(customKey=='1')
    {
      myDFPlayer.playFolder(2,1);
      delay(100);
      Serial.println("TVM_2");
      }
      else if(customKey=='2')
      {
        myDFPlayer.playFolder(2,2);
        delay(100);
        Serial.println("KLM_2");
        }
        else if(customKey=='3')
      {
        myDFPlayer.playFolder(2,3);
        delay(100);
        Serial.println("PTM_2");
        customKey=0;
        }
        else if(customKey=='4')
      {
        myDFPlayer.playFolder(2,4);
        delay(100);
        Serial.println("ALP_2");
        customKey=0;
        }
        else if(customKey=='5')
      {
        myDFPlayer.playFolder(2,5);
        delay(100);
        Serial.println("KTM_2");
        customKey=0;
        }
        else if(customKey=='6')
      {
        myDFPlayer.playFolder(2,6);
        delay(100);
        Serial.println("IDK_2");
        customKey=0;
        }
        else if(customKey=='7')
      {
        myDFPlayer.playFolder(2,7);
        delay(100);
        Serial.println("EKM_2");
        customKey=0;
        }
        else if(customKey=='8')
      {
        myDFPlayer.playFolder(2,8);
        delay(100);
        Serial.println("THR_2");
        customKey=0;
        }
        else if(customKey=='9')
      {
        myDFPlayer.playFolder(2,9);
        delay(100);
        Serial.println("PKD_2");
        customKey=0;
        }
        else if(customKey=='a')
      {
        myDFPlayer.playFolder(2,10);
        delay(100);
        Serial.println("MLP_2");
        customKey=0;
        }
        else if(customKey=='b')
      {
        myDFPlayer.playFolder(2,11);
        delay(100);
        Serial.println("KZD_2");
        customKey=0;
        }
        else if(customKey=='c')
      {
        myDFPlayer.playFolder(2,12);
        delay(100);
        Serial.println("WYD_2");
        customKey=0;
        }
        else if(customKey=='d')
      {
        myDFPlayer.playFolder(2,13);
        delay(100);
        Serial.println("KNR_2");
        customKey=0;
        }
        else if(customKey=='e')
      {
        myDFPlayer.playFolder(2,14);
        delay(100);
        Serial.println("KKD_2");
        customKey=0;
        }        
  
}}}

  
void cat3()
{
  myDFPlayer.playFolder(15,3);
  delay(100);
    Serial.println("CAT_3");

     char customKey;
    while(true)
    {
      customKey=customKeypad.getKey();
      
    if(customKey=='M')
    {++catCount;
        if(catCount==4)
      catCount=1;
            cat1();
      
      }
    else if(customKey)
    {
    if(customKey=='1')
    {
      myDFPlayer.playFolder(3,1);
      delay(100);
      Serial.println("TVM_3");
      }
      else if(customKey=='2')
      {
        myDFPlayer.playFolder(3,2);
        delay(100);
        Serial.println("KLM_3");
        }
        else if(customKey=='3')
      {
        myDFPlayer.playFolder(3,3);
        delay(100);
        Serial.println("PTM_3");
        customKey=0;
        }
        else if(customKey=='4')
      {
        myDFPlayer.playFolder(3,4);
        delay(100);
        Serial.println("ALP_3");
        customKey=0;
        }
        else if(customKey=='5')
      {
        myDFPlayer.playFolder(3,5);
        delay(100);
        Serial.println("KTM_3");
        customKey=0;
        }
        else if(customKey=='6')
      {
        myDFPlayer.playFolder(3,6);
        delay(100);
        Serial.println("IDK_3");
        customKey=0;
        }
        else if(customKey=='7')
      {
        myDFPlayer.playFolder(3,7);
        delay(100);
        Serial.println("EKM_3");
        customKey=0;
        }
        else if(customKey=='8')
      {
        myDFPlayer.playFolder(3,8);
        delay(100);
        Serial.println("THR_3");
        customKey=0;
        }
        else if(customKey=='9')
      {
        myDFPlayer.playFolder(3,9);
        delay(100);
        Serial.println("PKD_3");
        customKey=0;
        }
        else if(customKey=='a')
      {
        myDFPlayer.playFolder(3,10);
        delay(100);
        Serial.println("MLP_3");
        customKey=0;
        }
        else if(customKey=='b')
      {
        myDFPlayer.playFolder(3,11);
        delay(100);
        Serial.println("KZD_3");
        customKey=0;
        }
        else if(customKey=='c')
      {
        myDFPlayer.playFolder(3,12);
        delay(100);
        Serial.println("WYD_3");
        
        customKey=0;
        }
        else if(customKey=='d')
      {
        myDFPlayer.playFolder(3,13);
        delay(100);
        Serial.println("KNR_3");
    
        customKey=0;
        }
        else if(customKey=='e')
      {
        myDFPlayer.playFolder(3,14);
        delay(100);
        Serial.println("KKD_3");
        
        customKey=0;
        }
  
}}}

Hii I uploaded the circuit dia, but dont know y it is not shown here

// Include the Keypad library
#include <Keypad.h>

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(3, 4); // RX, TX
DFRobotDFPlayerMini myDFPlayer;


// 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', '4'},
  {'5', '6', '7', '8'},
  {'9', 'a', 'b', 'c'},
  {'d', 'e', 'M', '0'}
};

// Connections to Arduino
byte rowPins[ROWS] = {12, 11, 10, 9};
byte colPins[COLS] = {8, 7, 6, 5};

// Create keypad object
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

int catCount=0;

void setup() {
  // Setup serial monitor
  Serial.begin(9600);

   mySoftwareSerial.begin(9600);
  
  myDFPlayer.begin(mySoftwareSerial);  //Use softwareSerial to communicate with mp3.
   
  myDFPlayer.volume(30);  //Set volume value. From 0 to 30


//  keypad.addEventListener(keypadEvent);  // Add an event listener.
  customKeypad.setHoldTime(500);               // Default is 1000mS
  customKeypad.setDebounceTime(250);           // Default is 50mS
}

void loop() {
  // Get key value if pressed
  char customKey = customKeypad.getKey();

    if (customKey=='M')
    {
      catCount=1;
      
     Serial.println("CAT pressed"); 
            cat1();
      }
}

void cat1()
{
  Serial.println("CAT_1");
  myDFPlayer.playFolder(15,1);
        delay(100);
  
  char customKey;
 

    while(true)
    {
      customKey=customKeypad.getKey();
      
    if(customKey=='M')
    {++catCount;
        if(catCount==4)
      catCount=1;
            cat2();
 
      }
    else if(customKey)
    {
    if(customKey=='1')
    {
      myDFPlayer.playFolder(1,1);
      delay(100);
      Serial.println("TVM_1");
      customKey=0;
      }
      else if(customKey=='2')
      {
        myDFPlayer.playFolder(1,2);
        delay(100);
        Serial.println("KLM_1");
        customKey=0;
        }
        else if(customKey=='3')
      {
        myDFPlayer.playFolder(1,3);
        delay(100);
        Serial.println("PTM_1");
        customKey=0;
        }
        else if(customKey=='4')
      {
        myDFPlayer.playFolder(1,4);
        delay(100);
        Serial.println("ALP_1");
        customKey=0;
        }
        else if(customKey=='5')
      {
        myDFPlayer.playFolder(1,5);
        delay(100);
        Serial.println("KTM_1");
        customKey=0;
        }
        else if(customKey=='6')
      {
        myDFPlayer.playFolder(1,6);
        delay(100);
        Serial.println("IDK_1");
        customKey=0;
        }
        else if(customKey=='7')
      {
        myDFPlayer.playFolder(1,7);
        delay(100);
        Serial.println("EKM_1");
        customKey=0;
        }
        else if(customKey=='8')
      {
        myDFPlayer.playFolder(1,8);
        delay(100);
        Serial.println("THR_1");
        customKey=0;
        }
        else if(customKey=='9')
      {
        myDFPlayer.playFolder(1,9);
        delay(100);
        Serial.println("PKD_1");
        customKey=0;
        }
        else if(customKey=='a')
      {
        myDFPlayer.playFolder(1,10);
        delay(100);
        Serial.println("MLP_1");
        customKey=0;
        }
        else if(customKey=='b')
      {
        myDFPlayer.playFolder(1,11);
        delay(100);
        Serial.println("KZD_1");
        customKey=0;
        }
        else if(customKey=='c')
      {
        myDFPlayer.playFolder(1,12);
        delay(100);
        Serial.println("WYD_1");
        customKey=0;
        }
        else if(customKey=='d')
      {
        myDFPlayer.playFolder(1,13);
        delay(100);
        Serial.println("KNR_1");
        customKey=0;
        }
        else if(customKey=='e')
      {
        myDFPlayer.playFolder(1,14);
        delay(100);
        Serial.println("KKD_1");
        customKey=0;
        }
}}}


void cat2()
{
  myDFPlayer.playFolder(15,2);
  delay(100);
    Serial.println("CAT_2");

     char customKey;

    while(true)
    {
        customKey=customKeypad.getKey();
      
    if(customKey=='M')
    {++catCount;
        if(catCount==4)
      catCount=1;

            cat3();
      
      }
    else if(customKey)
    {
    if(customKey=='1')
    {
      myDFPlayer.playFolder(2,1);
      delay(100);
      Serial.println("TVM_2");
      }
      else if(customKey=='2')
      {
        myDFPlayer.playFolder(2,2);
        delay(100);
        Serial.println("KLM_2");
        }
        else if(customKey=='3')
      {
        myDFPlayer.playFolder(2,3);
        delay(100);
        Serial.println("PTM_2");
        customKey=0;
        }
        else if(customKey=='4')
      {
        myDFPlayer.playFolder(2,4);
        delay(100);
        Serial.println("ALP_2");
        customKey=0;
        }
        else if(customKey=='5')
      {
        myDFPlayer.playFolder(2,5);
        delay(100);
        Serial.println("KTM_2");
        customKey=0;
        }
        else if(customKey=='6')
      {
        myDFPlayer.playFolder(2,6);
        delay(100);
        Serial.println("IDK_2");
        customKey=0;
        }
        else if(customKey=='7')
      {
        myDFPlayer.playFolder(2,7);
        delay(100);
        Serial.println("EKM_2");
        customKey=0;
        }
        else if(customKey=='8')
      {
        myDFPlayer.playFolder(2,8);
        delay(100);
        Serial.println("THR_2");
        customKey=0;
        }
        else if(customKey=='9')
      {
        myDFPlayer.playFolder(2,9);
        delay(100);
        Serial.println("PKD_2");
        customKey=0;
        }
        else if(customKey=='a')
      {
        myDFPlayer.playFolder(2,10);
        delay(100);
        Serial.println("MLP_2");
        customKey=0;
        }
        else if(customKey=='b')
      {
        myDFPlayer.playFolder(2,11);
        delay(100);
        Serial.println("KZD_2");
        customKey=0;
        }
        else if(customKey=='c')
      {
        myDFPlayer.playFolder(2,12);
        delay(100);
        Serial.println("WYD_2");
        customKey=0;
        }
        else if(customKey=='d')
      {
        myDFPlayer.playFolder(2,13);
        delay(100);
        Serial.println("KNR_2");
        customKey=0;
        }
        else if(customKey=='e')
      {
        myDFPlayer.playFolder(2,14);
        delay(100);
        Serial.println("KKD_2");
        customKey=0;
        }        
  
}}}

  
void cat3()
{
  myDFPlayer.playFolder(15,3);
  delay(100);
    Serial.println("CAT_3");

     char customKey;
    while(true)
    {
      customKey=customKeypad.getKey();
      
    if(customKey=='M')
    {++catCount;
        if(catCount==4)
      catCount=1;
            cat1();
      
      }
    else if(customKey)
    {
    if(customKey=='1')
    {
      myDFPlayer.playFolder(3,1);
      delay(100);
      Serial.println("TVM_3");
      }
      else if(customKey=='2')
      {
        myDFPlayer.playFolder(3,2);
        delay(100);
        Serial.println("KLM_3");
        }
        else if(customKey=='3')
      {
        myDFPlayer.playFolder(3,3);
        delay(100);
        Serial.println("PTM_3");
        customKey=0;
        }
        else if(customKey=='4')
      {
        myDFPlayer.playFolder(3,4);
        delay(100);
        Serial.println("ALP_3");
        customKey=0;
        }
        else if(customKey=='5')
      {
        myDFPlayer.playFolder(3,5);
        delay(100);
        Serial.println("KTM_3");
        customKey=0;
        }
        else if(customKey=='6')
      {
        myDFPlayer.playFolder(3,6);
        delay(100);
        Serial.println("IDK_3");
        customKey=0;
        }
        else if(customKey=='7')
      {
        myDFPlayer.playFolder(3,7);
        delay(100);
        Serial.println("EKM_3");
        customKey=0;
        }
        else if(customKey=='8')
      {
        myDFPlayer.playFolder(3,8);
        delay(100);
        Serial.println("THR_3");
        customKey=0;
        }
        else if(customKey=='9')
      {
        myDFPlayer.playFolder(3,9);
        delay(100);
        Serial.println("PKD_3");
        customKey=0;
        }
        else if(customKey=='a')
      {
        myDFPlayer.playFolder(3,10);
        delay(100);
        Serial.println("MLP_3");
        customKey=0;
        }
        else if(customKey=='b')
      {
        myDFPlayer.playFolder(3,11);
        delay(100);
        Serial.println("KZD_3");
        customKey=0;
        }
        else if(customKey=='c')
      {
        myDFPlayer.playFolder(3,12);
        delay(100);
        Serial.println("WYD_3");
        
        customKey=0;
        }
        else if(customKey=='d')
      {
        myDFPlayer.playFolder(3,13);
        delay(100);
        Serial.println("KNR_3");
    
        customKey=0;
        }
        else if(customKey=='e')
      {
        myDFPlayer.playFolder(3,14);
        delay(100);
        Serial.println("KKD_3");
        
        customKey=0;
        }
  
}}}

If you had taken the advice offered in post #5 and formatted your code properly, you would see that only the statement catCount = 1 is associated with that if() statement and cat2() is always executed.


  if (customKey == 'M')
  { ++catCount;
    if (catCount == 4)
      catCount = 1;
    cat2();
  }

this error is repeated in all your cat2() cat3() and cat4() functions. These functions are basically the same and could be reduced to one function with the category (?) and text printed being variables.