How to change this function in to a non-blocking function? How do I modify the getTheNumber() function to satisfy the statement below??
"
This function blocks until the user is done typing the number. Try to rewrite it in a non-blocking fashion. For example, it could return -1 on every call as long as the user is not done typing the number. On the first call after the user is done it would return the valid value. If you manage to write everything non-blocking, then you loop() will restart very often, and the program will work with no significant latency."

Here is the code:

int getTheNumber()
{
    char buffer[4];
    // Input up to 3 numbers until we find a * or #
    int i=0;
    while (1)
    {
        char key = keypad.getKey();

        // If it's a number AND we have space left, add to our string
        if ('0' &lt;= key &amp;&amp; key &lt;= '9' &amp;&amp; i &lt; 3)
        {
            buffer[i] = key;
            i++;        
        }
        // If it's a * or #, end
        else if ('#' == key &amp;&amp; i &gt; 0)
        {
            // Null terminate
            buffer[i] =0; 
            int value = atoi(buffer);
            break;
        }        
    }
    return atoi(buffer);
    }
make buffer[] and i static.
get rid of the while() loop.


Like this:

int getTheNumber()
{
    static char buffer[4];
    static int i=0;
    // Input up to 3 numbers until we find a * or #
    char key = keypad.getKey();

    // If it's a number AND we have space left, add to our string
    if ('0' &lt;= key &amp;&amp; key &lt;= '9' &amp;&amp; i &lt; 3)
        {
            buffer[i] = key;
            i++; 
            return(-1)       
        }
        // If it's a * or #, end
    else if ('#' == key &amp;&amp; i &gt; 0)
        {
            // Null terminate
            buffer[i] =0; 
            int value = atoi(buffer);
            i=0;
            return atoi(buffer);
        }        
}