I am new I took a code of keypad in arduino. Arduino code is not working in setup area but if I place the same code in loop are it works why? Kindly guide me. Here is the code.

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
byte rowPins[ROWS] = {2, 3, 4,5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

void setup(){
  char customKey = customKeypad.getKey();

  if (customKey){

void loop(){

  • Please note that setup() runs just once while loop() is repeated forever (see e.g. here) that's an important difference.
    – Ghanima
    Commented Jun 11, 2019 at 15:40
  • but it must run once, it is not . Commented Jun 11, 2019 at 15:49
  • It is. It's just running waaaaay faster than you can press a key.
    – Majenko
    Commented Jun 11, 2019 at 15:52
  • 1
    Ok thanks alot. Commented Jun 11, 2019 at 16:42
  • 1
    To illustrate what the are talking about, try putting an else on your if.... I.e. else { Serial.println("no key press"); } . As a bonus you will get a free game called "can I beat the no key press message?" :-)
    – GMc
    Commented Jun 11, 2019 at 22:54


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.