3 digit keypad input value to be sent to servo motor

I am working on a lab for one of my classes and the lab required the use of a 4x4 keypad.

There is much more to the lab but this specific section says that when "C" is pressed the user can input a number, after inputting the number (up to three digits) the user then presses the "#" character and the value inputted gets sent to the servo motor along with being displayed on an LCD screen.

here is the code I have for the servo and it seems like it should work when I read it but then nothing happens when it runs:

//================================================================
void Servoscreen(void)
{
 ServoScreenSetup();
 static int pressedKeyCount = 0;
 char firstchar, secondchar, thirdchar;
 int thisscreen = 1;
 static int finishinput = 1;
 
 char key;
 int finConversion = 0;
 
 int ThreeDigNum, TwoDigNum, OneDigNum;
 
 
 
//----------------------------------------------------------------  
 do
 {
   key = myKeypad.getKey();
   if(key == 'C')
   {
     finishinput = 0;
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print("Input Servo Val");
     lcd.setCursor(0,1);
     lcd.print("Then press #");
   }
//----------------------------------------------------------------  

       
while(finishinput = 0)
   {
     pressedKeyCount = 0;
     
     
     key = myKeypad.getKey();
     if(key && pressedKeyCount == 0);
     {
       firstchar = key;
       pressedKeyCount = pressedKeyCount + 1;
     }
     
     key = myKeypad.getKey();
     if(key && pressedKeyCount == 1);
     {
       secondchar = key;
       pressedKeyCount = pressedKeyCount + 1;
     }
     
     key = myKeypad.getKey();
     if(key && pressedKeyCount == 2);
     {
       thirdchar = key;
       pressedKeyCount = pressedKeyCount + 1;
     }
     
     if(key == '#')
     {
       finishinput = 1;
     }
   
   }
   
//----------------------------------------------------------------       
  
   if(pressedKeyCount == 1)
   {
     int Firstdecimal = CharToInt(firstchar);
     finConversion = 1;
     OneDigNum = Firstdecimal;
   }
   
   if(pressedKeyCount == 2)
   {
     int Firstdecimal = CharToInt(firstchar);
     int Seconddecimal = CharToInt(secondchar);
     Firstdecimal = Firstdecimal * 10;
     finConversion = 1;
     TwoDigNum = Firstdecimal + Seconddecimal;
   }
   
   if(pressedKeyCount == 3)
   {
     int Firstdecimal = CharToInt(firstchar);
     int Seconddecimal = CharToInt(secondchar);
     int Thirddecimal = CharToInt(thirdchar);
     Firstdecimal = Firstdecimal * 100;
     Seconddecimal = Seconddecimal * 10;
     finConversion = 1;
     ThreeDigNum = Firstdecimal + Seconddecimal + Thirddecimal;
   }
//----------------------------------------------------------------    
if(finConversion == 1)    
   {
     lcd.clear();
     ServoScreenSetup();
     switch(pressedKeyCount)
     {
       case 1:
        lcd.setCursor(7,1);
        lcd.print(OneDigNum);
       case 2:
        lcd.setCursor(7,1);
        lcd.print(TwoDigNum);
       case 3:
        lcd.setCursor(7,1);
        lcd.print(ThreeDigNum);
     }
   }
   
   
   
   
   
//----------------------------------------------------------------    
   key = myKeypad.getKey();
   if(key == 'B')
   {
     choice = '2';
     thisscreen = 0;
   }
   if(key == 'D')
   {
     choice = '_';
     thisscreen = 0;
   }
//----------------------------------------------------------------
 }while(thisscreen == 1);
 lcd.clear();
}
//=================================================================
void BDtext (void)
{
 lcd.setCursor(11,0);
 lcd.print("prv-B");
 lcd.setCursor(11,1);
 lcd.print("nxt-D");
}
//=================================================================
void ServoScreenSetup(void)
{

 lcd.clear();
 BDtext();
 lcd.setCursor(1,0);
 lcd.print("Servo deg");
//----------------------------------------------------------------
 lcd.setCursor(1,1);
 lcd.print("A");
 lcd.setCursor(6,1);
 lcd.print("C");
}
//==================================================================
int CharToInt(char character)
{
 int decimal;
 switch(character)
 {
   case '0':
    decimal = 0;
    break;
   case '1':
    decimal = 1;
    break;
   case '2':
    decimal = 2;
    break;
   case '3':
    decimal = 3;
    break;
   case '4':
    decimal = 4;
    break;
   case '5':
    decimal = 5;
    break;
   case '6':
    decimal = 6;
    break;
   case '7':
    decimal = 7;
    break;
   case '8':
    decimal = 8;
    break;
   case '9':
    decimal = 9;
    break;
 }
 
 return decimal;
 
}
//==================================================================

This is no way completed code (i have been working on this all day and can getting tired) like how there are no comments which I will add later (not there because with the amount of time I had to change the code retyping the comments became tiresome)

When I run the code I can get to the screen on the LCD where is says "Servo deg" and has "A" and "C" on the next line. When I press C I get the next screen saying to input the Servo Val and Pressing # but after that nothing happens

Also, and I do not know if this is the reason why it will not work, due to COVID-19 my classes are all online and the professor had us use a simulator instead of purchasing an Arduino because of the components we use are in the classroom which is currently inaccessible, the simulator does give components we can use. The simulator we are using is TinkerCad and my total program is currently more than 400 lines (but I believe this should not matter because the Servo Motor is a function that the Arduino runs and ignore the rest of the code)

if needed I can post the entire code and send a picture of my circuit

also sorry for the formatting if this i have never used this forum before

You can make it simple by reading three digits into a string and then convert to integer by using myString.toInt() function.

To read three digit into a string, refer to keypad - password code

IoT_hobbyist:
You can make it simple by reading three digits into a string and then convert to integer by using myString.toInt() function.

To read three digit into a string, refer to keypad - password code

No. Don't do that. You do not want to use Strings on an Uno. Learn how to use regular C strings and not rely on the 'String' class. It will cause you trouble.

blh64:
No. Don't do that. You do not want to use Strings on an Uno. Learn how to use regular C strings and not rely on the 'String' class. It will cause you trouble.

If you use properly, it will not cause any trouble (memory leak). By using a global String and String.reserve() function, you do not need to worry about memory leak.

You can check whether memory is leaked or not by using MemoryFree Library

Take a look at Keypad data entry for simpler techniques to get a multi digit number from a keypad

I was going to say that String is a sledgehammer to crack a nut, but in light of this

int CharToInt(char character)
{
  int decimal;
  switch(character)
  {
    case '0':
       decimal = 0;
       break;
    case '1':
       decimal = 1;
       break;
    case '2':
       decimal = 2;
       break;
    case '3':
       decimal = 3;
       break;
    case '4':
       decimal = 4;
       break;
    case '5':
       decimal = 5;
       break;
    case '6':
       decimal = 6;
       break;
    case '7':
       decimal = 7;
       break;
    case '8':
       decimal = 8;
       break;
    case '9':
       decimal = 9;
       break;
  }
 
  return decimal;
 
}

sure, go ahead.

while(finishinput = 0)

Oops

   if(key && pressedKeyCount == 1);

And again, oops.

was not expecting this many responses in such a short amount of time, thanks I will try everyone's advice

I was able to clean up my code and I want to especially thank UKHeliBob - the forum of how to use a keypad was perfect and I am going to save that forum in case I need it later

However, I ran into another problem where at the beginning of the ServoScreen when you press B, C, or D it is not recognizing it or not do anything like moving on to the next screen or the previous screen or changing to the screen to input the number, nothing I press on the keyboard does anything and when I use a Serial.print nothing gets sent however when I do it elsewhere in the program things get printed

The only reason I can think of is that I am using TinkerCad and not an actual Arduino

Code:

//====================================
void Servoscreen(void)
{
 ServoScreenSetup();
 int thisscreen = 1;
 int number = 0;
 char key;
//----------------------------------------------------------------  
 do
 {
//----------------------------------------------------------------  
   key = myKeypad.waitForKey();
   switch(key)
   {
    case 'C':
      number = servoinput();
      lcd.clear();
      ServoScreenSetup();
      lcd.setCursor(7,1);
      lcd.print(number);
      break;
//----------------------------------------------------------------    
    case 'B':
      choice = '2';
      thisscreen = 0;
      break;
  case 'D':
      choice = '_';
      thisscreen = 0;
      break;
   }
//----------------------------------------------------------------
 }while(thisscreen == 1);
 lcd.clear();
}
//=====================================
void BDtext (void)
{
 lcd.setCursor(11,0);
 lcd.print("prv-B");
 lcd.setCursor(11,1);
 lcd.print("nxt-D");
}
//=====================================
void ServoScreenSetup(void)
{
 lcd.clear();
 BDtext();
 lcd.setCursor(1,0);
 lcd.print("Servo deg");
//----------------------------------------------------------------
 lcd.setCursor(1,1);
 lcd.print("A");
 lcd.setCursor(6,1);
 lcd.print("C");
}
//=====================================
int servoinput (void)
{
 int finishinput = 1;
 int total = 0;
 int digitcounting = 0;
 while(finishinput = 1);
 {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Input Servo Val");
  lcd.setCursor(0,1);
  lcd.print("Then press #");
  
  char Key = myKeypad.getKey();
  if(Key == '#')
   {
     finishinput = 0;
     return total;
   }
   if(Key >= '0' && Key <= '9')
   {
     int value = Key - 48;
     total = total *10;
     total = total + value;
     digitcounting = digitcounting + 1;
   }
   if(digitcounting == 3)
   {
     finishinput = 0;
     return total;
   }
 }
}

while(finishinput = 1); Oops

Please remember to use code tags when posting code

thank you so much TheMemberFormerlyKnownAsAWOL - I do not know if I will ever learn to check for semi-colons and make sure they are not in places where they should not be.

Just tested the code and it works thank you, everyone

I do not know if I will need Arduino for a future class but I do know that the people here are very helpful

again

Thank you

The semicolon is not the only issue in the code I highlighted