Another Numeric Input Question, Sorry

Hi there all,

Please, Please help. I am trying to create a menu where I can enter specific values. I cannot seem to get it right, all I want to do is enter a timer value; maximum two digits in length and between 5 and 99.

To make it a little easier for me to mess with the code I have created a subroutine to achieve this. The menu part of the code looks like this:-

  case 'A':           
          lcd.clear();
          lcd.setCursor (1,0);
          lcd.print ("Enter Start Delay");
          lcd.setCursor(1,1);
          lcd.print ("(5");
          lcd.setCursor (3,1);
          lcd.write (1);
          lcd.setCursor (5,1);
          lcd.print ("99)");
          lcd.setCursor (9,1);
          lcd.print ("MINUTES");
          lcd.setCursor (1,2);
          startTimeInput();
          lcd.print(startDelay);

          
          keypad.waitForKey();
          lcd.clear();
          
          break;

(the wait for keypad at the end is just to stop the programme while I am trying to debug, it will be removed in a little bit)

the subroutine that is supposed to capture the number and create the integer from the string is :-

void startTimeInput(){
   char stKey = keypad.getKey();
   if (stKey) {
    Serial.print ("Start delay = ");
    Serial.print (stKey);
    Serial.print (" Full Delay = ");
    Serial.println (startDelayMins);

    if (stKey >= '0' && stKey <= '9') {
      startDelay += stKey;
    }  else if (stKey == '*'){
      if (startDelay.length() > 0){
        startDelayMins = startDelay.toInt();
        if (startDelayMins <1){ break; }
              
      }
    } else if (stKey == '#'){
      startDelay = "";
    }
   }

when I run this, I get no data saved, no output to serial and only one keypress.....

(the serial print instructions are again to try and help debug this mess....)

please, any assistance will be greatly appreciated...

Doesn't the keypad library return no_key or similar if nothing is pressed? If so, that would probably explain your symptoms.

OK, some progress....

I have got the system accepting the input......
it even serial prints the digits I press......

I used a while loop, and set a flag once the number was inputted. I am using the command .toInt to convert the string to a numeric value.....

   do {
            char stKey = keypad.getKey();
   if (stKey) {
    Serial.print ("Start delay digit input = ");
    Serial.print (stKey);
    Serial.print (" Full Delay = ");
    Serial.println (startDelayMins);

    if (stKey >= '0' && stKey <= '9') {
      startDelay += stKey;
    }  else if (stKey == '*'){
      if (startDelay.length() == 2){
        startDelayMins = startDelay.toInt();
        startDelayFlag = 1;
        
              
      }
    } else if (stKey == '#'){
      startDelay = "";
    }
   }
  } while (startDelayFlag !=1);
         

the variable 'full delay' should be this numeric value....

Start delay digit input = 2 Full Delay = 0
Start delay digit input = 3 Full Delay = 0
Start delay digit input = * Full Delay = 0

in this case 23...... Help?

This will increment startDelay by a value between 48 and 57. Is that what you want?

In what case?

Maybe some of the ideas I used here for a serial calculator would help out...

https://forum.arduino.cc/t/dont-cross-the-streams-fp-scientific-calculator-serial-co-processor/144054/12?u=mrburnette
Basically allows one to mix commands and numeric strings.

not sure if I am allowed to say this, but I was a tit!!!

I had solved the issue and didn't realise.....

the full delay value is only saved on exit of the subroutine....

sorry to be a numpty.

the code works, if anyone would like to use it!

I will post it here to be copied....

 if (startDelayFlag != 1 ){
 lcd.setCursor (0,2);
 lcd.print("F1 Set Start Delay");
 } else {
  lcd.setCursor (0,2);
  lcd.print("Delay Set");
  lcd.setCursor (10,2);
  lcd.print (startDelayMins);
  lcd.setCursor (13,2);
  lcd.print ("Mins");
 }

 if (runTimeFlag != 1){
  lcd.setCursor (0,3);
  lcd.print ("F2 Set Run Time");
 } else {
  lcd.setCursor (0,3);
  lcd.print ("Run Time Set");
 }

 
  char keypressed = keypad.getKey();
  if (keypressed != NO_KEY){
  
    switch (keypressed){
    
      
        break; 
      case 'A': 
          startDelayFlag = 0;      
          lcd.clear();
          lcd.setCursor (1,0);
          lcd.print ("Enter Start Delay");
          lcd.setCursor(1,1);
          lcd.print ("(5");
          lcd.setCursor (3,1);
          lcd.write (1);
          lcd.setCursor (5,1);
          lcd.print ("99)");
          lcd.setCursor (9,1);
          lcd.print ("MINUTES");
          lcd.setCursor (1,2);
          do {
            char stKey = keypad.getKey();
   if (stKey) {
    Serial.print ("Start delay digit input = ");
    Serial.print (stKey);
    lcd.setCursor ((csr)+1, 2);
    lcd.print (stKey);
    
   
    if (stKey >= '0' && stKey <= '9') {
      startDelay += stKey;
      csr++;
    }  else if (stKey == '*'){
      if (startDelay.length() == 2){
        startDelayMins = startDelay.toInt();
        startDelayFlag = 1;
        startDelay = "";
        
              
      }
    } else if (stKey == '#'){
      startDelay = "";
      startDelayMins = 0;
      startDelayFlag = 0;
    }
   }
  } while (startDelayFlag !=1);
  lcd.clear();
         
          
          
          break;

just add as many cases as needed :grin: :partying_face: