-1

I want an Arduino code to give the output in following format on LCD display

If the user click 'A', 1, 2, 3 from the keypad, LCD should display Hi:1,2,3,

This is what I have tried but I cannot figure out a way to build the code as I am a beginner in arduino

#include <Keypad.h>
#include <LiquidCrystal.h>


const byte numRows= 4;
const byte numCols= 4;

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

byte rowPins[numRows] = {9,8,7,6};      // Pin Assign
byte colPins[numCols] = {5,4,3,2};      // Pin Assign

Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

// LCD
//                RS E  D4 D5 D6 D7
LiquidCrystal lcd(A0,A1,A2,A3,A4,A5);   // Pin Assign


void setup()
{   
    lcd.begin(16, 2);       
    lcd.clear();
    lcd.print("PUSH ANY KEY! ");
    lcd.cursor();
    lcd.blink();
}


void loop(){
    char keypressed = myKeypad.getKey();
    if (keypressed != NO_KEY){
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print(keypressed);
        lcd.print(':');
        lcd.setCursor(3, 0);

    }

}

1 Answers1

0

You may need to create a state machine, where the change in the state happens when you receive the 'A' char. So, while you do not receive the 'A' char, your state machine keeps in the busy-wait (while (keypressed != 'A') ).

Once the 'A' is received, you then are going to check the next 3 chars received, and verify if they match what you expect ('1', '2', '3').

If you receive it in any different order, then the loop breaks, and the verification fails at the statement if (i == 4). In other words, as soon as you receive a number that you do not expect, the loop breaks and the verification fails.

Here is what you could add in your loop() code:

void loop() {
  char expected[3] = { '1', '2', '3' };
  int i = 0;
  char keypressed = myKeypad.getKey();
  while (keypressed != 'A')
    ;

  while (i < 4) {
    for (i = 0; i < 4; i++) {
      if (keypressed != expected[i])
        break;
  }

  if (i == 4)
    lcd.print("Hi:1,2,3");
}
campescassiano
  • 809
  • 5
  • 18