Stepper Motor Code Help, Vibrating and not moving.

Hello everyone, I am fairly new to arduino. I am trying to make my stepper motor move a 1/2 turn when I press on button and a full turn when i press another. If I write just the code for one button it works, but if I add the coding for the other button the stepper vibrates constantly and wont move. What am I doing wrong?

I plan on eventually incorporating 4 buttons to do 1/4, 1/2, 3/4 and a full turn.

//Stepper Driver for table saw fence V.1



#include <Stepper.h> // Include stepper library

#define STEPS 2038 // Number of steps per revolution (28BYJ-48)stepper

Stepper stepper(STEPS, 8, 9, 10, 11); // name of stepper, number of steps and pins 

int pushButton = 3; // Push button on pin 3
int pushButton2 = 4; // Push button on pin 4

void setup()
{
  pinMode(pushButton, INPUT); // push button is an input
  pinMode(pushButton2, INPUT);
 
  stepper.setSpeed(10); // sets speed of stepper to 10
}

void loop()
{
  int buttonState = digitalRead(pushButton); // assign variable to button
  
  if (buttonState == 1)
    {
      stepper.step(1019); // if button is pressed turn 1/2 rotation and stop
    }

  int buttonState2 = digitalRead(pushButton2);

  if (buttonState2 == 1)
  {
    stepper.step(2038);
  }
    delay(1000); //delay to allow time for spin and release of buttons
}

Thanks!

How are the buttons wired? Normally it's easiest to use INPUT_PULLUP mode, and wire the button to ground. Then LOW indicates that it's pressed.

  int buttonState = digitalRead(pushButton); // assign variable to button

if (buttonState == 1)

This detects if the button IS pressed, not when it first BECOMES pressed. You should also research debouncing.

You never told it what to do when the button is low. Also do you use pull up or down resistors for you buttons?

A 'vibrating and not moving' stepper motor almost always means that you have the wires to the coils hooked up in the wrong order.

I have pull Down resistors connected to the buttons. I will Try The INPUT_PULLUP and see if that makes a difference.

Do i need to tell it what to do when no button is pressed? does it just default to doing nothing?

The stepper should be wired correctly. When i uploaded a test program made for the stepper and stepper controller it worked just like it was supposed to.

What happens if you change the code like this

void loop()
{
  int buttonState = digitalRead(pushButton); // assign variable to button
  int buttonState2 = digitalRead(pushButton2);
 
  if (buttonState == 1)
  {
     stepper.step(1019); // if button is pressed turn 1/2 rotation and stop
  }

  else if (buttonState2 == 1)
  {
    stepper.step(2038);
  }
  delay(1000); //delay to allow time for spin and release of buttons
}

...R

Got it to work! Ended up using a variety of what you guys said to make this code,

//Stepper Driver for table saw fence V.1


#include <Stepper.h> // Include stepper library

#define STEPS 2038 // Number of steps per revolution (28BYJ-48)stepper

Stepper stepper(STEPS, 8, 9, 10, 11); // name of stepper, number of steps and pins 

int pushButton = 3; // Push button on pin 3
int pushButton2 = 4; // Push button on pin 4

void setup()
{
  pinMode(pushButton, INPUT_PULLUP); // push button is an input pull up (High when not presses)
  pinMode(pushButton2, INPUT_PULLUP); // push button is an input pull up
 
  stepper.setSpeed(10); // sets speed of stepper to 10
}

void loop()
{
  int buttonState = digitalRead(pushButton); // assign variable to button
  int buttonState2 = digitalRead(pushButton2);
  
  if (buttonState == LOW)
    {
      stepper.step(1019); // if button is pressed turn 1/2 rotation and stop
    }
    else if (buttonState2 == LOW)
  {
    stepper.step(2038);
  }
  
  
    delay(1000); //delay to allow time for spin and release of buttons
}

Thanks for all the help!