can I get some help with a stepper motor program

Hi, I don’t have much experience using c to program and I’m having a hard time with what should be an easy program.
I’m basically trying to control a stepper motor based on two separate inputs. One input is for counterclockwise, the other for clockwise.
So I got the motor to go different directions but now it doesn’t step as many steps as I’m telling it to. And it seems like it’s erratic moving different degrees every time. And it doesn’t match up between counter and clockwise. Anyone have any ideas?

#include <Stepper.h>

// change this to the number of steps on your motor
#define STEPS 48

// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 9, 11, 10, 12);
int button = 7;
int sensor = 8;

void setup()
{
  // set the speed of the motor to 30 RPMs
  stepper.setSpeed(50);
  pinMode(button, INPUT);   // sets the digital pin 7 as input
  pinMode(sensor, INPUT);  // sets the digital pin 8 as input
}

void loop()
{
 
  if (digitalRead(button) == HIGH){
    stepper.step(24);               // move a number of steps clockwise
  }
  
  if (digitalRead(sensor) == HIGH) {
    stepper.step(-24);             // move a number of steps counterclockwise
  }
}

You probably need to do some debouncing on your button reads.

The stepper code is effectivly taking so much time (600ms), the button has debounced by the time we go around the loop again. Good enough in this case, but buttons should be debounced either in hardware or in software, for the general case.

Speed(50) might be too much for the stepper. Try a lower value.

yea, I tried using different speeds but that didn't work. I don't think it has anything to do with bounce but I can throw in a one shot and see if that is any help.

Trying to verify the program: You ask for 50 rpm (although the comment says 30 ... tch tch) which is 1.2 seconds for one revoultion which is defined as 48 steps, and you do 24 steps, which should be half, which is the 0.6 seconds for each time round the loop(). Put in a, say, 3 second delay() at the beginnig of loop(). Does it seem like a "half second" motion? If not, then there is something lurking somewhere making us go at the wrong speed.

Secondly there are plenty of wiring mistakes possible. Getting a pair of wires crossed makes a stepper jump a little around. Having one wire loose makes it jerk and stop (depends on the speed)

And last but not at least - and comming to think of it, a probable explanation - are you delivering enough power? In the above experiment (3 second delay) is it moving a half revolution? If you try and hold the axis with you fingers is it easy to stop it from turning?

I tried using different speeds but that didn't work.

how slow did you go?
Irratic movement is almost always caused by moving the motor too fast.

Apart from being too fast the other thingbthat could be wrong is that you power supply could not be up to providing the power the motor needs.

Especially if the motor is driving a load you do best to accelerate it smoothly so that mechanically it can keep up and doesn't mis-step. - check out the AccelStepper library?

I figured out where I was going wrong. I wasn't pulling the pins low when not in use. But thanks for the help.