I am trying to run this code only once, but cant't as the code is inside void() loop. I just want it to run once and stop where I wrote "END".
So basically, I want my motor to go 100 steps clockwise then 100 steps anti-clockwise then 200 steps clockwise and again 100 steps anticlockwise . Finally, 300 steps clockwise and stops.
I am using NEMA 17 motor with Arduino UNO and driver A4988.
It would be great if someone can help.
Thanks in advance
const int stepPin = 5;
const int dirPin = 4;
void setup() {
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
}
void loop() {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
for(int x = 0; x < 100; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000); // One second delay
digitalWrite(dirPin,LOW); //Changes the rotations direction
for(int x = 0; x < 100; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000);
digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000); // One second delay
digitalWrite(dirPin,LOW); //Changes the rotations direction to 2nd direction
for(int x = 0; x < 100; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000);
digitalWrite(dirPin,HIGH); // Enables the motor to move in a 1st direction
for(int x = 0; x < 300; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000);
END
}