Stepper motor won't spin like I want it to

the problem I’m having is that the stepper motor is just spinning in the forward Clockwise direction forever. What’s wrong?

Here’s my code:

#include <Stepper.h>                          

int buttonPin = 2;              // pushbutton switch is connected to pin 2
int ButtonMode = 0;
int pushbutton;                        // variable for reading the pin status
int doublecheck;                       // variable for reading the delayed status
int buttonState;                // variable to hold the button state; initial


const int stepsPerRevolution = 48;
Stepper stepper(stepsPerRevolution, 8, 9, 10, 11);


void setup() {
  pinMode(buttonPin, INPUT);    // Declare pushbutton as input


pinMode(8, OUTPUT); 
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
 
stepper.setSpeed(120);
  
  Serial.begin(9600);           
  buttonState = digitalRead(buttonPin);   // read the initial state
}

void loop(){
  
  pushbutton = digitalRead(buttonPin);      // read input value and store it in pushbutton
  delay(10);                                // 10 milliseconds
  doublecheck = digitalRead(buttonPin);     // read the input again to check for bounces
  if (pushbutton == doublecheck) {          // make sure we got 2 consistant readings
    if (pushbutton != buttonState) {        // the button state has changed
      if (pushbutton == LOW) {             // check if the button is pressed
        if (ButtonMode == 0) {               // if its off
          ButtonMode = 1;           // First time push 
    } else {
          if (ButtonMode == 1) {             // After first push
            ButtonMode = 0; // Go back to intial button state
           
            
          } 
        }
      }
    }
    buttonState = pushbutton;                 // save the new state in our variable
  }




  // Different motor modes for each push of the button
  if (ButtonMode == 0) {                // initial mode
  
  digitalWrite(8, LOW); digitalWrite(9, LOW); digitalWrite(10, LOW); digitalWrite(11, LOW);
  
  }





  if (ButtonMode == 1) {                // 
  
 
for (int s = 0; s <= 20; s++){
  for (int i = 0; i <= 48; i++){
  digitalWrite(8, HIGH); digitalWrite(9, LOW); digitalWrite(10, HIGH); digitalWrite(11, LOW);
  delay(10);
  digitalWrite(8, HIGH); digitalWrite(9, LOW); digitalWrite(10, LOW); digitalWrite(11, HIGH);
  delay(10);
  digitalWrite(8, LOW); digitalWrite(9, HIGH); digitalWrite(10, LOW); digitalWrite(11, HIGH);
  delay(10);
  digitalWrite(8, LOW); digitalWrite(9, HIGH); digitalWrite(10, HIGH); digitalWrite(11, LOW);
  delay(10);

}
}

delay(2000);
for (int r = 0; r <= 15; r++){
for (int i = 0; i <= 48; i++){
digitalWrite(8, LOW); digitalWrite(9, HIGH); digitalWrite(10, HIGH); digitalWrite(11, LOW);
delay(10);
digitalWrite(8, LOW); digitalWrite(9, HIGH); digitalWrite(10, LOW); digitalWrite(11, HIGH);
delay(10);
digitalWrite(8, HIGH); digitalWrite(9, LOW); digitalWrite(10, LOW); digitalWrite(11, HIGH);
delay(10);
digitalWrite(8, HIGH); digitalWrite(9, LOW); digitalWrite(10, HIGH); digitalWrite(11, LOW);
delay(10);
  }
}
}
}

Nope. That code is WAY to hard to read. Put the { on a new line. Run Tools + Auto Format to properly indent the code. Delete extra blank lines.

Put each statement on it's own line.

There is a Button class that would handle all the stuff you are doing to debounce the switch, making the code much shorter/easier to understand/follow.

Comments should be meaningful.

int buttonState;                // variable to hold the button state; initial

That's pretty obvious, so the comment simply distracts from reading the code.

You are not using the internal pullup resistor, so you must have an external pullup or pulldown resistor wires with the switch. You haven't told us how the switch is wired.

Stepper stepper(stepsPerRevolution, 8, 9, 10, 11);

So, why aren't you using the stepper.step() method to make the motor move?

What kind/size is it? What motor shield are you using?

What order are the pins connected to the motor windings? A+/A-/B+/B- ??

PaulS: Nope. That code is WAY to hard to read. Put the { on a new line. Run Tools + Auto Format to properly indent the code. Delete extra blank lines.

You are not using the internal pullup resistor, so you must have an external pullup or pulldown resistor wires with the switch. You haven't told us how the switch is wired.

Stepper stepper(stepsPerRevolution, 8, 9, 10, 11);

So, why aren't you using the stepper.step() method to make the motor move?

What kind/size is it? What motor shield are you using?

Sorry. I am very weak in programming thus I use a lot of spacing to make things understandable for me and also I use a lot of comments to let myself know what each line is doing.

I'm not using the stepper.step() cause it's just how the instructor wants us to do this assignment. He wants us to first use the digitalWrite() to control each of the four pins and see that it can work like that too before going on to using the stepper.step().

The stepper motor is a PM4222-09 7.5 degree 100 ohm. I know my wiring of it is correct cause the stepper motor works so I'm guessing it's my coding. I set up my circuit by following this: http://img.azega.com/wp-content/uploads/2009/04/Arduino_Stepper_bb.png

I am doing a active-low button setup and I know that's working too cause when I press the button, the stepper motor starts moving.

MarkT:
What order are the pins connected to the motor windings? A+/A-/B+/B- ??

I have the RED wire from the stepper connected to my 9V power supply.

Yellow to pin 8.

Orange to pin 9.

Brown to pin 10.

Black to pin 11.

All of these wires are going through a TIP120 then into the Arduino.

I know my wiring of it is correct cause the stepper motor works

All that this means is that the wiring for the stepper motor is correct. It says nothing about the wiring for the switch.

How is the switch wired (2nd request)?

For a start, I suggest removing the lines:

Stepper stepper(stepsPerRevolution, 8, 9, 10, 11);

and

stepper.setSpeed(120);

Either you're using the Stepper library to drive the motor, or your'e driving it yourself. Don't try to do both at once.

[EDIT: and have you actually waited for the 39 or so seconds it takes to go through the code you've written to do 20 revolutions?]

PaulS:

I know my wiring of it is correct cause the stepper motor works

All that this means is that the wiring for the stepper motor is correct. It says nothing about the wiring for the switch.

How is the switch wired (2nd request)?

It is wired like this. All of this is in one row on my breadboard.

dc42: For a start, I suggest removing the lines:

Stepper stepper(stepsPerRevolution, 8, 9, 10, 11);

and

stepper.setSpeed(120);

Either you're using the Stepper library to drive the motor, or your'e driving it yourself. Don't try to do both at once.

[EDIT: and have you actually waited for the 39 or so seconds it takes to go through the code you've written to do 20 revolutions?]

Alright, so I took out those guys and edited my if loops on the bottom of the code. It goes 3 revolutions forward, stops for 2 seconds, then goes backwards 64~ revolutions, stops then goes back to the 3 revolutions and so on.

I am doing a active-low button setup and I know that’s working too cause when I press the button, the stepper motor starts moving.

I missed this earlier. The motor starts when you press the switch. It takes about 1/2 a second per revolution. The 20 revolutions will take about 10 seconds. Then, the motor should pause for 2 seconds, and rotate back 15 revolutions, taking another 7.5 seconds.

Does that agree with what you are seeing, or not?

PaulS:

I am doing a active-low button setup and I know that's working too cause when I press the button, the stepper motor starts moving.

I missed this earlier. The motor starts when you press the switch. It takes about 1/2 a second per revolution. The 20 revolutions will take about 10 seconds. Then, the motor should pause for 2 seconds, and rotate back 15 revolutions, taking another 7.5 seconds.

Does that agree with what you are seeing, or not?

No the stepper motor isn't doing that. Based on the most recent code I pasted above your post, It's doing 3-4 revolutions, stopping for 2 seconds, then doing about 64 revolutions in the opposite direction then it starts doing the whole cycle over again.

I asked you earlier to put each { on it's own line, and use the Tools + Auto Format tool. If you will do that, I think you will see what the problem is.

If not, create a function to rotate step the motor 4 times. That is what the 4 blocks of digitalWrite and delay statements are doing. Call that function in the inner for loop. That will shorten your code considerably, moving the { and } closer together, to make it easier for you to see the problem.

(Yes, I see it.)

PaulS: I asked you earlier to put each { on it's own line, and use the Tools + Auto Format tool. If you will do that, I think you will see what the problem is.

If not, create a function to rotate step the motor 4 times. That is what the 4 blocks of digitalWrite and delay statements are doing. Call that function in the inner for loop. That will shorten your code considerably, moving the { and } closer together, to make it easier for you to see the problem.

(Yes, I see it.)

I found out what was wrong. It was my for loops. Now it's working fine. My logic was wrong. I didn't know that it would multiply if I put another for loop inside another for loop.