1

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);
 }
asked Apr 10, 2017 at 0:24

2 Answers 2

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
answered Apr 10, 2017 at 6:58
1
  1. make buffer[] and i static.

  2. 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);
 } 
}
Dave X
2,35015 silver badges29 bronze badges
answered Apr 10, 2017 at 0:51
5
  • make buffer() and i static ? I don't understand that Sir. How do i do that? Commented Apr 10, 2017 at 0:55
  • please show me sir.. Commented Apr 10, 2017 at 1:15
  • 1
    He typed the word static as you can see in the posted code. Did you not see that? Commented 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). Commented 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? Commented Apr 13, 2017 at 12:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.