Sequential fading LEDs with the Adruino UNO R3

Hi. I got this code from a youtube video to make a set of LEDs blink in sequence. I am new with the Arduino UNO R3, and electronics in general. This code works great, but what I want to do is have 4 LEDs fade in and out during the sequence. I've seen the sketch code for one fading LED in your Examples > Basics > Fade, but I don't know how to incorporate that into my sketch, or vise versa. Here is the code. Thanks very much.

[code]
/* A simple program to sequentially turn on and turn off 3 LEDs */


int led1 = 13;
int led2 = 12;
int led3 = 11;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
}

void loop() {
  digitalWrite(led1, HIGH);   // turn on led1
  delay(1000);                 // wait for 1000
  digitalWrite(led2, HIGH);   // turn on led2
  delay(1000);                 // wait for 1000
  digitalWrite(led3, HIGH);   // turn on led3
  delay(1000);                 // wait for 2000ms
  digitalWrite(led1, LOW);   // turn off led1
  delay(1000);                 // wait for 1000ms
  digitalWrite(led2, LOW);   // turn off led2
  delay(1000);                 // wait for 1000ms
  digitalWrite(led3, LOW);   // turn off led3
  delay(1000);                 // wait for 1000ms
}

[/code]

what I want to do is have 4 LEDs fade in and out during the sequence

That does not really explain what you want but I would guess that the first thing to be done is to rewrite the sketch so that it uses millis() correctly, instead of delay(...).

You take that fade code and write it as a function that accepts an LED pin number and fades that PIN number.
Then you call that function each time you do a digital write in the code you posted. And you remove those delays in the code you posted.

Note only 6 of the Arduino pins will work with that function because it depends on the pin having a PWM capability. You can spot them because they have a ~ next to the PIN number on the board.

I would hang on before you try and use the millis() function as that is a step up in complexity, get to know the basics first.

I suspect you will have to look up what is a function first and how you pass numbers into it.

Hi folks,
I obtained a new code to make this work. Thanks.

Hi. Please keep in mind that I am new at this. I'm using a factory Arduino UNO R3 to program an ATtiny45. have a Sketch for 4 LEDs that I download onto the tiny, and it displays a pattern on the LEDs. I would like to take it up a notch, and repeat about 3 or 4 different sets of patterns so that the tiny runs a "Demo" of the different patterns without cutting & pasting the code many times. The following is a code for a sequence. This code causes a pair of LEDs to blink (strobe) twice fast, and another pair to do the same. I hope I've provided enough info. If I haven't, I'm sorry. Thanks! Here is the Sketch:

int Led1 = 0;
int Led2 = 1;
int Led3 = 2;
int Led4 = 4;

void  setup(){
pinMode (Led1, 5);
pinMode (Led2, 6);
pinMode (Led3, 7);
pinMode (Led4, 3);
}

void loop(){
digitalWrite (Led1, HIGH);
delay (5);
digitalWrite (Led2, HIGH);
delay (5);
digitalWrite (Led3, HIGH);
delay (5);
digitalWrite (Led4, HIGH);
delay (5);
digitalWrite (Led1, LOW);
delay (5);
digitalWrite (Led2, LOW);
delay (5);
digitalWrite (Led3, LOW);
delay (5);
digitalWrite (Led4, LOW);
delay (5);
delay (800);
digitalWrite (Led1, HIGH);
delay (5);
digitalWrite (Led2, HIGH);
delay (5);
digitalWrite (Led3, HIGH);
delay (5);
digitalWrite (Led4, HIGH);
delay (5);
digitalWrite (Led1, LOW);
delay (5);
digitalWrite (Led2, LOW);
delay (5);
digitalWrite (Led3, LOW);
delay (5);
digitalWrite (Led4, LOW);
delay (5);
delay (800);
digitalWrite (Led1, HIGH);
delay (5);
digitalWrite (Led2, HIGH);
delay (5);
digitalWrite (Led3, HIGH);
delay (5);
digitalWrite (Led4, HIGH);
delay (5);
digitalWrite (Led1, LOW);
delay (5);
digitalWrite (Led2, LOW);
delay (5);
digitalWrite (Led3, LOW);
delay (5);
digitalWrite (Led4, LOW);
delay (5);
delay (800);
digitalWrite (Led1, HIGH);
delay (5);
digitalWrite (Led1, LOW);
delay (5);
digitalWrite (Led2, HIGH);
delay (5);
digitalWrite (Led2, LOW);
delay (5);
digitalWrite (Led3, HIGH);
delay (5);
digitalWrite (Led3, LOW);
delay (5);
digitalWrite (Led4, HIGH);
delay (5);
digitalWrite (Led4, LOW);
delay (5);
delay (800);

}

Put the patterns in a 2 dimensional array and iterate through it with code in a function

Sorry, I do not know what "Put the patterns in a 2 dimensional array and iterate through it with code in a function" means, but thanks anyway. Like I said, I am new at this, and that reply is way over my head at this point. I will do some more research and tutorials.

Do some research on arrays then look at the following code with your new found knowledge

In practice you should be able to change it to use your pin numbers and patterns quite easily but it is important that you understand how it works so that you can use the techniques in future projects

const byte ledPins[] = {3, 5, 6, 9};  //list of LEDs
byte patterns[2][16] =    //2 patterns each with 4 subpatterns
{
  {LOW, LOW, LOW, LOW,   HIGH, HIGH, HIGH, HIGH,   LOW, LOW, LOW, LOW,   HIGH, HIGH, HIGH, HIGH},
  {HIGH, LOW, HIGH, LOW,   HIGH, HIGH, HIGH, HIGH,   HIGH, LOW, HIGH, LOW,   HIGH, HIGH, HIGH, HIGH}
};



void setup()
{
  for (int led = 0; led < 4; led++)
  {
    pinMode(ledPins[led], OUTPUT);
    digitalWrite(ledPins[led], HIGH); //set start LED states
  }
}

void loop()
{
  for (int pattern = 0; pattern < 2; pattern++) //2 different sets of patterns
  {
    for (int subPattern = 0; subPattern < 4; subPattern++)  //4 subpatterns withing each pattern
    {
      for (int led = 0; led < 4; led++) //4 LEDs in each display
      {
        digitalWrite(ledPins[led], patterns[pattern][(subPattern * 4) + led]);  //calculate position in pattern, read value and and write to corresponding LED
      }
      delay(500); //wait a while after writing to all 4 LEDs
    }
  }
}

Oops, I forgot to attach this pic.

unor3man:
Oops, I forgot to attach this pic.

Twice ?

Thanks for the tips!

Lucky you! :roll_eyes:

@unor3man

TOPIC MERGED. Do NOT cross post / duplicate posts.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum.