Start and stop of bipolar stepper motor

Hi i am controlling a stepper motor with a driver... how can i stop a stepper motor from spinning when it reach my desired position.. e.g when i say go to 180 degrees it will turn to 180 degrees and stop der.. stepper is controlled with clock and direction pin. my stepper is a 1.8 degree bipolar stepper motor so 200 steps for 1 rev.

sample code

int dirPin = 3;
int clkPin = 2;


void setup(){
  
pinMode(dirPin,OUTPUT);
pinMode(clkPin,OUTPUT);

digitalWrite(dirPin,HIGH);
digitalWrite(clkPin,LOW);

}

void loop(){


digitalWrite(dirPin,HIGH);   //CLOCKWISE
for(int x=0;x<100;x++){ // 100 * 1.8 = 180
digitalWrite(clkPin,HIGH);
delayMicroseconds(1000);
digitalWrite(clkPin,LOW);
delayMicroseconds(1000);
}
}

say after executing the loop the stepper must stop to that position. how will i do it?.. because it just keeps on spinning.

I just need to know how to stop it when it reach my desired position because i'll be adding buttons to control it but first i need how stop it.

Thanks a lot.

Your stepper keeps spinning because you are continuing to send it step pulses.

Loop continues to run because its a loop. Outside of the program is a main() that calls loop repeatedly.

What signal are you using to start the step sequence?

Suggestion - the motor control sequence should be in a function of its own. To move the motor you call that function and the motor would move.

You might have 2 buttons for forward and reverse - you set direction to 0 or 1 and then call the move function.

You might consider making your function take an argument for the number of steps. makes it a bit more universal.

For now you could call your motor function from setup and it will run just once.

wow thanks didn't know that :).. it's working now.. now i want to move the stepper in 5 different position using 5 buttons.. 1 - 90 degrees, 2 - 180 degrees, 3 - 270 degrees, 4 - 360 degrees, 5 - random angle from the ff. angle.. 90,180,270,360..

can i use this setup so i can save arduino pins?.. http://tronixstuff.wordpress.com/2011/01/11/tutorial-using-analog-input-for-multiple-buttons/

And how will i know the absolute position of my motor so every press of a button it will return to it's initial position and go to the position of the pressed button?.

Let's say i want the initial position to be on top always.. so it's 0 degrees.

sample code.

if(button1=HIGH){
go to 0 degrees then turn 90 degrees
}
if(button2=HIGH){
go to 0 degrees then turn 180 degrees
}

thanks for your help.

Unless you have some sort of homing switch you have no way of knowing where the motor is other than looking at it. As far as keeping track of position, you will have to have a counter that you can use to monitor the current position. For a 200 step motor - 50 full steps = 90 degrees.

Sounds like button 1 - go to 0 degrees, then move to 90 degrees?
button 2 go to 0 degrees then move to 180 degrees

and such?

You can use the step routines, or create your own. Not very hard to do.