Stepper Motor cycle count

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

I also tried the following code in Void Loop () but the motor keeps running! :frowning: Hope someone can come up with a solution, many thanks !

void loop() {
if (keep_running == true) {
for (int y = 0 ; y<cycles ; y++) {

 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);
}
keep_running == false;
}
}

Hello @nativenate !

This code is pretty close to what it should be :wink:.

Yes, you need a for loop that has the stepper motor code inside of it.

For example:

boolean done = false; //added to the top of the code

//in void loop()
if (!done){//if 'done' variable is false, run the stepper motor
for (int i=0;i<10;i++){ //loop 10 times
  digitalWrite(dirPin,HIGH);
  for(int x = 0; x <180 ; x++) { // turn 180˚ one way
   digitalWrite(stepPin,HIGH); 
   delayMicroseconds(5000); 
   digitalWrite(stepPin,LOW); 
   delayMicroseconds(5000); 
 }
 delay(10); // delay between turning one way and turning back

 digitalWrite(dirPin,LOW); //Changes the rotations direction
 for(int x = 0; x < 180; x++) { //turn 180˚ the other way
   digitalWrite(stepPin,HIGH);
   delayMicroseconds(5000);
   digitalWrite(stepPin,LOW);
   delayMicroseconds(5000);
 }
}
 done = true; // set 'done' variable to true; indicating that we don't need to run the above code again
}

Edit:
Also, when posting code, place ``` at the top and bottom of your code (that way it formats correctly).

1 Like

thanks so much, I was about to message here that I solved it by removing one = :smiley:
I put instead at the end keep_running = false; and it worked wohoo

1 Like

If you go into Preferences... and set "Compiler warnings:" to "All" you will get this helpful warning:

sketch_nov29a.ino: In function 'void loop()':
sketch_nov29a.ino:33:18: warning:
 statement has no effect [-Wunused-value]
     keep_running == false;
     ~~~~~~~~~~~~~^~~~~~~~

That points directly at your mistake and would allow you to fix it before posting.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.