Controlling Stepper Motor CW and CCW thru inputting the degrees using 4x4 Keypad

good day to all! I am new in electronics and programming
and i am having a problem with controlling this stepper motor for my final project
I am using a stepper motor and driver ic same with the components found here

i tried to mix up things that i now about programming stepper using stepper but got a trouble with connecting keypad as my input.. i hink i have a problem with my code
here's what i did

#include <Keypad.h>
#include <Stepper.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
 
int in1Pin = 9;
int in2Pin = 10;
int in3Pin = 11;
int in4Pin = 12;

int degreesVal;
int digit1 = -1;
int digit2 = -1;

boolean confirm = false;

Stepper motor(768, in1Pin, in2Pin, in3Pin, in4Pin);  
 
void setup()
{
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(in3Pin, OUTPUT);
  pinMode(in4Pin, OUTPUT);
 
  //while (!Serial);
  Serial.begin(9600);
  motor.setSpeed(20);
}
 
void loop()
{
  char key = keypad.getKey();
  
  if (key == '1')
    key = 1;
  else if (key == '2')
    key = 2;
  else if (key == '3')
    key = 3;
  else if (key == '4')
    key = 4;
  else if (key == '5')
    key = 5;
  else if (key == '6')
    key = 6;
  else if (key == '7')
    key = 7;
  else if (key == '8')
    key = 8;
  else if (key == '9')
    key = 9;
  else if (key == '0')
    key = 0;
  Serial.println(key);
  
  if (digit1 > -1) {
    if (digit2 > -1) {
      if (key == '#') {
        confirm = true;
      }
      else if (key == '*') {
        digit1 = -1;
        digit2 = -1;
      }  
    }
    else if (key == '*') {
      digit1 = -1;
    }
    else {
      digit2 = key;
    }
  }
  else if (key == '0') {
    
  }
  else {
    digit1 = key;
  }
  
  if (confirm == true) {
    degreesVal = digit1 * 10 + digit2;
    if (degreesVal > 90) {
      degreesVal = 90;
    } 
    motor.step(degreesVal);
  }
  
}

the motor response when i press the # but not the exact degree i inputted. my deadline is near that's why i posted my problem here today. hoping that someone could help me figure out what problem i have with my code. thanks in advance!

Go back to your post, select "modify" on it, highlight the section of code and click the "Insert Code" button above with the "#" symbol.

thank you sir for your guidance
sorry i am a newbie in this forum

I get what you're trying to do, but it's obviously incomplete and the logic of handling the reset button doesn't look quite right. Also, you don't set confirm back to false after you have sent the command to the stepper motor.

To start with I suggest you ignore the stepper motor and concentrate on getting the keypad input logic right, and print out the value of digit1 and digit2 after each button press so that you can see what effect it has had. Keeping the same architecture you have currently, I think the logic ought to be something like this:

  if(digit1 < 0)
  {
  	// we're setting digit1
  	switch(key)
  	{
  	case '0' ... '9':
  		digit1 = key - '0';
  		break;
  	case '#':
  		// no action - confirm not applicable when entering first digit
  		break;
  	case '*':
  		// no action - cancel not applicable when entering first digit
  		break;
  	}
  }
  else
  {
  	// we're setting digit2
  	switch(key)
  	{
  	case '0' ... '9':
  		digit2 = key - '0';
  		break;
  	case '#':
  		confirm = true;
  		break;
  	case '*':
  		// cancel previous digit
  		digit1 = -1;
  		break;
  	}
  }

What is the stepper motor intended to do, by the way? Are you trying to move it by a specified distance, or move it to a specified position?

@PeterH

Why not just use one case switch like so:

switch( key )
{
   case '0' ... '9' :
      digit1 = digit1 * 10 + (key - '0');
      break;

   case '#' : 
      // set stepper position
      // clear digit1
      break;

   case '*' :
      //clear digit1
      break;
}

thanks for your replies.
SIR PETERH
i need to make the stepper move in a specified position.
for example i pressed the numbers '2' and '8'( 28 degrees) and press the letter 'A' means enter/clockwise
the stepper will rotate at 28 degrees position..

i'm new in programming that's why i actually do trials and errors.. and look for some examples over the internet..

bravokilo:
i need to make the stepper move in a specified position.
for example i pressed the numbers '2' and '8'( 28 degrees) and press the letter 'A' means enter/clockwise
the stepper will rotate at 28 degrees position..

I can guess that English is not your first language and it may not be easy to explain clearly what you want. If you mean that you enter '28#' and the stepper moves clockwise to the '28 degrees' position, and then you enter '20#' and the stepper moves counter-clockwise to the '20 degrees' position then your motor code is not correct; you would need to keep a record of the motor's current position, compare that with the desired new position read from the keypad, and that would tell you how far to move and in which direction.