Coding max waiting time for a user input response


Another trivial question for me. I want to code for a max waiting time for a user response before executing a process. I don't want to wait in a loop because this may cause other problem (i suppose)

My English is not perfect so I will describe my idea

The goal is to have a Arduino software reset availble by using a keypad.

Once the user has press the button # 7 . Message display:" Software Reset. Yes 6 No 7" wait max 3 seconds then get out if no response

If button 6 press within the allowed time then SoftwareReset if Button 7 then exit

How it it be implemented without a risk of an extended period loop


Use Serial.available() and millis() in a loop to determine when the timeout has expired.

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.


Keep track of the value of millis() from the last time the user pressed a button. Each new button press will update that value.

When you're in that "waiting to confirm" state, you can exit the state via a button press or by the "last pressed" getting too old.

Here's an example of how you might do it with the serial monitor. You might need to change your source of keys to suit but the premise seems pretty sound:

#define RST_ARMED_TIMEOUT   3000ul      //mS    reset-armed timeout

enum States

//reset function
void(* resetFunc) (void) = 0;

void setup( void )
    Serial.begin( 115200 );
    Serial.println( "**\tReset..." );     //indicate we reset on serial monitor
    bCh = false;

void loop( void )
    //get a new char if available     
    if( Serial.available() > 0 )
        chr =;
        bCh = true;

    //check reset timing/sequencing


void HandleKeyReset( void )
    static unsigned long
    static byte
        stateReset = ST_REQUEST;

    //new character?
    if( bCh )
        //yes; clear flag and see where we are
        bCh = false;
        switch( stateReset )
            case    ST_REQUEST:
                //waiting for the request here. That's a press of '7'
                if( chr == '7' )
                    //inform user
                    Serial.println( "Software Reset. Yes 6   No 7" );
                    //get time now so we can do timeout
                    timeReset = millis();
                    //we're armed now...
                    stateReset = ST_RST_ARMED;

            case    ST_RST_ARMED:
                //if we see a new char and it's '6', reset
                if( chr == '6' )
                else if( chr == '7' )
                    //if 7, disarm reset
                    Serial.println( "Software Reset canceled" );
                    stateReset = ST_REQUEST;

    //if we're armed see if we've timed out waiting for the '6' to confirm
    if( stateReset == ST_RST_ARMED )
        if( (millis() - timeReset) >= RST_ARMED_TIMEOUT )
            stateReset = ST_REQUEST;
            Serial.println( "**\tTime expired." );

ToddL1962: Use Serial.available() ...

arjfca: ...using a keypad.

I saw that 12Stepper but there were so many other replies I chose not to address it.