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' <= key && key <= '9' && i < 3)
{
buffer[i] = key;
i++;
}
// If it's a * or #, end
else if ('#' == key && i > 0)
{
// Null terminate
buffer[i] =0;
int value = atoi(buffer);
break;
}
}
return atoi(buffer);
}
2 Answers 2
I have a post about doing serial input without blocking.
Example code:
/*
Example of processing incoming serial data without blocking.
Author: Nick Gammon
Date: 13 November 2011.
Modified: 31 August 2013.
Released for public use.
*/
// how much serial data we expect before a newline
const unsigned int MAX_INPUT = 50;
void setup ()
{
Serial.begin (115200);
} // end of setup
// here to process incoming serial data after a terminator received
void process_data (const char * data)
{
// for now just display it
// (but you could compare it to some value, convert to an integer, etc.)
Serial.println (data);
} // end of process_data
void processIncomingByte (const byte inByte)
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;
switch (inByte)
{
case '\n': // end of text
input_line [input_pos] = 0; // terminating null byte
// terminator reached! process input_line here ...
process_data (input_line);
// reset buffer for next time
input_pos = 0;
break;
case '\r': // discard carriage return
break;
default:
// keep adding if not full ... allow for terminating null byte
if (input_pos < (MAX_INPUT - 1))
input_line [input_pos++] = inByte;
break;
} // end of switch
} // end of processIncomingByte
void loop()
{
// if serial data available, process it
while (Serial.available () > 0)
processIncomingByte (Serial.read ());
// do other stuff here like testing digital input (button presses) ...
} // end of loop
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' <= key && key <= '9' && i < 3)
{
buffer[i] = key;
i++;
return(-1)
}
// If it's a * or #, end
else if ('#' == key && i > 0)
{
// Null terminate
buffer[i] =0;
int value = atoi(buffer);
i=0;
return atoi(buffer);
}
}
-
make buffer() and i static ? I don't understand that Sir. How do i do that?rjadkins– rjadkins2017年04月10日 00:55:26 +00:00Commented Apr 10, 2017 at 0:55
-
-
1He typed the word
static
as you can see in the posted code. Did you not see that?2017年04月10日 07:01:16 +00:00Commented Apr 10, 2017 at 7:01 -
@rjadkins static variables keep their values from one function call to another. They're basically like global variables, but they can't be accessed outside the function they're declared in (if that makes any sense).Andrei Dragan– Andrei Dragan2017年04月10日 08:35:44 +00:00Commented Apr 10, 2017 at 8:35
-
Thanks for the reply, really appreciate it. Now, why is the value I enter does does not follow this sequence when the function ic being called: int v = getTheNumber(); lcd.setCursor(2,0); int y = getTheNumber(); lcd.setCursor(2,1); int x = getTheNumber(); lcd.setCursor(2,2); Why does the output in the lcd screen does not follow the setCursor stated in the code?rjadkins– rjadkins2017年04月13日 12:21:58 +00:00Commented Apr 13, 2017 at 12:21