How do I stop led strips after specific time ?

Hello guys,

I got a project with 8 led stripes,
I use fade in/out but I found out that I can't stop in specific time...

What I want to do is led1 fade in/out for 5sec and stop, continues with led2 fade in/out for 5sec and so on...

Before I decide to do the fade in/out effect I just uses blink,
so I wonder if I can turn all my previous led HIGH/LOW into fade in / fade out ?

much thanks, really appreciate for your help !

int brightness = 0;
int fadeAmount = 5;
int delayDuration = 30;

void setup() {
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);

}

void loop() {
  {
    analogWrite(13, brightness);
    brightness = brightness + fadeAmount;
    if (brightness <= 0 || brightness >= 255) {
      fadeAmount = -fadeAmount ;
    }
    delay(delayDuration);
  }
  {
    analogWrite(12, brightness);

    brightness = brightness + fadeAmount;
    if (brightness <= 0 || brightness >= 255) {
      fadeAmount = -fadeAmount ;
    }
    delay(delayDuration);
  }
  {
    analogWrite(11, brightness);
    brightness = brightness + fadeAmount;
    if (brightness <= 0 || brightness >= 255) {
      fadeAmount = -fadeAmount ;
    }
    delay(delayDuration);
  }
  {
    analogWrite(10, brightness);
    brightness = brightness + fadeAmount;
    if (brightness <= 0 || brightness >= 255) {
      fadeAmount = -fadeAmount ;
    }
    delay(delayDuration);
  }
  {
    analogWrite(9, brightness);
    brightness = brightness + fadeAmount;
    if (brightness <= 0 || brightness >= 255) {
      fadeAmount = -fadeAmount ;
    }
    delay(delayDuration);
  }
  {
    analogWrite(8, brightness);
    brightness = brightness + fadeAmount;
    if (brightness <= 0 || brightness >= 255) {
      fadeAmount = -fadeAmount ;
    }
    delay(delayDuration);
  }
  {
    analogWrite(7, brightness);
    brightness = brightness + fadeAmount;
    if (brightness <= 0 || brightness >= 255) {
      fadeAmount = -fadeAmount ;
    }
    delay(delayDuration);
  }
  {
    analogWrite(6, brightness);
    brightness = brightness + fadeAmount;
    if (brightness <= 0 || brightness >= 255) {
      fadeAmount = -fadeAmount ;
    }
    delay(delayDuration);
  }
  

  digitalWrite(13, HIGH);
  digitalWrite(12, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  digitalWrite(9, LOW);
  digitalWrite(8, LOW);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);

  delay(1000);

  digitalWrite(13, LOW);
  digitalWrite(12, HIGH);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  digitalWrite(9, LOW);
  digitalWrite(8, LOW);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  delay(1000);

  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
  digitalWrite(11, HIGH);
  digitalWrite(10, LOW);
  digitalWrite(9, LOW);
  digitalWrite(8, LOW);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  delay(1000);

  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(8, LOW);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  delay(1000);

  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  digitalWrite(9, HIGH);
  digitalWrite(8, LOW);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  delay(1000);
}

Hi

My humle advice is try using for or while loop for turning leds in series, or better yet try using arrays in i a for loop. Writing code with a million digitalWrite(number, HIGH) and LOW is just way to many copy pastes...

You can check my discussion about 2 pages earlier called blinking LED in series...i got it working and copied a code there.

What I want to do is led1 fade in/out for 5sec and stop, continues with led2 fade in/out for 5sec and so on...

Before going any further you need to decide whether you want to be able to stop the sequence at any time and whether the Arduino is going to be doing anything else whilst fading the LEDs. The answers are important in deciding how to write the program.

Can you confirm that you are running the program on a Mega or another Arduino capable of using pins 6 to 13 for PWM output.

UKHeliBob:
Before going any further you need to decide whether you want to be able to stop the sequence at any time and whether the Arduino is going to be doing anything else whilst fading the LEDs. The answers are important in deciding how to write the program.

Can you confirm that you are running the program on a Mega or another Arduino capable of using pins 6 to 13 for PWM output.

Hi Bob,
Yes I'm running on Mega and using pin 13-6.

The result I want is that I could make each individual led stripe fade in/out in my control as what I mention the example in my post, and I think Arduino ain't doing anything else, just multiple led stripes fade in/out in different time, what my problem now is I could only make it loop but I can't stop or start it in specific time (like start in 10s, stop after 5s).

really appreciate for your helping!

almirlalicic:
Hi

My humle advice is try using for or while loop for turning leds in series, or better yet try using arrays in i a for loop. Writing code with a million digitalWrite(number, HIGH) and LOW is just way to many copy pastes...

You can check my discussion about 2 pages earlier called blinking LED in series...i got it working and copied a code there.

Hi,
Yes I've checked your post and that's really cool,
but can it fits with pwm?

really appreciate for your help !!!

Here's my advice. Store your pin numbers, brightness values and whether the strip is fading up or down in arrays. Every 5 seconds change the array index to switch between strips.

Since you keep track of the brightness level and fade direction for each strip you can begin right were you left off when switching between strips.

I wrote some code as an example of this technique. I cannot test it with strips but the serial prints look good.

int brightness = 0;
int fadeAmount = 5;
int delayDuration = 30;

const byte NUM_STRIPS = 8;        // # of striops
unsigned long fadeStart;          // when fading began for current strip
unsigned long fadeTime = 5000UL;  // interval to switch strips
unsigned long lastStep = 0UL;     // how long since last fade step on strip
byte stripIdx = 0;                // index into arrays

byte pins[NUM_STRIPS] = {13, 12, 11, 10, 9, 8, 7, 6}; // LED pins
int level[NUM_STRIPS] = {0, 0, 0, 0, 0, 0, 0, 0};    // brightness level
int fadeDirection[NUM_STRIPS] = {1, 1, 1, 1, 1, 1, 1, 1,}; // 1 => fade up, -1 => fade down


void setup() {
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);

  fadeStart = millis(); // mark start time

  Serial.begin(9600);
}

void loop() {

  // time to switch strips?
  if ( (millis() - fadeStart) >= fadeTime )
  {
    // next strip
    stripIdx++;

    // wrap to first
    if ( stripIdx == NUM_STRIPS ) stripIdx = 0;

    fadeStart = millis();  // mark start time
    lastStep = 0UL;        // reset fade step time

  } // if


  // time to perform a fade step?
  if ( (millis() - lastStep) >= delayDuration )
  {
    // write level value to LED
    analogWrite(pins[stripIdx], level[stripIdx]);

    Serial.print("Strip:  ");
    Serial.print(stripIdx);
    Serial.print("  level:  ");
    Serial.print(level[stripIdx]);
    Serial.println("");

    // mark step time
    lastStep = millis();

    // set new brigthness level
    level[stripIdx] += fadeAmount * fadeDirection[stripIdx];

    // change direction if necessary
    if ( (level[stripIdx] <= 0) && (fadeDirection[stripIdx] == -1) )
    {

      fadeDirection[stripIdx] = 1;  // fade up
      level[stripIdx] = 0;

    } else if ( (level[stripIdx] >= 255) && (fadeDirection[stripIdx] == 1) ) {

      fadeDirection[stripIdx] = -1;  // fade down
      level[stripIdx] = 255;
    }



  } // if


}

I think Arduino ain't doing anything else, just multiple led stripes fade in/out in different time,

I can't stop or start it in specific time (like start in 10s, stop after 5s).

It sounds like it is doing 2 things. It is fading LEDs and timing.

Can you please explain exactly what the program should do ?
Maybe wait 10 seconds, fade in and out for 5 seconds and stop whether or not the fading cycle is complete. Is that right ?

UKHeliBob:
It sounds like it is doing 2 things. It is fading LEDs and timing.

Can you please explain exactly what the program should do ?
Maybe wait 10 seconds, fade in and out for 5 seconds and stop whether or not the fading cycle is complete. Is that right ?

Oh yes, this is exactly what I mean, sorry for not explain properly.
wait 10s - fade in and out on led1 - led 1 stop - wait 2s - fade in and out on led2 - led 2 stop -.....continues till led 8 stop

really really appreciate your help.

Thank you ! I've tried your code,
but if I want to made my first led stop after fade in/out , where shall I adjust in the code?
thank you so much !

klansie:
Hi,
Yes I've checked your post and that's really cool,
but can it fits with pwm?

really appreciate for your help !!!

i think yes, because i increment the value of a intiger variable that a function digitalWrite uses, so basicly dont see any reason why not use analog write instead...ur gonna have a lot of nested loops but i think it's a nice beginner exercise...