Loop in a loop

I am working on a keypad code. I am testing for either a ‘g’ or an ‘!’. If the ‘g’ is pressed it should drop into keymap3 that will stay there until a second ‘g’ is pressed and then resume testing. Currently the ‘!’ will go to the keymap2 for only one character, which is what it should do. The ‘g’ does the same for kemap3 but doesn’t stay there. I have included the INO that I currently have. The area between the lines of ************** is where I think my problem is.

I appreciate any help.


DMU_start-up.ino (17.9 KB)

void loop() {//keypad

  char key = getKey();
  if (key != 0) { //if the character is not 0 then it's a valid key press

}//end void loop

What is the point of reading the key, if you don't care what key was pressed?

What is the point of all the blank lines IN the function, when there are no blank lines BETWEEN functions?

There IS a Keypad library that really does know how to read keypads. Unlike your code...

        text = String(key);

What is the point of making a String of ONE character?

        int str_len = text.length() + 1;
        char char_array[str_len];
        text.toCharArray(char_array, str_len);

That pisses away a tremendous amount of resources to create a 2 element array with the key value in the first position and a NULL in the second.

        sprintf(abuffer, char_array);

That must be the most expensive way possible to get one character from one array into another array that is far larger than it needs to be to hold one character.

        dsply.scrollString(abuffer);  //I use sprintf to format data for display, thus show the results here

Scrolling one character must be super exciting. Please post a youtube video. I need a nap.

        if (String(key) == "g")

What is wrong with:

        if (key == 'g')


          if (keyCount = 2)

The result of the assignment operator is the value assigned, so this is equivalent to

          keyCount = 2;
          if (2)

which really looks useless.

The 'g' does the same for kemap3 but doesn't stay there.

I didn't understand this statement before looking at your code. Now that I have, this statement makes even less sense. keymap3 is an array. The code can't possibly "stay there".

You should not have code in your getKey() function except the code necessary to figure out which key has been pressed and return its value to the calling function.

If you separate the activities into short single-purpose functions it will be much easier to develop and debug your program. Have a look at Planning and Implementing a Program


I understand this is not the best way to code but it is the best I can do currently. The reason for 3 keymaps was I wanted it to work independntly with each one until I get it to do the "caps lock" function. Once each works I will remove the keymap3 and refer to only keymap2 for both 'g' and '!'. Some of the code is there to test the ability to pass the letter to an 8 character 15-segment led and a New Haven display. There is a much larger menu code being worked where the user will select menu items which will have to appear either on the New Haven display or pass one character at a time, right to left on the 8 character display and be stored (for a total of 32 characters).

The keypad we have built is 7 rows and 10 columns. and I was unable to make the library do what is needed. Maybe a failing in my part.

I am trying my best to just get the caps-lock to work. I will work to get the coding cleaner (including removing blank lines) and more compact after each part works. Probably not the best way but I haven't worked with C++ in many years (forgot most it) and Arduino is getting easier but I have a long way to go.

I got the keypad.h to work. Research helps.

Now I am back to the original problem. I have 2 hexakeys layouts. I want caps-lock when the ‘g’ key is pressed and stay on the second hexakey until i press ‘g’ again.

DMU_keypad_using_keypad.h.ino (1.45 KB)

Please post your program code in your next Reply so we don’t have to download it.

When posting code please use the code button </>

so your code looks like this

and is easy to copy to a text editor See How to use the Forum



#include <Keypad.h>
const byte ROWS = 7; 
const byte COLS = 10; 

char hexaKeys1[ROWS][COLS] = {
  {'a', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'},
  {'b', 'h', 'm', 'A', 'B', 'C', 'D', 'E', '\0', '\0'},
  {'c', 'i', 'n', 'F', 'G', 'H', 'I', 'J', '\0', '\0'},
  {'d', 'j', 'o', 'K', 'L', 'M', 'N', 'O', 't', 'z'},
  {'e', 'k', 'p', 'P', 'Q', 'R', 'S', 'T', 'v', '&'},
  {'f', 'l', 'q', 'U', 'V', 'W', 'X', 'Y', 'x', '!'},
  {'g', '\0', 'r', 'Z', '.', ',', '\\', '?', '\0', '\0'} //'g' is CONT. Keyboard.press() functions as if key were pressed and held. Keyboard.release() to turn off CONT??
char hexaKeys2[ROWS][COLS] = {
  {'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'},
  {'\0', '\0', '\0', '1', '2', '3', '\0', '\0', '\0', '\0'},
  {'\0', '\0', '\0', '4', '5', '6', '\0', '\0', '\0', '\0'},
  {'\0', '\0', '\0', '7', '8', '9', '\0', '\0', 's', 'y'},
  {'\0', '\0', '\0', '0', '\0', '\0', '\0', '\0', 'u', '

  {’\0’, ‘\0’, ‘\0’, ‘\0’, ‘\0’, ‘\0’, ‘\0’, ‘\0’, ‘w’, ‘\0’},
  {’\0’, ‘\0’, ‘\0’, ‘\0’, ‘\0’, ‘\0’, ‘\0’, ‘#’, ‘\0’, ‘\0’}

byte rowPins[ROWS] = {22, 24, 26, 28, 30, 32, 34};    //ROWS 1,2,3,4,5,6,7
byte colPins[COLS] = {36, 38, 40, 42, 44, 46, 48, 23, 25, 27}; //COLS 1,2,3,4,5,6,7,8,9,10

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

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

Use the .begin() method to change key maps as required.