Problems with making a stepper motor move-stop-move repeatedly for ever

Hello Folks,

I’m trying to make a Stepper motor move during 8 seconds, stop 30 seconds and repeat this cycle; however,

my Stepper motor just move 8 seconds, stop and it doesn’t move again, what’s wrong with my code?

Thanks for your time!!

#include <AccelStepper.h>
/////
AccelStepper stepper(1,9,8); 
////
void setup()
{  
  stepper.setMaxSpeed(1000);
  stepper.setAcceleration(1000);
}
////
void loop()
{    
  stepper.runSpeed();
  stepper.moveTo(8000);
  while(stepper.currentPosition() != 9000)
    stepper.run();
  stepper.stop();
  //stepper.runToPosition();
  delay(30000);
}

The program is doing exactly as expected. It moves the motor to 8000 and stops.

What is this line supposed to do?

  while(stepper.currentPosition() != 9000)
    stepper.run();

jremington:
The program is doing exactly as expected. It moves the motor to 8000 and stops.

What is this line supposed to do?

  while(stepper.currentPosition() != 9000)

stepper.run();

Moves the stepper until that position, when it arrives to that position, stops.

Well the program is doing his job, but i just want to add a loop that makes the motor stop and move agaian after 30seconds haha

You don't need this line at all

while(stepper.currentPosition() != 9000)

just call stepper.run() in every iteration of loop(). It will automatically stop when it gets to its destination.

You can use stepper.distanceToGo() to check if it has reached the destination. When it has, record the value of millis() like this

if (stepper.distanceToGo() == 0 && motorMoving == true) {
   motorStoppedMillis = millis();
   motorMoving = false;
}

and then you can check for the interval like this

if (millis() - motorStoppedMillis >= interval) {
   // set new destination for the motor
   motorMoving = true;
}

Have a look at how millis() is used to manage timing without blocking in Several things at a time

...R
Stepper Motor Basics
Simple Stepper Code

Robin2:
You don’t need this line at all

while(stepper.currentPosition() != 9000)

just call stepper.run() in every iteration of loop(). It will automatically stop when it gets to its destination.

You can use stepper.distanceToGo() to check if it has reached the destination. When it has, record the value of millis() like this

if (stepper.distanceToGo() == 0 && motorMoving == true) {

motorStoppedMillis = millis();
  motorMoving = false;
}




and then you can check for the interval like this


if (millis() - motorStoppedMillis >= interval) {
  // set new destination for the motor
  motorMoving = true;
}




Have a look at how millis() is used to manage timing without blocking in [Several things at a time](http://forum.arduino.cc/index.php?topic=223286.0)

...R
[Stepper Motor Basics](http://forum.arduino.cc/index.php?topic=284828.0)
[Simple Stepper Code](http://forum.arduino.cc/index.php?topic=277692.0)

Thank you so much for your help!!

i found another way to make the motor do what i want without using the library <Accelstepper.h>

Here is the code:

#include <EEPROM.h>
int addr = 0;
int Pin = 9;
 int i;
int j;

// the setup function runs once when you press reset or power the board
void setup() {
  pinMode(Pin, OUTPUT);
}

// the loop function runs over and over again forever
void loop() 
{
  for(i=0; i <=12500; i++){
    j++;
//  EEPROM.write(j);
  
  digitalWrite(Pin, HIGH);   
  delayMicroseconds(315);                       
  digitalWrite(Pin, LOW);    
  delayMicroseconds(315);                       

delay(2000);
i = 0;
}

Now i’m trying to save where is the position of the motor if the power turn of, so when it comeback it will end in the same position, that’s why i’m using EEPROM :stuck_out_tongue:

Gendrak:
Now i'm trying to save where is the position of the motor if the power turn of, so when it comeback it will end in the same position, that's why i'm using EEPROM :stuck_out_tongue:

It is not practical to write to the EEPROM for every step. The EEPROM can only take about 100,000 writes. Also, writing to the EEPROM is quite slow.

You could possibly use an SD Card but writing to it will also be slow - maybe even slower.

Even if you record the position correctly there is no guarantee that the motor won't have been moved while the Arduino is off. Even f it is not moved it won't necessarily power up at the exact same step - it might move forwards or backwards one step.

The usual way to position a stepper is to drive it towards a limit switch at startup. When the switch is triggered the Arduino will know that the motor is at the HOME or ZERO position.

If you intend to grow that program into something more complex you should probably get rid of all the delay()s and use millis() or micros() to manage the timing without blocking as in the second of my examples.

...R