Hello everyone, Super new to Arduino programming and trying to make a basic stepper motor back and forth move, and count how many cycles has the motor spines. Here is my code to make the stepper motor spin back and forth with some degree. (not a full rotation)
// defines pins numbers
const int reset_pin = 7;
const int sleep_pin = 5;
const int stepPin = 3;
const int dirPin = 2;
const int en_pin = 8;
const int M1_pin = 16;
const int M2_pin = 14;
const int M3_pin = 15;
const int switch_pin = 10;
const int stepsPerRevolution = 200;
void setup() {
pinMode(reset_pin,OUTPUT);
pinMode(sleep_pin,OUTPUT);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(en_pin,OUTPUT);
pinMode(M1_pin,OUTPUT);
pinMode(M2_pin,OUTPUT);
pinMode(M3_pin,OUTPUT);
// init driver
digitalWrite(en_pin,LOW);
digitalWrite(sleep_pin,HIGH);
digitalWrite(reset_pin,HIGH);
digitalWrite(dirPin,LOW);
// use full step
digitalWrite(M1_pin,LOW);
digitalWrite(M2_pin,LOW);
digitalWrite(M3_pin,LOW);
void loop() {
//delay(100);
digitalWrite(dirPin,HIGH);
for(int x = 0; x <180 ; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(5000);
digitalWrite(stepPin,LOW);
delayMicroseconds(5000);
}
delay(10);
digitalWrite(dirPin,LOW); //Changes the rotations direction
for(int x = 0; x < 180; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(5000);
digitalWrite(stepPin,LOW);
delayMicroseconds(5000);
}
delay(10);
}
Considering that one back&forth move is one cycle, I wanna run it for lets say 10 cycles and then stop the motor! How can I do it ? Right now with this code, I can run it with no problem, but I wanna stop after 10 cycles.
I tried adding another for loop inside the void loop , and break at the end. seems like not working.
I am really new in programming, so I would appreciate any kind of help.
Thank you