Stepper motor problem [SOLVED]

I'm having trouble with a stepper motor.

Okay, so I'm still quite new to using an Arduino, so this is based on a tutorial I found on youtube (which I'll link at the end of the post).

I'm just trying to get the motor to move at the moment, but it keeps getting stuck every few seconds. If I twist it hard enough, it'll change back to turning the way it should, but it'll still get stuck again after a few seconds. The other sticking problem is that it will automatically be stuck when I change the direction of the motor in the programming. I'm 99% sure the it's not a problem with the programming, because I completely copied it from the video after it stopped working.

The thing that I think it could be is a battery problem, because I had to attach another battery onto the Arduino (9V), after the original hadn't worked.

Link to video: Control Stepper motor 28BYJ-48 with ULN2003 for Arduino - YouTube

Here's the code by the way:

int Pin1 = 10; 
int Pin2 = 11; 
int Pin3 = 12; 
int Pin4 = 13; 
int _step = 0; 
boolean dir = true;// false=clockwise, true=counter clockwise
int count=0;
void setup() 
{ 
 pinMode(Pin1, OUTPUT);  
 pinMode(Pin2, OUTPUT);  
 pinMode(Pin3, OUTPUT);  
 pinMode(Pin4, OUTPUT);  
} 
 void loop() 
{ 
 switch(_step){ 
   case 0: 
     digitalWrite(Pin1, LOW);  
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, LOW); 
     digitalWrite(Pin4, HIGH); 
   break;  
   case 1: 
     digitalWrite(Pin1, LOW);  
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, HIGH); 
     digitalWrite(Pin4, HIGH); 
   break;  
   case 2: 
     digitalWrite(Pin1, LOW);  
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, HIGH); 
     digitalWrite(Pin4, LOW); 
   break;  
   case 3: 
     digitalWrite(Pin1, LOW);  
     digitalWrite(Pin2, HIGH); 
     digitalWrite(Pin3, HIGH); 
     digitalWrite(Pin4, LOW); 
   break;  
   case 4: 
     digitalWrite(Pin1, LOW);  
     digitalWrite(Pin2, HIGH); 
     digitalWrite(Pin3, LOW); 
     digitalWrite(Pin4, LOW); 
   break;  
   case 5: 
     digitalWrite(Pin1, HIGH);  
     digitalWrite(Pin2, HIGH); 
     digitalWrite(Pin3, LOW); 
     digitalWrite(Pin4, LOW); 
   break;  
     case 6: 
     digitalWrite(Pin1, HIGH);  
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, LOW); 
     digitalWrite(Pin4, LOW); 
   break;  
   case 7: 
     digitalWrite(Pin1, HIGH);  
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, LOW); 
     digitalWrite(Pin4, HIGH); 
   break;  
   default: 
     digitalWrite(Pin1, LOW);  
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, LOW); 
     digitalWrite(Pin4, LOW); 
   break;  
 } 
 if(dir){ 
   _step++; 
 }else{ 
   _step--; 
 } 
 if(_step>7){ 
   _step=0; 
 } 
 if(_step<0){ 
   _step=7; 
 } 
 delay(1); 

}

That looks like a sensible video, though I have not watched it all.

Looking at your program, I suggest you start with a much bigger delay(). At the moment you are trying to do a step every millisec. Try delay(500) and see if that improves things.

You mention a 9v battery to power the Arduino - if it is one of the PP3 style batteries it is completely unsuitable - they cannot provide enough current. But why not power the Arduino from the USB cable for test purposes?

And you have not told us how you are powering the motor. The YouTube video clearly states that it needs a separate power supply.

...R

Sounds to me like a broken cog in the stepper gearbox.

Great! Thanks for the quick responses!

I tried messing with the delay between steps first, but 500 was a bit too slow for it to be able to do anything. But delay(1) was so fast that it wasn't able to keep up with the functions, and just ended up trying to turn both ways at the same time. So I changed it a bit, and found that the perfect delay amount was..... 2.

So it's twice as slow now (which I'm okay with, as the time on the thing I'm making doesn't really matter), and it works without any problems.

I'm pretty glad it wasn't a cog problem- I have no idea about the inner functions of a motor. Thanks so much for the help!

CormAlan:
I tried messing with the delay between steps first, but 500 was a bit too slow for it to be able to do anything.

I was aware of that. But once you have it moving reliably at a very slow speed you have a base from which to experiment with higher speeds.

Good to hear you have a solution.

...R