KeyPad - issues start when using a loop inside the switch

I have been working on a LED Cube. However, this time, I added a KeyPad onto this - such that a keypress on the keypad will get me the particular pattern right away. All was seemingly okay. But then I ran into an issue.

The chaserDisplay() - This uses a loop which breaks after a key on the keypad is pressed.

But once I hook it up (enable the Case ‘4’ in the Loop which calls this if key 4 is pressed), the keypad seems to fail. There are other issues as well (like a few colors going wayward if I activate this case ‘4’). But for now, let me focus on the Keys and KeyPad.

Here is the Loop Code

void loop() 
{
  // put your main code here, to run repeatedly:
  static char switchKey = '\0';
  char customKey = customKeyPad.getKey();
  if(customKey != '\0')
  {
    switchKey = customKey;
    pixels.clear();
  }
   switch(switchKey)
    {
      case '1':{Serial.println("RAINDROPS"); rainDrops();break;}
      case '2':{Serial.println("Horizontal Planes");horizontalPlanes();break;}
      case '3':{Serial.println("Vertical Planes");verticalPlanes();break;}
      case '4':{Serial.println("Chaser Light");chaserDisplay(0,0,0);switchKey='z';break;}
      case '5':{Serial.println("Single Columns");randomLines(); break;}
      case '6':{Serial.println("Cube Borders");break;}
      case '7':{Serial.println("GLOW ALL");break;}
      case '8':{Serial.println("Fountain");displayFountain();break;}
      case '9':{Serial.println("Squares");break;}
      case '0':{Serial.println("RESET");break;}
      case 'A':{Serial.println("All Effects");break;}
      case 'B':{Serial.println("Default Color Settings");break;}
      case 'C':{Serial.println("PINK Color");break;}
      case 'D':{Serial.println("Blue Color");break;}
      case '*':{Serial.println("Increase Speed");break;}
      case '#':{Serial.println("Decrease Speed");break;}
    }
}

And this is the code for the chaserDisplay() -

void chaserDisplay(int xpos, int ypos,int zpos)
{
  int arrChaser[4][4][4]
  {
      {
        {0,1,2,3},
        {28,29,30,31},
        {32,33,34,35},
        {60,61,62,63}
      },
      {
        {4,5,6,7},
        {24,25,26,27},
        {36,37,38,39},
        {56,57,58,59}
      },
      {
        {8,9,10,11},
        {20,21,22,23},
        {40,41,42,43},
        {52,53,54,55}
      },
      {
        {12,13,14,15},
        {16,17,18,19},
        {44,45,46,47},
        {48,49,50,51}
      }
   };
   
    
    while(customKeyPad.getKey()=='\0')
    {
       switch(random(0,6))
       {
        case 0:
          if(xpos > 0) {xpos--;break;}
        case 1:
          if(xpos < 3) {xpos++;break;}
          xpos--; break;
        case 2:
          if(ypos > 0) {ypos--;break;}
        case 3:
          if(ypos < 3) {ypos++;break;}
          ypos--; break;
        case 4:
          if(zpos > 0) {zpos--;break;}
        case 5:
          if(zpos < 3) {zpos++;break;}
          zpos--; break;
       }
    pixels.setPixelColor(arrChaser[xpos][ypos][zpos], pixels.Color(50,50,50));/*random(150),random(150), random(150)));-*/
    pixels.show();
    delay(SPEED);
    pixels.clear();
    delay(SPEED);
    }
}

Questions:

  1. How do I loop in the loop() without having the need to keep pressing keys - Is my approach correct?
  2. How do I exit the loop in the chaserDisplay() - I need it to exit only when a Key on the Keypad is pressed. So again, is the approach correct?
  3. Why the KeyStrokes are not getting interpreted easily (on the loop())? I mean, I have to keep pressing some of the keys that are not associated for a while (number of times) to halt the execution of a case and then the other seems to work fine.

It might also be helpful to note that I am using Adafruit’s NeoPixel Library and the KeyPad library on Arduino Nano (3.0, older version).

You use delay(SPEED) twice. In that time other useful things could be done. See BlinkWithoutDelay for replacing loops inside loop() and use a state variable to select the action to be taken instead of toggling the LED.

DrDiettrich:
You use delay(SPEED) twice. In that time other useful things could be done. See BlinkWithoutDelay for replacing loops inside loop() and use a state variable to select the action to be taken instead of toggling the LED.

Thank you for the lead. Sorry for the delay in replying.
I searched for BlinkWithoutDelay and found the github page here - Arduino - BlinkWithoutDelay
Can you please clarify what a state variable is ? And how can I use it to select the action (as my action would probably be toggling the LED).

In your code switchKey is a state variable, telling in which state the program is and what should be done then.

BlinkWithoutDelay is one of the example projects available in the IDE. Look what other examples may be helpful for you.

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