Arduino 4x4 Keypad Coding error

Hi,

I am doing a design project for one of my courses, and one of our tests for the keypad is to press a button on the keypad, and have it output the location of the button on the keypad (row, col). My code is below, but I keep getting an error saying “expected unqualified-id before ‘return’” for the last line of code that says ‘return 0’. I can’t seem to find anything to fix this error so any help would be greatly appreciated!

#include <Key.h>
#include <Keypad.h>

#include <keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
int index;
int LIST_MAX=16;

char hexaKeys[ROWS][COLS]={
{‘1’,‘2’,‘3’,‘A’},
{‘4’,‘5’,‘6’,‘B’},
{‘7’,‘8’,‘9’,‘C’},
{’*’,‘0’,’#’,‘D’}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup() {
Serial.begin(9600);
}

void loop() {
char customKey = customKeypad.getKey();
for (byte i=0; i<LIST_MAX; i++) {
if (hexaKeys[ROWS][COLS] == customKey) {
index = hexaKeys[ROWS][COLS];
Serial.print(index);
return i;
}
}
}
return 0;
}

See comments:

#include <Key.h>
#include <Keypad.h>
#include <keypad.h>

const byte ROWS = 4;
const byte COLS = 4;
int index;
//int LIST_MAX = 16;  // this causes an error.  Need to change the library 
                      //if this value needs changed


char hexaKeys[ROWS][COLS] =
{
   {'1', '2', '3', 'A'},
   {'4', '5', '6', 'B'},
   {'7', '8', '9', 'C'},
   {'*', '0', '#', 'D'}
};


byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};


Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);


void setup()
{
   Serial.begin(9600);
}


void loop()
{
   char customKey = customKeypad.getKey();
   for (byte i = 0; i < LIST_MAX; i++)
   {
      if (hexaKeys[ROWS][COLS] == customKey)
      {
         index = hexaKeys[ROWS][COLS];
         Serial.print(index);
         //return i;  // this does nothing.  Error = "return-statement with a value, 
                      //in function returning  'void".   loop() is the void function.
      }
   }
   //}   } out of place
   //return 0;  // this does nothing.  Error = "return-statement with a value, 
                 //in function returning 'void".  loop() is the void function.
}

Code compiles with no errors or warnings, though I have no idea if it does what you want.

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called “copy error message”. Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

if(hexaKeys[ROWS][COLS]

That would be out of bounds.

It’s hard to tell what the for loop is doing there

Hi, thank you for the responses. See below for proper formatting:

#include <Key.h>
#include <Keypad.h>

#include <keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
int index;
int LIST_MAX=16;

char hexaKeys[ROWS][COLS]={
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  char customKey = customKeypad.getKey();
    for (byte i=0; i<LIST_MAX; i++) {
    if (hexaKeys[ROWS][COLS] == customKey) {
      index = hexaKeys[ROWS][COLS];
      Serial.print(index);
      return i;
  }
}
}
return 0;
}

Now, here is the error message:

Arduino: 1.8.12 (Windows 10), Board: "Arduino Uno"

In file included from C:\Users\dotKa\OneDrive\Documents\Arduino\keypadTest3\keypadTest3.ino:2:0:

C:\Users\dotKa\OneDrive\Documents\Arduino\libraries\Keypad\src/Keypad.h:69:18: error: expected unqualified-id before numeric constant

 #define LIST_MAX 10  // Max number of keys on the active list.

                  ^

C:\Users\dotKa\OneDrive\Documents\Arduino\keypadTest3\keypadTest3.ino:8:5: note: in expansion of macro 'LIST_MAX'

 int LIST_MAX=16;

     ^~~~~~~~

C:\Users\dotKa\OneDrive\Documents\Arduino\keypadTest3\keypadTest3.ino: In function 'void loop()':

C:\Users\dotKa\OneDrive\Documents\Arduino\keypadTest3\keypadTest3.ino:34:14: warning: return-statement with a value, in function returning 'void' [-fpermissive]

       return i;

              ^

C:\Users\dotKa\OneDrive\Documents\Arduino\keypadTest3\keypadTest3.ino: At global scope:

keypadTest3:38:1: error: expected unqualified-id before 'return'

 return 0;

 ^~~~~~

keypadTest3:39:1: error: expected declaration before '}' token

 }

 ^

exit status 1
expected unqualified-id before 'return'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Still not sure how to fix the error, and since this is my first time coding a keypad I don’t know if this code will do what I want it to do, which is return the location of the button pressed on the keypad.

int LIST_MAX=16;

Oops

if (hexaKeys[ROWS][COLS]

That’s still out of bounds, and I still can’t see what the for loop is for.

index = hexaKeys[ROWS][COLS]; Again, out of array bounds.

Do you know how I would fix it to be within the bounds?

I don’t even know what you’re trying to do, so a fix is some way off.

The goal of the code is to find the location of the button that is being pressed on a 4x4 keypad. So if the button for '6' was pressed, I want the output to be (2,3), meaning 2nd row and 3rd column. I based my code off of other codes that I saw but I'm new to coding keypads so I don't know if I have it right. Any help would be greatly appreciated.

So, the library gives you a character value, and you want to convert that row/column?

A couple of nested loops should do this easily.

Why are you using the library in the first place, since the only purpose of the library is to hide the rows and columns, and simply give you a character code?

Which library are you referring to? I thought the keypad.h library was necessary for all keypad coding.

Yes, I was referring to the keypad library.

Okay, is there a library I should be using instead? I also don't know how to get the key without using the keypad library. Will I need to create an array of the keys on the keypad to index into during the nested loops? I'm thinking that I need to create an array associated to the keys of the keypad, and then read the key that is being pressed, and then use that value that was read to find the location of the matching element in the array. That's my thought process, but I could be totally wrong. Please let me know your thoughts.

Carry on using the library if it makes it easier for you, but you need a pair of nested for loops, iterating over the rows and columns, to search for the input character.

TheMemberFormerlyKnownAsAWOL:
Carry on using the library if it makes it easier for you, but you need a pair of nested for loops, iterating over the rows and columns, to search for the input character.

switch.. case would be more structured and easier to manage.
Nested if statements look messy and indenting makes them hard to follow.
Tom... :slight_smile:

A switch would introduce another level of mapping that could introduce errors, if the keymap changed, because of duplicated information.
A for loop search would maintain consistency automatically.

There would be no nested if statements; I can’t imagine why you thought there would be.

Hi,
Use the switch..case with the keyboard library...
My missss for .. if..
Tom.. :slight_smile:

kmaxwell:
The goal of the code is to find the location of the button that is being pressed on a 4x4 keypad. So if the button for ‘6’ was pressed, I want the output to be (2,3), meaning 2nd row and 3rd column. I based my code off of other codes that I saw but I’m new to coding keypads so I don’t know if I have it right. Any help would be greatly appreciated.

#include <Keypad.h>


const byte ROWS = 4;
const byte COLS = 4;


char hexaKeys[ROWS][COLS] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};


byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};


Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup()
{
  Serial.begin(115200);
}
void loop()
{
  char customKey = customKeypad.getKey();
  if (customKey != NO_KEY)
  {
    for (int row = 0; row < ROWS; row++)
      for (int col = 0; col < COLS; col++)
        if (hexaKeys[row][col] == customKey)
        {
          Serial.print(row);
          Serial.print(',');
          Serial.println(col);
        }
  }
}
{
          Serial.print(row + 1);
          Serial.print(',');
          Serial.println(col + 1);
        }

:wink: