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);
}
}
}
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.
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;
}