Ive recently bought my self a arduino Mega 2560 and managed to get a sort of RANDOM function to work with different cases which go of by the number selected. My question is is there a way to also make a PWM sequence in such a case? so that the LED is fully light to dark and then the the next case starts? IS there a more random sequence wich i could then also expand if neccesary?


int ledcolor = 0;
 int a = 500; //this sets how long the stays one color for
int b = 2000;
int brightness = 255;    // how bright the LED is
int fadeAmount = 25;    // how many points to fade the LED by

 void setup() { //this sets the output pins

 pinMode(11, OUTPUT);
 pinMode(10, OUTPUT);
 pinMode(9, OUTPUT);
 pinMode(8, OUTPUT);

 void loop() {
 int ledcolor = random(1,16); //this randomly selects a number between 1 and 15

 switch (ledcolor) {

 case 1: //if ledcolor equals 1 then the led on pin 11 will turn on etc.
 analogWrite(11, brightness);
 brightness = brightness - fadeAmount;     break;
 case 2:
 digitalWrite(10, HIGH);
 digitalWrite(10, LOW);
 case 3:
 digitalWrite(9, HIGH);
 digitalWrite(9, LOW);
  • Research this, but I believe the Arduino library random function returns the same set of (pseudo) random numbers after each power up. If this is a problem you can research if you can seed the Arduino random number generator. However, finding a (always changing) starting seed (with out something like a RTC or Temperature sensor) is difficult in an embedded environment.
    – st2000
    Jun 8 '16 at 14:19
  • What are you actually trying to do? Are you trying to set random brightness on 4 LEDs? Or is the aim more complex?
    – gbulmer
    Jun 8 '16 at 23:26
  • @gbulmer my Goal is to have many different cases wich are going to be set of random with different LED effects for Simulation a firework on my Model Traintrack. and some of those fireworks should also be able to fade from 255 to 0.
    – Mark
    Jun 9 '16 at 11:51

In order to fade in and fade out LEDs you need to use PWM feature. And, specifically you need to use the analogWrite() function call. Also, you need to make sure all your LEDs are connected to PWM port pins. Port pins which support the PWM feature for various Arduinos are listed in the above link.

In your code you might try replacing your 500ms delay with a count up followed by a count down loop. Each loop should contain the appropriate delay and count up or down to the appropriate value.

For instance, you might try a 1ms delay and a count from 0 to 255 followed by a count from 255 to 0. In both loops just before or just after calling the delay() function, place the call to the analogWrite() function and pass the counter value as the PWM duty cycle (LED brightness setting).

added later...

For example, you might try to see if this code works for the 1st case statement:

int brightness_level;

case 1: //if ledcolor equals 1 then the led on pin 11 will turn on etc.
  for(brightness_level = 0 ; brightness_level < 255 ; brightness_level++)
    analogWrite(11, brightness_level);
  for(brightness_level = 255 ; brightness_level >= 0 ; brightness_level--)
    analogWrite(11, brightness_level);
  • thank you for the quick help i now have changed the Code but the led does not fade bevor the next case which is randomised beginns even tho i made the delay time longer. (see my changed Code pls) Mark
    – Mark
    Jun 8 '16 at 15:13
  • Not exactly what I had in mind. In each case segment of code use two for loops. The first should count from 0 to 255. The second should count from 255 to 0. In each loop there should be a 1ms delay and a call to analogWrite passing the pin number and the current loop value. Make sure the pin used for your Arduino supports the PWM feature!
    – st2000
    Jun 8 '16 at 15:20
  • would there be a possibility fopr you to do an example for me im not quite shore what u mean. and it should be only counting down from 255 to 0 and then the next random case Shell start.
    – Mark
    Jun 8 '16 at 18:39
  • I added an exmple to the answer. I haven't tried it so errors might have snuck in. Try it. Make sure pin 11 on your Arduino supports PWM.
    – st2000
    Jun 9 '16 at 6:50
  • thank you. Great now I know what u meant with the for loop.
    – Mark
    Jun 9 '16 at 6:51

To make a completely "non blocking" system you need to change your thinking somewhat. Instead of thinking "A need to fade from X to Y" you instead need to start thinking "I am at brightness X at the moment. I want to get to brightness Y. What do I need to do to get there?".

For instance, you might have a loop() than contains a structure like:

  • My target brightness is Y
  • My current brightness is X
  • Is X equal to Y?
    • Yes:
      • Select a different target brightness to get to
    • No:
      • If my current brightness is below my target brightness then make it brighter.
      • Otherwise make it dimmer.

You can introduce a small delay to slow the fading down, or use millis() to only run that structure at pre-defined intervals (like BlinkWithoutDelay does) to get your desired fade speed.

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.