Writing code for simultaneous operation

As you can see this is my first post in the forum, and while I consider myself to have some experience with elektrickery, I have very little experience programming.

The goal with the code is to drive a series of LED lights in sequence, with a "pause" button that will cause all the LEDs to go HIGH.

In addition, I've added a short sequence to drive a fan motor forward, delay, reverse, delay forward etc.

  • The arduino nano v3 scans each line and executes it before moving on to the next
  • Is there a method(s) that I can have both routines run at the same time?
  • How can I get the pause button to stop the sequence at any point? At present, the LOW is not recognized unless it is at the end/beginning of the scan cycle.

If could suggests areas, links or keywords that I can google for answers that would be helpful.

Thanks.

int out2 = 2;
int out3 = 3;
int out4 = 4;
int out5 = 5;
int out6 = 6;
int startButton = 11;
int stopButton = 12;

//Fan Motor
int ina = 10;
int inb = 9;


void setup() {
  // put your setup code here, to run once:
  pinMode(out2, OUTPUT);
  pinMode(out3, OUTPUT);
  pinMode(out4, OUTPUT);
  pinMode(out5, OUTPUT);
  pinMode(out6, OUTPUT);

// Fan Motor Pinmodes
  pinMode(ina, OUTPUT);
  pinMode(inb, OUTPUT);
  
  pinMode(startButton, INPUT);
  digitalWrite(startButton, HIGH);
  pinMode(stopButton, INPUT);
  digitalWrite(stopButton, HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(startButton) == LOW){
    digitalWrite(out2, HIGH);
    digitalWrite(out3, HIGH);
    digitalWrite(out4, HIGH);
    digitalWrite(out5, HIGH);
    digitalWrite(out6, HIGH);
  }
  else{
    digitalWrite(out2, HIGH);
    delay(250);
    digitalWrite(out2, LOW);
    digitalWrite(out3, HIGH);
    delay(250);
    digitalWrite(out3, LOW);
    digitalWrite(out4, HIGH);
    delay(250);
    digitalWrite(out4, LOW);
    digitalWrite(out5, HIGH);
    delay(250);
    digitalWrite(out5, LOW);
    digitalWrite(out6, HIGH);
    delay(250);
    digitalWrite(out6, LOW);
  }

//Fan Motor Loop
  digitalWrite(inb, LOW);
  delay(1800); //coast to a stop
  digitalWrite(ina, HIGH);
    delay(3000);
  digitalWrite(ina, LOW);
  delay(1800); //coast to a stop
  digitalWrite(inb, HIGH);
  delay(3000);
  
  
}

See Several things at the same time

So code written outside of the void loop() will work as well.

There is some great information to digest.

Thanks.

And that lead me to "Blink without delay"... Blink without delay() explained line-by-line

It was easier with Allen Bradley. :wink:

That is because PLCs have an OS that handles multiple threads for you. Low-end microcontrollers do not. Many wheels need reinventing.

"So code written outside of the void loop() will work as well."
If that code is in a function that loop() calls, that code will be run. Other the function sits out there, waiting to be called.
If the code is not in a functions and is outside of loop, that usually causes a compiler error.
Example:

void setup(){
// set up code, runs once
}
void loop(){
// the following code that runs over and over, never stopping
function1();
function2();
function1();
// some other code
} // end of loop, go back to start

void function1(){
// function1 code, waiting to be run
}
void function2(){
// function2 code, waiting to be run
}

Syntax may need a little work, but you can get the idea from that.