Blinking LED with Sequence

Hey! So basically im new to all this coding, but what I am trying to do is have a separate LED blink the number of times I have gone through the sequence to a cap of five times. After it does it five times it just hold to white and restarts the sequence.

I have no clue where to start, but I have the code of the sequence...

int red = 6;
int green = 4;
int blue = 3;
void setup() {
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);

  digitalWrite(red, HIGH);
  digitalWrite(green, LOW);
  digitalWrite(blue, LOW);
}
int redValue;
int greenValue;
int blueValue;

void loop() {
  #define delayTime 25

  for(int i=0; i<255;i+= 1)
  {
    redValue -= 1;
    greenValue += 1;
    analogWrite(red, redValue);
    analogWrite(green, greenValue);
    delay(delayTime);
  }

  redValue=0;
  greenValue=255;
  blueValue=0;

  for(int i=0; i<255; i+=1)
  {
    greenValue -= 1;
    blueValue +=1;
    analogWrite(green, greenValue);
    analogWrite(blue, blueValue);
    delay(delayTime);
  }
  redValue=0;
  greenValue=0;
  blueValue=255;

  
  for(int i=0; i<255; i+=1)
  {
    blueValue -= 1;
    redValue += 1;
    analogWrite(blue, blueValue);
    analogWrite(red, redValue);
    delay(delayTime);
  }
#define delayTime 1

  for(int i=0; i<255;i+= 1)
  {
    redValue -= 1;
    greenValue += 1;
    analogWrite(red, redValue);
    analogWrite(green, greenValue);
    delay(delayTime);
  }

  redValue=0;
  greenValue=255;
  blueValue=0;

  for(int i=0; i<255; i+=1)
  {
    greenValue -= 1;
    blueValue +=1;
    analogWrite(green, greenValue);
    analogWrite(blue, blueValue);
    delay(delayTime);
  }
  redValue=0;
  greenValue=0;
  blueValue=255;

  
  for(int i=0; i<255; i+=1)
  {
    blueValue -= 1;
    redValue += 1;
    analogWrite(blue, blueValue);
    analogWrite(red, redValue);
    delay(delayTime);
  }
}

It's basically just an RGB LED that fades into Red, Green, and Blue slowly and goes throught it faster the second time.

I will really appreciate some help

Thanks!

These 2 statements seem to contradict each other.

Welcome to the forums. Kuddos for using code tags!

So, you have your fading sequence (copied from somewhere, I am guessing) and now you want to add another LED and blink it the number of times through loop, but only up to 5?

The easiest method would be to have a new, global variable that you increment each time loop() begins and, if it is greater than 5, reset it to 5. Then blink that many times (or all WHITE if at 5) and then continue on with your sequence as defined.

The better method (but more advanced) would be to study the Blink Without Delay example in the IDE (File->examples->02.digital->Blink Without Delay) and this tutorial: several things at the same time
and refactor your code to eliminate all those for() loops and delay() statements and let loop() do what it was born to do. :slight_smile:

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