Enter function using keypad

Hello, I have previously made a post about a project I'm creating for school using arduinos and have run into another issue that I cannot solve. My final project will be a jukebox, but for now, I want to make an enter key using a keypad, LCD, and Mega. The idea is that after entering a certain set of numbers (such as 123) and using the enter function to play a certain song, but for now, I just want the LCD to clear and display the letter 'a'. I cannot figure out how to make an enter key for a singular input and have tried using multiple variations of case statements and if statements.

Below is a diagram of my setup, an image of my setup, and my current code. Any help would be appreciated. Thank you!


//this is test code trying to make an enter button ONLY

#include <Keypad.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

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

uint8_t rowPins[ROWS] = { 23, 25, 27, 29 }; // Pins connected to R1, R2, R3, R4
uint8_t colPins[COLS] = { 31, 33, 35, 37 }; // Pins connected to C1, C2, C3, C4

int cursor = 0;

int numbers[] = {};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup()
{ 
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.blink();
  lcd.setCursor(0,0);
}

void loop()
{
  char key = keypad.getKey();

  /*if (key != NO_KEY && cursor < 16) 
  {
    lcd.print(key);

    switch (key)
    {
      default:
        break;

      case '*':
        lcd.setCursor(0, 1);
        lcd.clear(); // Clear the display
        break;
    }*/

    /*switch (key == '1' && key == '2' && key != '3' && key != '4' && key != '5' && key != '6' && key != '7' && key != '8' && key != '9' && key != '0' && key != 'A' && key != 'B' && key != 'C' && key != 'D')
    {
      case '#':
        lcd.setCursor(0, 1);
        lcd.clear(); // Clear the display
        lcd.print("a");
        break;
    }

    switch (key)
    {
      //if (key == '1' && key == '2' && key != '3' && key != '4' && key != '5' && key != '6' && key != '7' && key != '8' && key != '9' && key != '0' && key != 'A' && key != 'B' && key != 'C' && key != 'D')
      //{
        case '#':
        lcd.setCursor(0, 1);
        lcd.clear(); // Clear the display
        lcd.print("a");
        break;
      //}

       default:
        //if (key != NO_KEY && cursor < 16) 
        break;
    }*/
  //}
  if (key != NO_KEY && cursor < 16) 
  {
    lcd.print(key);

    if (key == '*')
    {
      lcd.setCursor(0, 1);
      lcd.clear(); // Clear the display
    }
  }

  if (key == '#') 
  {
    if (key == '1')
    {
      lcd.print(key);
      lcd.setCursor(0, 1);
      lcd.clear(); // Clear the display
      lcd.print("a");
    }
  }
}

Think these two steps through, a bit more carefully.

  if (key == '#') 
  {
    if (key == '1')

but for now, I just want the LCD to clear and display the letter 'a'.

No if statements are needed for that.

I'm not experienced with making code for anything arduino related, so this is a bit difficult for me, sorry. I'm used to making code for Unity haha so this is entirely new for me.

Would I use an else statement instead of an if statement for the 1 key?

It is not clear what you are trying to do. Which key is supposed to be the enter key?

The hashtag button. Sorry for not clarifying that initially

The way a key entry routine typically works is by buffering a set of characters until a certain "terminator" is received. In your example it looks like '#' is the terminator. Here's an example (not guaranteed to compile or work properly...)

void getInput(char key)
{
static char buffer[100] = {0};
static int i = 0;

if (key == '#')
{
  buffer[i] = 0;
  i = 0;
  // Process buffer here...
  Serial.println(buffer);
}
else
{
  buffer[i] = key;
  i++;
  if (i == 100)
  {
    // Wrap around or do something more intelligent
    i = 0;
  }
}
}
if (key == something) {
do_some_things();
}

Yeah the '*' key is just supposed to clear the display in case there's a mistake when entering anything. It's not supposed to do anything related to the enter button though if that makes sense. I'll try out your code, thank you for your help :--)

There are plenty of keypad tutorials on line. Look for one that enters and checks a PIN code or password.

I'm able to do that, but I want to make an enter button instead of just typing a number and then the code automatically does stuff. I want it so that when the # key is pressed, it analyzes what's displayed on the LCD (preferably multiple characters), then plays a song based on the set on characters

I can key a certain key to do certain things but I want it so that the enter keys looks at what's displayed and does stuff based on the characters displayed

That's why the * button works as a clear display button but not the # button as an enter button, but I can't figure out why

See post #2 for a snippet of your code that can't possibly do anything useful.

I've seen many for the keyboard library, but haven't found anything for the keypad library that does something similar to what I'm trying to do. Are there any sources that you would recommend?

I tried switching them before this. I deleted all of my failed attempts and commented out my attempts at using case statements. I actually have reference code that does something almost identical to what I'm trying to do but for the life of me can't figure out how they got their code to work. It's on GitHub so there's likely more code involved that wasn't uploaded

"key" cannot be equal to two different things at the same time.

Oh, I didn't know that, thanks for letting me know! That reminds me that I attempted to make a char that declares specifically the hashtag key instead of all keys, but have been unsuccessful at that as well. Is that possible to do or is that only possible by making another function? I'm only experienced at making simple games in Unity and any obvious arduino logic goes over my head sorry

If it helps, I attempted this

char hashtagKey = (key == #);

or something similar to that

I just realized I misread your post, sorry. I have not heard of terminators and will be looking into them! I'm concerned that any previously cleared characters (like typing 12 and then clearing using the * button) will be stored and that those characters will be read despite being cleared. Is that a possibility or will the terminator only read the character displayed on the LCD instead of the characters typed since the program started running?

I recommend to stop wasting your time guessing what C/C++ code looks like. No wonder that you are struggling.

There are plenty of language guides and coding tutorials to study.

The terminator is nothing more than the character you choose to indicate "I'm done, now process that data." The example I showed is literally that: an example of how to input a string of characters. It's up to you to decide how to use it.

I'm certified in Unity programming, so I know what C++ code looks like in general, especially for code used with Unity, but I do not know what Arduino specific code looks like. I'm not a pro with coding by any means because I'm still a high school student, and I'm struggling with this since it's very advanced and new to me.

I just don't know how to code with arduinos well and don't know if there's any methods that I'm unaware of that could work with what I'm trying to do, like with the terminators that cedarlakeinstruments mentioned.