LED Arrays with Millis

Hi Everyone,
I’m working on a led project with a Mega 2560 where I created 2 different sketches and I need to combine them into one. The first sketch is comprised of 8 leds where each led turns on and stays on, one at a time, every 50ms, until all leds are lit and then they all shut off at once and the whole process repeats itself.

The 2nd sketch is comprised of 4 leds where each leds comes on for 1 second and then shuts off and then the next led lights up and does the same thing, this going left to right down a row of 4 leds. There is also a push button that when pressed reverses the order they light up, making it go right to left.

I used “delay” in both sketches to establish the timing and in order to combine them into one sketch I've been studying up on using “millis” for the last 5 days. After endless YouTube videos, articles and experimenting, I have a basic understanding of millis, but I just can’t get it to work and it feels like time to ask for some help.

When I wrote the sketches, I put all the leds in arrays and created “for() Loop” functions for them which I’m beginning to think is the problem why millis won’t work. The millis interval works fine during experimenting for turning specific leds on & off but I can’t figure out how to use it to run “for()Loop” functions, not to mention the push button in sketch #2.
I'm still pretty new to all this, it's kind of like trying to learn Chinese all by yourself. I’ll include both sketches and any help would be appreciated.

//Sketch #1
int timer = 50;
int blueLeds[8] = {2,3,4,5,6,7,8,9};
int pinCount = 8;

void setup() {
  for (int thisPin = 0; thisPin < pinCount; thisPin++){
  pinMode(blueLeds[thisPin], OUTPUT);
     }
}
void loop() {

 for (int thisPin = 0; thisPin < pinCount; thisPin++){
 
   digitalWrite(blueLeds[thisPin], HIGH);
    delay(timer);
     }
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--){
 digitalWrite(blueLeds[thisPin], LOW);
  
}
}

Sketch #2
int timer1 = 750;
int redLeds[4] = {32, 34, 36, 38};
int redLeds1[4] = {38, 36, 34, 32};
int pinCount1 = 4;
int button = 40;
int buttonState = 0;

void setup() {

  for (int thisPin = 0; thisPin < pinCount1; thisPin++) {
    pinMode(redLeds[thisPin], OUTPUT);
    pinMode(redLeds1[thisPin], OUTPUT);
    pinMode(button, INPUT);
  }

}
void loop() {

  buttonState = digitalRead(button);
  if (buttonState == LOW)

    for (int thisPin = 0; thisPin < pinCount1; thisPin++) {
      digitalWrite(redLeds[thisPin], HIGH);
      delay(timer1);
      digitalWrite(redLeds[thisPin], LOW);
    }

  else
  {
for (int thisPin = 0; thisPin < pinCount1; thisPin++) {
      digitalWrite(redLeds1[thisPin], HIGH);
      delay(timer1);
      digitalWrite(redLeds1[thisPin], LOW);
    }
  }

}

That was not too clever, unless you want to use multitasking,
which does not really fit a Mega.

What I'm missing is your try to rework and then combine the sketches.
I will not do that conversion for you.

Hi @cptech .
Post the sketch containing the 2 sketches together, tell us how it should work and what is going wrong.

It helps a lot if you attach a schematic of your project's links.

RV mineirin

Both of the sketches I included earlier work perfectly separate from each other. It’s when I’m trying to combine the 2 in one sketch and using the “blink without delay” sketch as an example base is when neither sketch will work in this new combined sketch. I’m pretty sure everything before “void Loop” is correct. It’s when I get to the loop function that I’m completely lost. Turning led states high or low seems wrong. I need to turn on and run multiple “for() loop” at the same time. If you could point me in the right direction or recommend a book or an article or another sketch that deals with something similar to what I’m trying to accomplish that would really help.
Here is a recent try of mine which is one of many ways I found it does not work.
There is stuff missing, there's no code for the switch or the reversing of the red lights. I'm just starting small and trying to get 2 sketches to work together first and then I'll try to add other options later.

//int timer = 50;
int blueLeds[8] = {2, 3, 4, 5, 6, 7, 8, 9};
int pinCount = 8;

//int timer1 = 1000;
int redLeds[4] = {32, 34, 36, 38};
int pinCount1 = 4;
int button = 40;

int blueLedsState = LOW;
int redLedsState = LOW;

unsigned long previousMillisblue = 0;
unsigned long previousMillisred = 0;
const long intervalblue = 50;
const long intervalred = 1000;



void setup() {
  for (int thisPin = 0; thisPin < pinCount; thisPin++)
    for (int thisPin = 0; thisPin < pinCount1; thisPin++) {
      pinMode(blueLeds[thisPin], OUTPUT);
      pinMode(redLeds[thisPin], OUTPUT);
    }

}

void loop() {
  unsigned long currentMillisblue = millis();
  unsigned long currentMillisred = millis();


  if (currentMillisblue - previousMillisblue >= intervalblue) {

    previousMillisblue = currentMillisblue;


    if (blueLedsState == LOW) {
      blueLedsState = HIGH;
    } else {
      blueLedsState = LOW;
    }

  }

  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    digitalWrite(blueLeds[thisPin], HIGH);
    //delay(timer);
  }

  for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
    digitalWrite(blueLeds[thisPin], LOW);
  }


  if (currentMillisred - previousMillisred >= intervalred) {

    previousMillisred = currentMillisred;


    if (redLedsState == LOW) {
      redLedsState = HIGH;
    } else {
      redLedsState = LOW;
    }

    for (int thisPin = 0; thisPin < pinCount1; thisPin++) {
      digitalWrite(redLeds[thisPin], HIGH);
      //delay(timer1);
      digitalWrite(redLeds[thisPin], LOW);

    }
  }
}

Hi @cptech .
Try this skerch.

RV mineirin


int blueLeds[8] = {2, 3, 4, 5, 6, 7, 8, 9};
int redLeds[4] = {32, 34, 36, 38};
int pinCount = 8;
int pinCount1 = 4;
int button = 40;
unsigned long previousMillisblue = 0;
unsigned long previousMillisred = 0;
int myDelay1 = 50;
int myDelay2 = 1000;
int bluePin = 2;
int redPin = 2;
//----------------------------------------------
void setup() {
  Serial.begin(9600);
  pinMode(button, INPUT);
  for (int bluePin = 0; bluePin < pinCount; bluePin++)
  {
    pinMode(blueLeds[bluePin], OUTPUT);
  }
  for (int redPin = 0; redPin < pinCount1; redPin ++)
  {
    pinMode(redLeds[redPin], OUTPUT);
  }
  bluePin = 0;
  redPin = 0;
  previousMillisred = millis();
}
//----------------------------------------------
void blueLEDs()
{
  if (bluePin < pinCount )
  {
    digitalWrite(blueLeds[bluePin], HIGH);
  }
  if (bluePin >= pinCount )
  {
    for (int bluePin = 0; bluePin < pinCount; bluePin++)
    {
      digitalWrite(blueLeds[bluePin], LOW);
    }
    bluePin = 0;
    return;
  }
  bluePin++;
}
//----------------------------------------------
void redLEDs()
{
  if (digitalRead(button) == LOW)
  {
    if (redPin < pinCount1 )
    {
      if (digitalRead(redLeds[redPin]) == LOW)
      {
        digitalWrite(redLeds[redPin], HIGH);
      }
      else
      {
        digitalWrite(redLeds[redPin], LOW);
        redPin++;
      }
    }
    if (redPin >= pinCount1 )
    {
      redPin = 0;
    }
  }
  else
  {
    if (redPin < pinCount1 )
    {
      if (digitalRead(redLeds[redPin]) == LOW)
      {
        digitalWrite(redLeds[redPin], HIGH);
      }
      else
      {
        digitalWrite(redLeds[redPin], LOW);
        redPin--;
      }
    }
    if (redPin < 0 )
    {
      redPin = 3;
    }
  }
}
//----------------------------------------------
void loop() {
  if (millis() - previousMillisblue >= myDelay1 )
  {
    blueLEDs();
    previousMillisblue = millis();
  }
  if (millis() -  previousMillisred >= myDelay2 )
  {
    redLEDs();
    previousMillisred = millis();
  }
}

Thank you for the sketch, RV mineirin.
I tested it out on breadboard and it works perfectly!
Now I have to dissect it line by line and try to figure out how you came to these conclusions in case I choose to add even more to this sketch.

You eliminated the “for() loops and created “if” statements

You created “void blueLeds” and “void redLeds
I would of never even thought to think you could do that.

I still have to study it to figure out how the red leds are reversing

There is so much I do not know. I want to ask what do I need to study to get a handle on all this. Trial by error only takes you so far, especially when you don’t even know what you don’t know.

That is exactly what you must not do. All for loops must be eliminated.

Have a look at my explanation:-
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0