LED Adusting Time and Brightness

I have found many videos on fading or blinking an LED. what I’m trying to do is setup an LED to start at a low brightness, fade to bright over a set time (2000ms+/-), then fade to a low value, over the same time as above, so it just glows , then hold that value (brightness) for a set time (5000ms +/-), then repeat.
I’m not having any luck getting the LED to hold the value for 5000ms, the last {delay(12)} seams to affect the timing on the fade not what happens after the fade from bright to dim.
The timing of the LED fade up and down in the skit below is close enough for my needs.

It would be great if there was a piece of code where you could just set the values for - time, fade time, brightness, etc., for fade or flashing a LED.

Code-----

Fade Up/Dn control

This example shows how to fade an LED on pin 9 using the analogWrite()
function.

The analogWrite() function uses PWM, so if you want to change the pin you’re
using, be sure to use another PWM capable pin.

The goal is to have the LED fade from dim to bright over a period of 2 seconds (2000ms)+/-,
then fade from bright to dim over a period of 2 seconds +/-.
The LED will then hold its dim value (it will never have a false value) for 5 seconds +/-,
then repeat.

*/

int ledPin = 9; // the PWM pin the LED is attached to
int brightness = 2; // how bright the LED is
int fadeAmount = 1; // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(ledPin, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(ledPin, brightness);

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:
if (brightness <= 2 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 12 milliseconds to see the dimming effect
{delay(12);

}

}

  if (brightness <= 2 || brightness >= 255) {
    fadeAmount = -fadeAmount;
    if (brightness <= 2) delay (5000);
  }

As long as you fadeamount is either 1 or -1 this is fine.

I added your code after the "delay" and changed the int fadeAmount = 2 to 1.
all it did was fade the LED from dim to bright in a loop, no fade down. maybe I placed your code in the wrong spot.
Thanks for the response

After taking another look and modifying the code per your suggestion it worked

thanks

const byte LEDPin = 9;
const byte LowBrightness = 20;
const byte Bright = 255;

void setup()
{
}

void loop()
{
  // what I'm trying to do is setup an LED to start at a low brightness,
  // fade to bright over a set time (2000ms+/-),
  for (int brightness = LowBrightness; brightness < Bright; brightness++)
  {
    analogWrite(LEDPin, brightness);
    delay(2000 / (Bright - LowBrightness));
  }

  // then fade to a low value, over the same time as above, so it just glows ,
  for (int brightness = Bright; brightness >= LowBrightness; brightness--)
  {
    analogWrite(LEDPin, brightness);
    delay(2000 / (Bright - LowBrightness));
  }

  // then hold that value (brightness) for a set time (5000ms +/-),
  delay(5000);

  //then repeat.
}
1 Like