I'm able to set the Timer based mode. But not able to proceed to setting the time.
#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 );
unsigned long TimeCount = 0;
const unsigned long LongPress = 300;
bool ModeAcquired = true;
bool readytoGetTimer = true;
const byte inputMaxSize = 5;
static char timer[inputMaxSize + 1];
static byte index1;
bool isTimerBuilt = true;
void setup()
{
lcd.begin(LCD_COLS, LCD_ROWS);
//prevMillis = millis(); // get the time at the start
getDefMode1();
}
void loop()
{
if (keypad.getKeys()) // check for keypad activity
{
// we won't handle multiple keypresses, just single ones, so just key index 0
const byte key = keypad.key[0].kchar;
const byte state = keypad.key[0].kstate; // IDLE, PRESSED, HOLD, RELEASED
switch (key)
{
case '#': getMode1(state); break;
case 'D': setMode1(); setTime(); break;
}
}
}
void getMode1(const byte state)
{
static unsigned long pressedTime; // static so the value is remembered like a global
if (state == PRESSED)
{
pressedTime = millis();
}
else if (state == RELEASED)
{
if (millis() - pressedTime > LongPress)
{
handleLongPress();
}
else
{
handleShortPress();
}
}
}
void handleShortPress()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Mode:");
lcd.setCursor(5, 0);
lcd.print("TimerBased");
ModeAcquired = false;
//Serial.println("short");
}
void handleLongPress()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Mode:");
lcd.setCursor(5, 0);
lcd.print("SensorBased");
ModeAcquired = true;
//Serial.println("long");
}
void setMode1()
{
if (!ModeAcquired)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Timer Mode Set!");
readytoGetTimer = true;
}
else
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sensor Mode Set!");
}
}
void getDefMode1()
{
lcd.setCursor(0, 0);
lcd.print("Mode:");
lcd.setCursor(5, 0);
lcd.print("SensorBased"); //default mode
}
unsigned long temptime; // temporary time created by key entry
void buildTime()
{
if (readytoGetTimer)
{
if (keypad.getKeys()) // check for keypad activity
{
// we won't handle multiple keypresses, just single ones, so just key index 0
const byte key = keypad.key[0].kchar;
const byte state = keypad.key[0].kstate; // IDLE, PRESSED, HOLD, RELEASED
if (state == PRESSED)
{
// if not 5 characters yet
if (index1 < inputMaxSize)
{
// add key to userinput array and increment counter
if ( key >= '0' && key <= '9' ) // key is of type char and has a value between 0 and 9 so do something with it.
{
timer[index1++] = key;
timer[index1] = '\0';
temptime = atol(timer);
char displayText[16] = "";
snprintf(displayText, sizeof(displayText), "Set Time: %5lu", temptime);
lcd.setCursor(0, 0);
lcd.print(displayText);
}
}
else
{
timerWarning();
}
}
}
}
}
void setTime()
{
if (isTimerBuilt)
{
if (index1 == 0)
{
invalidTime();
}
else
{
lcd.clear();
lcd.print("Time Set: ");
for (byte i = 0; i < index1; i++)
{
lcd.print(timer[i]);
isTimerBuilt = false;
}
unsigned long TimeCount = atol(timer);
Serial.print(TimeCount);
//EEPROM.put(0, count); // <<<<<<<<< save to EEPROM here
}
}
}
void invalidTime()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Invalid Time!!");
lcd.setCursor(0, 1);
//lcd.print("Press A"); // suggesting the user to enter the time again
isTimerBuilt = false;
}
void timerWarning()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("5 Digits Only!!"); // warning for the user if more than permitted digits are entered
lcd.setCursor(0, 1);
//lcd.print("Press A"); // suggesting the user to enter the time again
isTimerBuilt = false;
}