// 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>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
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
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
Serial.begin(9600);
Serial.print("Enter the duration in decimal value: ");
Serial.println(" ");
float relay_duration = getKeypadFloat();
Serial.println(" ");
Serial.print("The value is ");
Serial.print(relay_duration );
}
void loop()
{
}//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;
}//getKeypadFloat