I wrote a simple stepper motor program using micros() , it runs clockwise or anticlockwise as expected , but when i try to run the motor for just one complete revolution , it doesn't rotate and also when i try to run it 200 steps for clockwise and 200 steps for anticlockwise , it just rotates clockwise continuously
The below program is for running it for one complete revolution, which is not happening . Circuit is not the issue , since i tried using other stepper program using delaymicroseconds() and it works as expected ( one revolution) .
I am using drv8225 , 1.8 step stepper motor
byte dirPin = 4;
byte stepPin = 3 ;
unsigned long curMills;
unsigned long prevStepMills = 0;
unsigned long millisBetweenSteps = 2000;
int k;
int sleep=5;
int reset=6;
int enable=7;
int gnd=8;
int detect=2;
int ByteReceived;
void setup() {
Serial.begin(9600);
pinMode(sleep,OUTPUT);
pinMode(reset,OUTPUT);
pinMode(enable,OUTPUT);
pinMode(gnd,OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(detect, INPUT);
k=1;
}
void loop() {
digitalWrite(sleep,HIGH);
digitalWrite(reset,HIGH);
digitalWrite(enable,LOW);
digitalWrite(gnd,LOW);
curMills = micros();
if (k==1)
{
for(int i =0;i<=200;i++)
{
clockwise();
Serial.println(i);
}
k=k+1;
}
}
void clockwise()
{ digitalWrite(dirPin,LOW);
if((curMills-prevStepMills) >= millisBetweenSteps)
{
prevStepMills = curMills;
digitalWrite(stepPin,HIGH);
digitalWrite(stepPin,LOW);
}
}
void cclockwise()
{digitalWrite(dirPin,HIGH);
if((curMills-prevStepMills) >= millisBetweenSteps)
{
prevStepMills = curMills;
digitalWrite(stepPin,HIGH);
digitalWrite(stepPin,LOW);
}
}
thanks in advance