Fading LED to full brightness then turn it off for a period

Hi

I am trying to get my head around an LED project I'm trying to make.

I've got an Arduino Nano, I've plugged in a LED to ground and digital pin 5 (a PWN pin as far as I know) to start testing.

What I'm trying to achieve is this -

I have 2 LEDs (in the final project).

The 1st LED fades on to full brightness then stops at full brightness for a short period then turns off completely.

No fade out, just completely turns off.

Then the 2nd LED fades on to full brightness and stops at full brightness for a short period and then turns off completely.

I've found some examples of LED fade code, and had a tinker with them but I'm still not able to achieve the above effect (because admittedly my Arduino code knowledge is <=0).

Please can someone here help me out on this?

Thanks in advance.

I hope you used a resistor in series with the led as well.

The coding for this should be straight forward. Post your attempt (use code tags) so we can point you in the right direction.

Yes the LED have appropriate resistors.

Here's what I've managed to cobble together with code found elsewhere (someone on these forums posted the original of this, many thanks to him/her) I've just copied/pasted/adjusted the code for the 2nd LED and added a variable (timedelay) -

#define fadeSpeed 10
#define timedelay 3.5
#define timedelay2 5

const uint8_t fadeLed = 5;            // the pin that the fading LED is attached to (needs to be a PWM pin)
const uint8_t fadeLed2 = 6;            //pin that the 2nd LED is on
const uint8_t rgbPins[] = {2,3,4};    // Pins the R, G & B
uint8_t ledCounter = 0;

void setup() {
  pinMode(fadeLed, OUTPUT);
  for (uint8_t x = 0; x < sizeof(rgbPins); x++){
    pinMode(rgbPins[x], OUTPUT);
  }
}

void loop() {
  digitalWrite(rgbPins[ledCounter],HIGH);
  for (uint16_t x = 0; x < 256; x++){
    analogWrite(fadeLed, x);
    delay(timedelay2);
  }
  for (uint16_t x = 0; x < 256; x++){
    analogWrite(fadeLed, 255 - 255);
    delay(timedelay);
  }
  digitalWrite(rgbPins[ledCounter],LOW);
  ledCounter++;
  if (ledCounter >= sizeof(rgbPins)){
    ledCounter = 0;
  }

  //2nd LED

    digitalWrite(rgbPins[ledCounter],HIGH);
  for (uint16_t x = 0; x < 256; x++){
    analogWrite(fadeLed2, x);
    delay(timedelay2);
  }
  for (uint16_t x = 0; x < 256; x++){
    analogWrite(fadeLed2, 255 - 255);
    delay(timedelay);
  }
  digitalWrite(rgbPins[ledCounter],LOW);
  ledCounter++;
  if (ledCounter >= sizeof(rgbPins)){
    ledCounter = 0;
  }
  
}

It kind of works OK, although the fade in is a bit non-existent.

Basically to give people more of an idea of what Im trying to achieve - I'm trying to imitate a sci-fi film pair of lasers powering up and the firing, one after the other in a repeated loop.

What I'd like is a nice quick but obvious fade in (that will be obvious through very thin (less that 1mm) fibre optic strand), then a sudden off.

I also don't understand what all the RGB Pin code in the code I've borrowed is for...

I think you would have better success if you started with included examples.
Thisvery simple example fade up and down.
File > Examples > 01.Basics > Fade.

In your code they used a RGB-led. 3 leds, red green blue, combined in one. Each color driven by pin 2,3 and 4. If you have identical code repeating for different leds, you can use an array. Arrays are maybe a bit too advanced for you today. Just write your repeating code for your two leds.

You should change

#define timedelay2 5

to

const unsigned long  timedelay2 = 500;

That will add a 1/2 second delay between changing brightness levels, make the value smaller or larger as desired.

Replace this

#define timedelay 3.5

with this

const unsigned long timedelay = 500;

to set the delay after fading up.

Replace this

for (uint16_t x = 0; x < 256; x++){
    analogWrite(fadeLed, 255 - 255);
    delay(timedelay);
  }

with this

analogWrite(fadeLed, 0);
delay(timedelay);

because continually writing a value of zero does not make it any more off.

In general, when you have blocks of code that perform the same function it's better to pull them out to their own function. That way you only need to fix problems in one place instead of mulitple places.

You have two sequences that do the same thing, the only difference is the LED pin number. That value can be passed into a function.

void fadeLED(int pinNum)
{
  for (uint16_t x = 0; x < 256; x++) {
    analogWrite(pinNum, x);
    delay(timedelay2);
  }

  analogWrite(pinNum, 0);

}

const byte versus #define is a non issue; both will work.

However delay() taken an integer so 3.5 is not very useful (3 or 4)

because continually writing a value of zero does not make it any more off.

:smiley: :smiley:

I also don't understand what all the RGB Pin code in the code I've borrowed is for...

See my signature; if that borrowed code contains stuff that you don't understand, borrow pieces that you do understand or leave it and find another example. Gabriel_swe pointed you to another code; if you also don't understand that, don't use it but ask.