Stepper Motor timer

Hi everyone! I've been trying to make a stepper Motor rotate for a certain amount of time IF I press a button once. For the momen I managed to make this coding, the motor does start rotating after I press the button, but it doesn't stop now. How should I fix it?

#include <Stepper.h>
const int stepsPerRevolution = 512;
Stepper myStepper1(stepsPerRevolution, 10, 12, 11, 13);
int Switch = 2;

unsigned long minutes = 6000;
void setup() {
pinMode(Switch, INPUT_PULLUP);
myStepper1.setSpeed(60);

}

void turns(int numberOfRotations){
for(int i = 0; i< numberOfRotations; i++){
for(int steps = 0; i< 500; steps++) {
myStepper1.step(1);
}}}

void loop() {
if (millis() < minutes * 1) {
//do this for 15mins from start up
if(digitalRead(Switch) == LOW)
turns(10);
} else { }
}

if (millis() < 6000).......
That will happend during the first 6 seconds after power up or reset....

This seems to be wrong. millis() are milliseconds, but you compare them to minutes. Maybe you should rename the variables to make it clearer....

unsigned long SixSecondsInMilliseconds = 6000 ;

for example.

Try this.

#include <Stepper.h>
const int stepsPerRevolution = 512;
Stepper myStepper1 ( stepsPerRevolution, 10, 12, 11, 13 );
int Switch = 2;
unsigned long minutes = 6000;

void setup ()
{
  pinMode ( Switch, INPUT_PULLUP );
  myStepper1.setSpeed ( 60 );
}

void loop ()
{
  if ( millis () < minutes )
  {
    if ( digitalRead ( Switch ) == LOW ) myStepper1.step ( 10 * stepsPerRevolution ); // 10 turns
  }
}

The stepper motor should stop after about 10 seconds.

You tell the Stepper library that the stepper is 512 steps per revolution but your code implies 500 steps per revolution:

How to write Timers and Delays in Arduino deals with writing timers, but you could also use AccelStepper library and run to a given position which is probably what you actually want. You can reset the current position as 0 so you can just repeat running the same amount.

Multi-tasking in Arduino which has a complete stepper example

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