Can I use getkey() inside setup() ?

Can I use kpd.getkey() function inside setup() function for Arduino Mega 2560 R3? I am new to ardunio and I don’t have much idea. Basically,my requirement is, I want to switch on an electrical appliance on particular time(SWITCH ON time varies based on a user’s input through keypad). Before getting the SWITCH ON time, I am planning to get the CURRENT time . Reason for getting the CURRENT time - I am planning to use the Arduino’s inbuilt timer instead of RTC. So, every time when my mom switches on the arduino, she needs to enter the CURRENT TIME and SWITCH ON time for the electric rice cooker.

I am planning to get input twice from keypad using kpd.get() function and provide the value to set time() and Alarm.alarmOnce() functions written inside setup() function respectively. Will this approach work ? Or if you have a ready made sketch, please share. I appreciate your help!


 #include <Time.h> #include <TimeAlarms.h>
#include <kepad.h>
 void setup() {   Serial.begin(9600);
  char key = keypad.getKey();
 // Other part of code
  setTime(*,*,0,1,11,16); // set CURRENT TIME
Alarm.alarmOnce(*,*,0, switchonApp); 
//* Values are obtained from keypad
void loop() {
//Other code
void switchonApp() {
//Code to switch on the electrical appliance

Can I use getkey() inside setup() ?

Have you tried reading the keypad until a certain character, perhaps '#', has been received to indicate that input is complete ?

No. I have not tried that. I will try and see how it goes. Thanks!

Yes, I am able to achieve the input complete via #. Below is the code. I can use the "final" value for setting the time.
But the problem is, I need to make the setup() function to wait for keypad input. If I paste the below code into setup function, it is not waiting for the keypad input. If i paste the code into loop() function, then it waits for user/keypad input. How can I make the arduino to wait for my input during setup() ?

char key = keypad.getKey();
if (key){

if(key == '*') {
input_password = ""; // clear input password
} else if(key == '#') {
input_password = ""; // clear input password
H = final;
} else {
input_password += key; // append new character to input password string
final = input_password.toInt();

Put the code to enter the password in a while loop in its own function and only exit the while loop and hence the function when the key indicating that password entry is complete is entered

Call the function from setup()