0

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?

Mark.

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);
 delay(a);
 digitalWrite(10, LOW);
 break;
 case 3:
 digitalWrite(9, HIGH);
 delay(a);
 digitalWrite(9, LOW);
 break;
asked Jun 8, 2016 at 13:45
3
  • 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. Commented Jun 8, 2016 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? Commented Jun 8, 2016 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. Commented Jun 9, 2016 at 11:51

2 Answers 2

1

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);
 delay(1);
 }
 for(brightness_level = 255 ; brightness_level >= 0 ; brightness_level--)
 {
 analogWrite(11, brightness_level);
 delay(1);
 }
break;
answered Jun 8, 2016 at 14:13
7
  • 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 Commented Jun 8, 2016 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! Commented Jun 8, 2016 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. Commented Jun 8, 2016 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. Commented Jun 9, 2016 at 6:50
  • thank you. Great now I know what u meant with the for loop. Commented Jun 9, 2016 at 6:51
0

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.

answered Jun 9, 2016 at 11:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.