Stepper Motors multitasking On/Off millis function

Hello everyone,

I am running a project for my University and I am stuck with programming the arduino. I want to multitask and run three stepper motors at the same time. Two of them will be running all the time with the press of a button, I've done that part. The third one has to run for 1 second, then wait for half a second and then run again and again and again etc... I can't use the delay function cause the other two motors will stop running. The code I have come so far is this.

At the bottom of the code there is a delay(500) in comment section. If I replace the last millis() time check, with the three lines that are written inside the if() and add the delay, the stepper motor does exactly what I want to do.

Is there a way that I can do it with millis(), in order to keep the other three motors running??

Thanks in advance,

Dimitris_C

// Define pin connections
const int stepPin = 3;
const int sleepPin = 2;

int sleepState = 0;
int buttonCurrent;
int buttonPrevious = 1;

unsigned long intervalM1 = 1;
unsigned long OnTime = 1000;
unsigned long OffTime = 500;
unsigned long PreviousTime1 = 0;
unsigned long PreviousTime2 = 0;
unsigned long PreviousTime3 = 0;

void setup()
{
  pinMode(sleepPin, OUTPUT);
  pinMode(stepPin, OUTPUT);  
}

void loop() {
  unsigned long currentTime1 = millis();
  unsigned long currentTime2 = millis();
  unsigned long currentTime3 = millis();

  if(sleepState == 0)
  {
    digitalWrite(sleepPin, HIGH);
    sleepState = 1;
  }

  if (currentTime1 - PreviousTime1 >= intervalM1)
  {
    digitalWrite(stepPin, HIGH);
    PreviousTime1 = currentTime1;
  }
  else
  {
    digitalWrite(stepPin, LOW);
    PreviousTime1 = currentTime1;
   }
   if (currentTime2 - PreviousTime2 >= OnTime)
   {
    PreviousTime2 = currentTime2;
      if (currentTime3 - PreviousTime3 >= OffTime)  //delay(500);
      {
        PreviousTime3 = currentTime3;
        digitalWrite(sleepPin, LOW);
        sleepState = 0;
      }    
   }
}

Are You studying at a university and needs to finish Your final work?
Stuck on programming? What's Your line of knowledge? Looking at Your code You're not in the programming courses, no explaining comments at all....
Use the IDE examples like "doing several thins at the same time".
Using the state machine strategy ought to work.

Why 3 current time variables ?

The way your code is written it does not "run" for 1 second and "wait" for a half second. I assume by "run" you should be continuously stepping the motor for a second and for "wait" you should cease stepping the motor for a half. The way it is coded you get a full step every 1.5 seconds (when the delay() is inserted). Additionally you don't define "run" in terms of speed (RPM). That is based on the number of steps per rotation and how fast you step the motor.

I also don't see how your other stepper could be working either.

Is there some reason you can't use a stepper library (like AccelStepper? It would make your life easier.

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