Please help with keypad

Hi, I am hoping someone can help me with my code. I have a 4x4 keypad hooked up to an arduino and how my code works is the 'C' button clears the numbers I have put in and the '#' enters the value and stops the loop. However, I am wanting to have the 'A' button be like a reset function where after I press it, the loop will start over even after I have ended it with the '#' button. Could someone let me know how I can include that in my code. Thank you.

#include <Wire.h>                      
#include <LiquidCrystal_I2C.h>       

LiquidCrystal_I2C lcd(0x27,20,4);  
#include <Keypad.h>

const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns

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

byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; 
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2};

Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

int page_no = 1;

String page1_Q = "Enter Duct Size(in): ";

int page1_cursor;

//Value of each page
String page1_v;



void setup() {
  lcd.init();  
  lcd.begin(20,4);                 
  lcd.backlight();  
}

void loop() {


  if (page_no == 1) {

    lcd.setCursor(0, 0);
    lcd.print(page1_Q);        // Print the respective question
    lcd.setCursor(7, 1);
    lcd.print(page1_v);  // Print the answer to the question


    char key = keypad.getKey();

    if (key) {

      
      if (key == '#') page_no++;


      else if (key == 'C') {
        page1_v = "";
        page1_cursor = 1;
        for (int i = 0; i < 16; i++)
        {
          lcd.setCursor(i, 1);
          lcd.print(" ");
        }
      }

      

      else {
        page1_v += key;

        if (page1_cursor <= 16)
          page1_cursor++;
        
        }

        lcd.setCursor(7, 1);
        lcd.print(page1_v);
      }
      
    }
  
}

 

Something like this?

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <Keypad.h>

const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns

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

byte pin_rows[ROW_NUM] = {9, 8, 7, 6};
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2};

Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

int page_no = 1;

String page1_Q = "Enter Duct Size(in): ";

int page1_cursor;

//Value of each page
String page1_v;



void setup() {
  lcd.init();
  lcd.begin(20, 4);
  lcd.backlight();
}

void loop() {


  if (page_no == 1) {

    lcd.setCursor(0, 0);
    lcd.print(page1_Q);        // Print the respective question
    lcd.setCursor(7, 1);
    lcd.print(page1_v);  // Print the answer to the question


    char key = keypad.getKey();

    if (key) {


      if (key == '#') page_no++;


      else if (key == 'C') {
        page1_v = "";
        page1_cursor = 1;
        for (int i = 0; i < 16; i++)
        {
          lcd.setCursor(i, 1);
          lcd.print(" ");
        }
      }
      else if (key == 'A') {
        //Do the action You need //Railroader
      }
      else {
        page1_v += key;

        if (page1_cursor <= 16)
          page1_cursor++;

      }

      lcd.setCursor(7, 1);
      lcd.print(page1_v);
    }

  }

}

Yes but what would I put in that else if statement if I am wanting to start the loop over? That is what I am confused about.

Did You read the comment //Do the action You need?

I need to restart the loop even after pressing '#', how can this be done in that statement?

Then set the needed variables to suitable values. What about page1_v and page1_cursor? You might knock out key by key = " ";

Sorry can you show that on my code because I do not understand.

Rather not as I don't get the picture, how/what the code does. Tour word description doesn't give the picture.

The trick is to make the execution of the code go out like in a flushed toilet and then loop takes over. You can set key to nill, zero. Then loop will wait for a new key.

I'd change to a switch/case statement:

  char key = keypad.getKey();

  switch (key)
  {
  case '#': 
    page_no++; 
    break;

  case 'C':
        page1_v = "";
        page1_cursor = 1;
        lcd.setCursor(0, 1);
        lcd.print("               ");
        break;

  case 'A':
    // Do what you need to do here.
    break;

  case '0'..'9':
        page1_v += key;
        lcd.setCursor(7, 1);
        lcd.print(page1_v);
        if (page1_cursor <= 16)
          page1_cursor++;
        break;
  }

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