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;
}
}
}