Running stepper motor and led sequence simultaneously.

I have 2 pieces of code:

1 that runs a stepper motor (cw, ccw, or stopped) and,

1 that runs LEDs in a specific sequence.

They each work independently perfectly fine but I cant seem to figure out how to make them run together at the same time, I think the issue is coming with not getting the “if” statements synched up right.

Both get a signal from different buttons that send signal to pwm ports.

I am fine with both processes being controlled form one button too!

I attached both separate pieces of code, If someone could help look them over and give me a direction on how to combine them that would be awesome!

I am very new to coding especially with the Arduino so any help would be appreciated. Thanks!

Here are the 2 pieces of code!

//==================Lights Code======================

//================variables===============================

int buttonvalue = 0;
int button = 8;
int green = 7;
int blue = 9;
int red = 5;

//=============void setup======================

void setup(){

pinMode(button, INPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(red, OUTPUT);

}

//=============void loop================================

void loop(){

buttonvalue = digitalRead(button);

if(buttonvalue !=0){

delay(1000);
//---------------------------------------
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(red, HIGH);
delay(1000);
//1 OFF
digitalWrite(green, LOW);
digitalWrite(blue, HIGH);
digitalWrite(red, HIGH);
delay(1000);
//1,2 OFF
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
digitalWrite(red, HIGH);
delay(1000);
//1,2,3 OFF
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
digitalWrite(red, LOW);
delay(1000);
//--------------------------------------
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(red, HIGH);
delay(200);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
digitalWrite(red, LOW);
delay(200);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(red, HIGH);
delay(200);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
digitalWrite(red, LOW);
delay(200);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(red, HIGH);
delay(200);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
digitalWrite(red, LOW);
delay(200);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(red, HIGH);
delay(200);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
digitalWrite(red, LOW);
delay(200);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(red, HIGH);
delay(200);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
digitalWrite(red, LOW);
delay(200);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(red, HIGH);
delay(200);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
digitalWrite(red, LOW);
delay(200);
//-------------------------------------
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(red, HIGH);
delay(1000);
//1 OFF
digitalWrite(green, LOW);
digitalWrite(blue, HIGH);
digitalWrite(red, HIGH);
delay(1000);
//1,2 OFF
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
digitalWrite(red, HIGH);
delay(1000);
//1,2,3 OFF
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
digitalWrite(red, LOW);
delay(1000);
//-------------------------------------
digitalWrite(green, LOW);
digitalWrite(blue, HIGH);
digitalWrite(red, HIGH);
delay(100);
digitalWrite(green, HIGH);
digitalWrite(blue, LOW);
digitalWrite(red, HIGH);
delay(100);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(red, LOW);
delay(100);
digitalWrite(green, LOW);
digitalWrite(blue, HIGH);
digitalWrite(red, HIGH);
delay(100);
digitalWrite(green, HIGH);
digitalWrite(blue, LOW);
digitalWrite(red, HIGH);
delay(100);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(red, LOW);
delay(100);
digitalWrite(green, LOW);
digitalWrite(blue, HIGH);
digitalWrite(red, HIGH);
delay(100);
digitalWrite(green, HIGH);
digitalWrite(blue, LOW);
digitalWrite(red, HIGH);
delay(100);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(red, LOW);
delay(100);
digitalWrite(green, LOW);
digitalWrite(blue, HIGH);
digitalWrite(red, HIGH);
delay(100);
digitalWrite(green, HIGH);
digitalWrite(blue, LOW);
digitalWrite(red, HIGH);
delay(100);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(red, LOW);
delay(100);
}else{
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
digitalWrite(red, LOW);}
}

//===================motor code========================

//======================Variables===============

int Pin1 = 10;//IN1 is connected to 10
int Pin2 = 11;//IN2 is connected to 11
int Pin3 = 12;//IN3 is connected to 12
int Pin4 = 13;//IN4 is connected to 13

int switchCW =2;//define input pin for CW push button
int switchStop=3;//define input pin for Stop push button
int switchCCW =4;//define input pin for CCW push button

int pole1 ={0,0,0,0, 0,1,1,1, 0};//pole1, 8 step values
int pole2 ={0,0,0,1, 1,1,0,0, 0};//pole2, 8 step values
int pole3 ={0,1,1,1, 0,0,0,0, 0};//pole3, 8 step values
int pole4 ={1,1,0,0, 0,0,0,1, 0};//pole4, 8 step values

int poleStep = 0;
int dirStatus = 3;// stores direction status 3= stop (do not change)

//==========Void Setup========================================

void setup()
{

pinMode(Pin1, OUTPUT);//define pin for ULN2003 in1
pinMode(Pin2, OUTPUT);//define pin for ULN2003 in2
pinMode(Pin3, OUTPUT);//define pin for ULN2003 in3
pinMode(Pin4, OUTPUT);//define pin for ULN2003 in4

pinMode(switchCW,INPUT_PULLUP);// CW push button pin as input
pinMode(switchStop,INPUT_PULLUP);//Stop push button pin as input
pinMode(switchCCW,INPUT_PULLUP);//CCW push button pin as input

}

//===========void loop============================

void loop()
{

if(digitalRead(switchCCW) == LOW)
{
dirStatus =1;
}else if(digitalRead(switchCW) == LOW)
{
dirStatus = 2;
}else if(digitalRead(switchStop) == LOW)
{
dirStatus =3;
}
if(dirStatus ==1){
poleStep++;
driveStepper(poleStep);
}else if(dirStatus ==2){
poleStep–;
driveStepper(poleStep);
}else{
driveStepper( 8 );
}
if(poleStep>7){
poleStep=0;
}
if(poleStep<0){
poleStep=7;
}
delay(1);

}

void driveStepper(int c)
{
digitalWrite(Pin1, pole1

);  
     digitalWrite(Pin2, pole2[c]); 
     digitalWrite(Pin3, pole3[c]); 
     digitalWrite(Pin4, pole4[c]);   
}


[final_project_lights.ino|attachment](upload://6xAH7KHyd5Cj5t8tZXUH9tvyQtX.ino) (3.8 KB)

[final_project_motor.ino|attachment](upload://fYPwX9PPlgcdWKTsNpdM9u8pBKH.ino) (2.17 KB)

Now that you have two pieces of code that tested the hardware, so you know the hardware is good, scrap both of them. Write a new program that does what you want.

You will NOT be using delay() anywhere in your code, so look at the threads in Introductory Tutorials, to learn how to do many things at once.

Learn how to use enums. Direction states of CW, CCW, and STOPPED are far easier to understand than states of 1, 2, and 3.

the problem with combining the code is that the LED code with all the delays takes about ~10 seconds to complete. That means the stepper motor would only step once every ~10 seconds

in order to merge the two, you would need to make the LED code state driven so that it can be repeatedly called without blocking, allowing other processing (e.g. motor)

i would suggest you define a struct that identifies states for each LED and a delay value. then create a table, an array of structs that describes you code as data

struct LedSeq_s {
    byte  green;
    byte  blue;
    byte  red;
    unsigned long  msec;
};


struct LedSeq_s  ledSeq [] = {
   { HIGH, HIGH, HIGH, 1000 },
   { LOW,  HIGH, HIGH, 1000 },
   { LOW,  LOW,  HIGH, 1000 },
    ...
};

and write code to step through each element in the table, change the led state and using millis() to wait in that state until the specified time has past before moving to the next state.

create a new loop() that calls the led and motor sub-functions.

of course there are many things you could do to simplify the code.

  • instead of calling driveStepper() in two of the 5 cases, why not call driveStepper(poleStep) once near the end of every iteration of loop(). set poleStep to 8 by default and handle wrap as you are

  • in your motor code, instead of setting dirStatus used to later modify poleStep, why not modify poleStep directly?

  • is the purpose of the delay to deal with button bounce?:

The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

Also look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

...R