// with help from https://stackoverflow.com/questions/39232634/how-to-input-a-multi-digit-integer-into-an-arduino-using-a-4x4-keypad
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
char msgs[]= {}
char text1[] = "Welcome To Relay Control Project";
char text2[] = "By Gautam Kumar";
char text3[] = "Enter the Duration:";
char text4[] = "Select The Relay Number(1-3):";
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
Serial.begin(9600);
Wire.begin(); // Initialize the Wire library for I2C communication
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.backlight();
lcd.setBacklight(HIGH);
lcd.setCursor(1,1);
lcd.print(text2);
for (int i = 0; i <= strlen(text1) - 16; i++) //From 0 to upto n-16 characters supply to below function
{
showLetters(0, i);
}
}
void loop()
{
delay (1000);
lcd.clear();
lcd.setCursor(0,0);
for (int i = 0; i <= strlen(text3) - 16; i++) //From 0 to upto n-16 characters supply to below function
{
time_dur(0, i);
}
Serial.print("Enter the duration in decimal value: ");
Serial.println(" ");
float relay_duration = getKeypadFloat();
Serial.println(" ");
Serial.print("The value is ");
lcd.setCursor(0,1);
lcd.print("relay_duration");
delay(1000);
}//loop
float getKeypadFloat()
{
int debug = 1;
float beforeDecimal = 0; // the number accumulator
float afterDecimal = 0;
byte howManyDecimals = 0;
float finalValue;
int keyval; // the key press
int isNum;
int isStar;
int isHash;
Serial.println("Enter digits,then * as decimal point, then decimals, and # to end");
if (debug)Serial.print("So far: ");
// accumulate the part before the decimal
do
{
keyval = keypad.getKey(); // input the key
isNum = (keyval >= '0' && keyval <= '9'); // is it a digit?
if (isNum)
{
if (debug)Serial.print(keyval - '0');
beforeDecimal = beforeDecimal * 10 + keyval - '0'; // accumulate the input number
}
isStar = (keyval == '*');
if (debug)if (isStar) Serial.print(".");
} while (!isStar || !keyval); // until a * or while no key pressed
// accumulate the part after the decimal
do
{
keyval = keypad.getKey(); // input the key
isNum = (keyval >= '0' && keyval <= '9'); // is it a digit?
if (isNum)
{
if (debug)Serial.print(keyval - '0');
afterDecimal = afterDecimal * 10 + keyval - '0'; // accumulate the input number
howManyDecimals++; // increment for later use
}
isHash = (keyval == '#');
} while (!isHash || !keyval); // until # or while no key pressed
finalValue = beforeDecimal + (afterDecimal / pow(10, howManyDecimals));
if (debug) Serial.println(" ");
if (debug)Serial.print("Returning: ");
if (debug)Serial.println(finalValue, howManyDecimals);
return (finalValue*1000);
}//getKeypadFloat
void showLetters(int printStart, int startLetter)
{
lcd.setCursor(printStart, 0);
for (int i = startLetter; i <= startLetter + 15; i++) // Print only 16 chars in Line #2 starting 'startLetter'
{
lcd.print(text1[i]);
}
lcd.print(" ");
delay(250);
}
void time_dur(int printStart, int startLetter)
{
lcd.setCursor(printStart, 0);
for (int i = startLetter; i <= startLetter + 15; i++) // Print only 16 chars in Line #2 starting 'startLetter'
{
lcd.print(text3[i]);
}
lcd.print(" ");
delay(250);
}