How to make keyboard and LCD work together

wiring and coding for keyboard and LCD not working.

void loop()
{
  // put your main code here, to run repeatedly:
  char key = keypad.getKey();

  if (key == '*')
  {
    int counter = 0;

    for (counter = 0 ; counter < 3;)
    {
      char key = keypad.getKey() ;
      if (key >= '0' && key <= '9')
      {
        values[counter] = key;
        lcd.print(key);
        counter++;
      }
    }
    // Process the three digits in "values" here
  }
}

i suggest making you keypad processing code more standalone. no doubt it will be just a component of you final code

not exactly sure why you need to prefix some entry with '*"? are you afraid of hitting a key by mistake?

why not always recognize a keycode and use '#' to accept the entered value, rather than counting values. '*' could be used to ignore/reset

and rather than capture the individual keycodes, why not construct a multi digit value as each numeric is entered.

  val = 10*val + key - '0';

this would at least allow < 3digit values

it would make sense that the entered values are immediately displayed. and the display would revert back to some normal display after the entry is accepted or ignored ('*"). the display mode would change when a numeric key is recognized

@gcjr can you suggest a sample code? I am unable to imagine it.

consider, simulating your getKey() with serial input of 123*88#9876*57#

resulting in

 Vol   72 ml
 Fill  70 ml
1
12
123
 Vol   72 ml
 Fill  70 ml
8
88
 Vol   72 ml
 Fill  88 ml
9
98
987
9876
 Vol   72 ml
 Fill  88 ml
5
57
 Vol   72 ml
 Fill  57 ml

code, also simulating LCD with serial monitor

struct Lcd {
    void backlight (void)  { }
    void clear (void)  { }
    void init  (void)  { }
    void setCursor  (int a, int b)  { }

    void print (char *s)  { Serial.println (s); }
} lcd;

// -----------------------------------------------------------------------------
int vol  = 72;
int fill = 70;

void
lcdUpdate (
    char *s0,
    char *s1 )
{
    lcd.clear ();

    lcd.setCursor (0, 0);
    if (s0)
        lcd.print (s0);

    lcd.setCursor (0, 1);
    if (s1)
        lcd.print (s1);
}

// -------------------------------------
void
dispUpdate (void)
{
    char s0 [40];
    char s1 [40];

    sprintf (s0, " Vol  %3d ml", vol);
    sprintf (s1, " Fill %3d ml", fill);
    lcdUpdate (s0, s1);
}

// -----------------------------------------------------------------------------
#define NO_KEY 0xFF 

char
getKey (void)
{
    if (Serial.available ())
        return Serial.read ();

    return NO_KEY;
}

// -------------------------------------
void
keyPad (void)
{
    static int val = 0;

    char c = getKey ();
    switch (c)  {
    case '0' ... '9':
        val = 10 * val + (c - '0');
        char s [40];
        sprintf (s, "%d", val);
        lcdUpdate (s, NULL);
        break;

    case '#':
        fill = val;
        // drop thru

    case '*':
        dispUpdate ();
        val       = 0;
        break;
    }
}

// -----------------------------------------------------------------------------
void
loop (void)
{
    keyPad ();
}

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

    lcd.init ();
    lcd.backlight ();

    dispUpdate ();
}

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