I'm trying to made the LED's go from minimum brightness to maximum brightness and back to minimum in 2.55 seconds.

I really don't know where to begin. I have the following basic code for making it blink simultaneously, but how do I make it cycle through during the time I want:

int led = 10;           //the PWM (tilde) pin the LED is attached to

int led2 = 9;

int led3 = 11;

int brightness = 0;    //how bright the LED is

int delaytime = 25;

int fadeAmount = 5;    //how many points to fade the LED by

//the setup routine runs once you press the reset:

void setup() {

  // declare pins to be an output:

  pinMode (led, OUTPUT);

  pinMode (led2, OUTPUT);

  pinMode (led3, OUTPUT);


//the loop routine runs over and over again forever:

void loop() {

  // set the brightness of pin 10:

  analogWrite (led, brightness);

  analogWrite (led2, brightness);

  analogWrite (led3, 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 <= 0 || brightness >= 255) {

    fadeAmount = -fadeAmount;


//wait for set milliseconds to see the dimming effect

delay (delaytime);


**bonus: the next part includes making each LED go on its own schedule so the more thoroughly you can explain this first the better I can attempt the next part.


  • You can set the delay and fadeAmount for getting through in 2.55s. And what do you mean by "on its own schedule"?
    – chrisl
    Jun 30 '19 at 20:13
  • So I sort of get the initial delaytime and fadeAmount values based on this reasoning: You set the fade amount and delay to specific values or at least keep the same ratio FadeAmount/510 = delayTime/TotalTime 5/510=25/2550 510 being the total brightness change from low to high to low again.
    – leojames
    Jun 30 '19 at 20:22
  • But I need my first LED to go from min to max to min brightness in 2.55 seconds, my second to go in 5. 1 sec, and my third in 10.2. How would I be able to set each of them on their own schedule, Like make LED each have its own fadeAmount if I kept the delaytime constant at 25? Does that make sense?
    – leojames
    Jun 30 '19 at 20:25
  • 1
    is this a school assignment? ... the word "bonus" says that it is one
    – jsotola
    Jun 30 '19 at 21:43
  • 1
    It is a school assignment, though I don't get bonus points for the second part...I just have to do it. Thankfully ya'll explained it well enough for me to do it on my own. thanks!
    – leojames
    Jun 30 '19 at 22:30

The way to go is the non-blocking coding style, that can be found in the BlinkWithoutDelay example, that comes with the Arduino IDE. There you use millis() to measure time. You save a timestamp for every LED and in every execution of the void loop() you check, if enough time has passed since the last timestamp. You have to do this for every LED.

Since this coding style has been explained numerous times through the internet and on this site, I will not go more into detail. You can google the rest, understand, how that style works and then transfer that to your case.


Have a look at these instructables...

One does the gradual fading of an led (on and off) via PWM: LED lighting in a staircase.

The second deals with the "there own schedule" bit - which I assume means cycle on and off independently of one another.: cooperative multitasking.

Disclaimer.. they are my instructables posts, but they do address the question. There are also several libraries available for running "timed tasks" if you prefer an alternative solution.

Here is an practical example that should deal with both aspects of your question (hopefully it will stem the tide of negative feedback)...

It should work on an UNO or any other Arduino with PWM on pins 3, 5 and 6. Of course you are free to adjust the pins to suit your needs (including adding more or taking some out).

int pwmPin[] = {3, 5, 6 };        // Pins that are being used for PWM.
int pwmValue [] = { 0, 0, 0};     // Initial value of PWM duty cycle in the range 0 to 255.
int pwmIncrement[] = {1, 1, 1};   // The value by which the PWM will be incremented at each step.
                                  // NB, this can not be zero but you can start with negative numbers if you wish.
unsigned int pwmDelay[] = { 20, 30, 70 }; // The delay between steps for each PWM.

// To get a good effect select good initial values for the delay, value and increment.
// larger delays mean less frequent changes to the PWM duty cycle.
// larger increments means each adjustment to the PWM duty cycle is larger.
// Set the value at the starting point e.g. 0, for off, 127 for 50% brightness or 255 for 100%.

#define PINS sizeof(pwmValue)
unsigned long pwmNextStepTime[PINS]; // The point in time when the PWM will next be adjusted.

void setup() {
  for (int i = 0; i < PINS; i++) {   // Initialise each pin as:
    pinMode(pwmPin[i], OUTPUT);      // an output.
    pwmNextStepTime[i] = millis() + pwmDelay[i];   // The time when next the duty cycle shoud be adusted.
    analogWrite(pwmPin[i], pwmValue[i]);           // Set the duty cycle of the PWM.

void loop() {
  // put your main code here, to run repeatedly:
  for (int i = 0; i < PINS; i++) {    // On each loop, check the PWM pin to see if we need to do anything.
    unsigned long now = millis();     // What is the current "time"?
    if (now > pwmNextStepTime[i]) {   // Has the time passed the adjustment time for PWM pin i?
      pwmNextStepTime[i] = now + pwmDelay[i];  // Yes, calculate the next adjustment time.
      pwmValue [i] += pwmIncrement[i];         // Adjust the duty cycle as per the pin's increment.
      if (pwmValue [i] >= 255) {               // Have we gone too far up?
        pwmValue[i] = 255;                     // Yes, set the maximum value.
        pwmIncrement[i] = -pwmIncrement[i];    // Reverse the increment (so now we count down)
      } else if (pwmValue[i] <= 0) {           // Have we gone too far down?
        pwmValue[i] = 0;                       // Yes, set the minimum value.
        pwmIncrement[i] = -pwmIncrement[i];    // Reverse the increment (so now we count up)
      analogWrite(pwmPin[i], pwmValue[i]);     // Finally, set the duty cycle for the LED.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.