Keypad Delay

Goodmorning everyone!

Let's get straight into the point. I have a 4x4 keypad on which I would like to use only the numeric buttons to set a delay for the rest of my program. The code below is the stage that I have accomplished since today, but I have some other problems still that I can't solve. At first, the code should take up to three digits. Secondly, the number should be stored to be used as a delay for a motor that I am using. Every answer is welcome.

Thank you in advance.

String GetNum(){            

  {
   String num;
   char key = keypad.getKey();
   
   lcd.setCursor(0, 0);
  lcd.print ("Set delay time:");

  
  delay (50);
   while(key != '#')
   {
      switch (key)
      {
         case NO_KEY:
            break;

         case '0': case '1': case '2': case '3': case '4':
         case '5': case '6': case '7': case '8': case '9':
           
            
            
            lcd.print(key);
            num = num +key;
            break;

         case '*':
            num = "";
            lcd.clear();
            break;
      }
      key = keypad.getKey();     
   }
   return num; // used to see the storaged number.  
}
}

Please post your complete sketch

I don't have the complete sketch at this computer. Is there any chance to help with the code above?

Is there any chance to help with the code above?

Without the context that the full sketch would provide it is difficult to give much help

First I would avoid the use of Strings and use C style strings instead
To get input of a multi digit number you need to accumulate the entered digits and there are a number of ways to do it. here is one

set an integer variable to zero
read the keypad
if a valid digit has been entered
  turn the character into a number by subtracting '0' (research ASCII if you are unfamiliar with it)
  multiply the total by 10 and add the new number
end if
if the required number of digits has been entered
  the total now contains the entered number as an integer
end if

electp:
At first, the code should take up to three digits.

What do you want it to do? Return the value when the third digit is entered?

            lcd.print(key);
            num = num + key;
            if (num.length >= 3)
                return num;
            break;

electp:
Secondly, the number should be stored to be used as a delay for a motor that I am using. Every answer is welcome.

The String you return from the function contains the number. The code that called the function (part of what you didn't show) should convert the String to a number and store the number to be used later.

This may be of interest Keypad data entry - a beginners guide

[/quote]

UKHeliBob:
This may be of interest Keypad data entry - a beginners guide

This tutorial ROCKS! Thanks a lot!

This tutorial ROCKS!

Blush !

I am glad you found it useful. I have answered questions about Keypads many times here, as have others, so I thought that I would put together something that I could link to rather than repeating myself in multiple threads

Good luck with your project

The codes given are tested? Because I'm using them as they are but they only take the digits. No prints about the "entry is completed" etc. I have used the last code given.

electp:
The codes given are tested? Because I’m using them as they are but they only take the digits. No prints about the “entry is completed” etc. I have used the last code given.

Are you expecting generic example code to do exactly what you want?

TheMemberFormerlyKnownAsAWOL:
exactly

Of course not. I was using the generic example as it is. No problem i came to a solution.

electp:
The codes given are tested? Because I'm using them as they are but they only take the digits. No prints about the "entry is completed" etc. I have used the last code given.

Tested and working for me

Are you pressing the '#' button to finish data entry ?

I assume that you have adjusted the colPins and rowPins arrays and baud rate to suit your system. Does the very first example in the tutorial work for you and print the value of they key pressed ? If not then something is wrong with your wiring, colPins and rowPins array or the baud rate that you are using

Please post the code copied from your IDE that does not show the "entry is complete" message when you press the '#' key

Found it Bob! Thanks!

So now I have another question... How can we store the "total" and use it on another function? I have read that EEPROM can be used for 100.000 times at write and read. Is there another option?

Thank you in advance!

electp:
I have read that EEPROM can be used for 100.000 times at write and read.

You read it wrong - read cycles are unlimited

TheMemberFormerlyKnownAsAWOL:
You read it wrong - read cycles are unlimited

Fine. But the thing is that I want a variable to be delivered at the EEPROM, use it at another function and if the user want to change the value, the value will be changed again at the same memory location so the previous variable will be deleted. (This variable is a Delay for a motor).

How can we store the "total" and use it on another function?

Copy it to another variable then use it when required

In the tutorial I said

The actual reading of a key and dealing with it has been turned into a function. Once it is debugged we can call it as and when required

So, if you want to input 2 multi digit numbers call readKeypad() until entry is complete then copy the total variable to another variable and then call readKeypad() until entry is complete again and copy total to a second variable. Now you have 2 variables to do with what you want. Unless you want to store the number(s) entered for use after power down or a reset there is no need to use EEPROM

Don't hold your breath waiting as I am busy, but I may add an example of multiple data entry to the tutorial at some time

I have updated the Keypad tutorial with an example of multiple number entry used to set on and off times of a blinking LED, but the two (or more) numbers entered could be used for whatever you like

See post #4 of Keypad tutorial

I have added a further example to the Keypad tutorial

UKHeliBob:
I have added a further example to the Keypad tutorial

You are awesome! Thumbs up!

Another example has been added to the tutorial.

The new example has the same functionality as before but better implemented which should help understand it. The examples are getting away from the main purpose of the thread which is to explain the use of the Keypad library so I will not be enhancing it any more but I may add other examples focusing more on the use of the library