Programming String of LED Lights to Blink, Fade, then repeat process

Hello Everyone,

I’ve been trying essentially combine the blink and fade sketches together. I would like the LED string that I’m working with to blink 3 times, then fade up and down 50 times, then repeat the whole process. I’ve read several different forum posts that almost describe what I’m trying to do, and looked at several example sketches.

I’ve gotten as far as getting the LED string to blink 3 times, then start the fade up and fade down, but I can’t seem to make the process start all over again.

This is my code (in the led fade up and fade down section I’m trying to use 2 cycles instead of the 50 for testing purposes). The code runs without errors, but doesn’t do what I want. I think it is likely something simple I’ve missed.

const int ledPin = 3; //PWM output 3 controls leds
int brightness = 0;
int fadeAmount = 1;
int counter = 0;
int counter2 = 0;

void setup() {
pinMode(ledPin, OUTPUT); //sets ledLight as output
}

void loop() {
beginning:
for(int counter2 = 0; counter <=3; counter++) {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
for(int counter = 0; counter <=2; counter++)
analogWrite(ledPin, brightness); {
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255)
fadeAmount = -fadeAmount;
delay(30);
}
}

Any help would be appreciated,

D

Does that even compile?
First, read the sticky at the top of the forum “Read this before posting a programming question”. If you post your code as described in the sticky, more forum members will read it.

beginning:
    for(int counter2 = 0; counter <=3; counter++) {

??

I'm not sure the purpose of the "beginning" label in your loop() function. There is rarely ever a need for a label in C/C++ unless you have a goto, which is not a good idea.

Also, you have global 'counter' and 'counter2' variables. Those are NOT the same as the local 'counter' and 'counter2' variables you define in your for loops! In your first for loop you define a local 'counter2' variable but use the global 'counter' variable to control the for loop. After the first time through the loop() function the global 'counter' never gets reset to 0 and therefore the first for loop does not execute the second and subsequent times through the loop() function. This is why it appears you can't "make the process start all over again."

I don't think the second for loop is doing what you think. There is only one line of code in the second for loop which is the analogWrite() function. I suspect that is not what you intended.

Admittedly I am unsure of exactly what I’m doing. This is the code formatted correctly for the forum. I tried many different control structures while looking at the language reference. This code does compile and successfully flashed the LEDs then begins to fade them. It just never starts to flash the leds again after a set number of fade cycles.

const int ledPin = 3; //PWM output 3 controls leds
int brightness = 0;
int fadeAmount = 1;
int counter = 0;
int counter2 = 0;

void setup() {
  pinMode(ledPin, OUTPUT); //sets ledLight as output
}

void loop() {
beginning:
  for (int counter2 = 0; counter <= 3; counter++) {
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    delay(1000);
  }
  for (int counter = 0; counter <= 2; counter++)
    analogWrite(ledPin, brightness); {
    brightness = brightness + fadeAmount;
    if (brightness == 0 || brightness == 255)
      fadeAmount = -fadeAmount;
    delay(30);
  }
}

Like I said in my previous response you have a problem with your first for loop and your second for loop probably doesn’t do what you think it does. Fix those 2 issues and see what happens.

for (int counter2 = 0; counter <= 3; counter++) {
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    delay(1000);
  }

well first of all, you initialize counter2 int the loop, and the increment counter, that may work, but only by coincidence, and probably only once, after which the loop is not looping anymore because the global variable has kept increasing but is never reset.

for (int counter = 0; counter <= 2; counter++)
    analogWrite(ledPin, brightness); {
    brightness = brightness + fadeAmount;
    if (brightness == 0 || brightness == 255)
      fadeAmount = -fadeAmount;
    delay(30);
  }

This loop only runs twice initially, but since brightness is not reset, the fading in and out is still happening, but the faded amount only lasts 30ms. Anyway it is not hugely complicated, but you got it quite wrong. Fix the first loop first.
then for the second loop, how about within the for loop you do something like

{
    while (brightness < 255) {
      brightness = brightness + fadeAmount;
      analogWrite(ledPin, brightness);
      delay(30);
    }
    fadeAmount = -fadeAmount;
    while (brightness > 0) {
      brightness = brightness + fadeAmount;
      analogWrite(ledPin, brightness);
      delay(30);
    }
    fadeAmount = -fadeAmount;

Thanks a lot for the advice! I worked through it. It works the way I wanted. I appreciate your help!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.