In case 'D' add a statement to set "a" to false ( to clear it as aarg said).
In default, add a condition "if ( a == true ) . . . " so that numbers are accepted only after "a" has been set to true ( to check it as aarg said).
#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & config exapander chip
// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {6, 7, 8, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 12, 11}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
bool a = true;
void setup()
{
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.setCursor(3, 0);
lcd.print(" PRIYA");
lcd.setCursor(3, 1);
lcd.print("ELECTRONICS");
delay(100);
for (int positionCounter = 0; positionCounter < 40; positionCounter++)
{
lcd.scrollDisplayLeft();
delay(150);
}
delay(800);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set Count:");
}
void loop()
{
const byte entryMaxSize = 5;
static char count[entryMaxSize];
// index
static byte x = 0;
char key = keypad.getKey();
if (key != NO_KEY) // Check for a valid key.
{
switch (key)
{
case 'A':
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set Count:");
// reset the counter
x = 0;
a = true;
break;
case 'D':
if (a){
lcd.clear();
lcd.print("Count Set:");
for(byte i=0;i<x;i++) {
lcd.print(count[i]);
a = false;
}
}
break;
default:
if (a)
{
// if not 5 characters yet
if (x < entryMaxSize)
{
// add key to userinput array and increment counter
count[x++] = key;
}
break;
}
}
}
}
But I am not able to print the digits onto the lcd as I enter them. Only after I press the 'D' key which is the enter key in my case, I am able to see the entire count. Can you suggest something? Thanks.
Just keep the questions coming and you will surely get help. Of course, it would be good if you also regard this project as a learning experience and demonstrate some attempts to solve the individual problems yourself.
6v6gt:
Of course, it would be good if you also regard this project as a learning experience and demonstrate some attempts to solve the individual problems yourself.
Surely !!
Things I want to implement:
If the user presses the key 'D' which is the enter key in our case, lcd must blink invalid count and direct him to the set count option where he gets to enter a valid count.
If user tries to enter the 6th digit while entering the 5 digit count lcd must display "5 digits only!!", while also displaying the count he has entered with only 5 digits. The count would be on the first line and the warning message on the second line.
If user tries to enter the 6th digit while entering the 5 digit count lcd must display "5 digits only!!", while also displaying the count he has entered with only 5 digits. The count would be on the first line and the warning message on the second line.
I have implemented the following, a little modified though.
default:
if (a)
{
// if not 5 characters yet
if (x < entryMaxSize)
{
// add key to userinput array and increment counter
count[x++] = key;
lcd.print( key ) ; // new print statement <<<<<<<<<<<<<<<<
}
else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Only 5 Digits!!");
lcd.setCursor(0, 1);
lcd.print("Press A");
}
}
break;
}
}
}
I have implemented the first part where invalid count is displayed if user pressed the enter w/o entering the count.
case 'D':
if (a) {
if (x == 0)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Invalid Count!!");
lcd.setCursor(0, 1);
lcd.print("Press A"); // suggesting the user to enter the count again
a = false;
}
else {
lcd.clear();
lcd.print("Count Set:");
for (byte i = 0; i < x; i++) {
lcd.print(count[i]);
a = false;
}
}
}
break;
Moving on. I am facing a slight problem while entering the count. Characters on the keypad such as A,B,C,D,# are used for performing certain functions like setting count, enter key, setting time, setting a particular mode of operation. The '*' is going to be used as a decimal point. But while entering the 5 digit count not only numbers from 0 to 9 are taken as input but above characters as well. What shall I do prevent them from being taken as input?