Random on/of Lighting for Model Building

Long time listener, first-time caller here!

I have a model building with several windows and want the various LEDs I have put inside to randomly switch on and off with minutes/hours between them.

Hardware on my nano is sorted, but I'm struggling with the code.

Any help would be really appreciated, I'm sat here with my model in darkness, waiting for life!!

Paul

So.....

Your code would be heavily dependent on how you have your LEDs hooked up to the arduino. Does it go through an I2C, a Serial bus, Each LED driven or controlled by a separate Pin. Lots of factors that can't be guessed. Do you have a schematic handy? Please don't take this as a discouraging post, but the examples menu has a lot of good material to look over like Blink without delay. This would allow you to turn on and off your LEDs without having the microcontroller sleep causing it to miss careful time events.

Best of Luck!

  • M

Try to get one of your LEDs to blink (best use "blink without delay").

That will allow you to have all your other LEDs to blink independently as well.

Then when you have a blinky house, you can start playing around with the delays - making them longer, adding random factors, etc.

Indeed without details on how they're attached not much more concrete we can say.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Show us what you have in code and we can advise you on getting it to perform the way you want.

This might help.

Tom... :slight_smile:

Have a look at the demo Several Things at a Time. It has 3 LEDs and that could easily be extended.

The intervals could also be updated with random values.

...R

Thanks for the awesome replies guys. I'm away from home right now but will report back with pics & code when I can.

Basically, my led's are on the pwm pins of my nano. Just struggling with getting my head around the code.

Really appreciate the advice & warm welcome though. :slight_smile:

TomGeorge:
...This might help.

https://www.youtube.com/watch?v=EAaio74GsVQ

Tom... :slight_smile:

Tom, that video is great!!! Did some digging and found the code in model railway hobbyist. The problem was the code was published in the magazine wrong, after more digging I have managed to find the correct code written in the comments section by the great man Geoff Bunza himself:

#define numleds  16                                                       

byte ledpins [ ] =  { 0,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 } ;

void setup( )  {                   

    for ( int  i=1; i <= numleds;  i++ )  {      

    pinMode ( ledpins [ i ], OUTPUT) ;

    digitalWrite ( ledpins [ i ] , HIGH) ;

   }

}

void loop ( )  {   

                digitalWrite ( ledpins [ random ( 0, numleds+1 ) ], lightsw ( ) ) ;

                 delay ( 900 ) ;                   

}

boolean lightsw ( )  {

  if  ( random (0,100) > 40 ) return LOW ;

   else return HIGH ;

}

Everything works great! Was like magic when I hit upload to my nano. : )

So, just one last question if I may, the code I've used above I would like just one of the LED's (pwm pin 11) to fade. I've looked at the fade sketch and tried working it in but I'm struggling to work out how to bolt that on.

Any help would be really appreciated
Paul

What do you want the led on pin 11 to do?

Fade in & out continuously?

Fade when picked at random and stop when picked again?

Fade when picked at random fading to fully on when off & vice versa?

Even just Fade in & out continuously would be great. :slight_smile:

I just can't work out how to incorporate it.

Many thanks
Paul

Very happy with my random light code:

#define numleds  16                                                       

byte ledpins [ ] =  { 0,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 } ;

void setup( )  {                   

    for ( int  i=1; i <= numleds;  i++ )  {      

    pinMode ( ledpins [ i ], OUTPUT) ;

    digitalWrite ( ledpins [ i ] , HIGH) ;

   }

}

void loop ( )  {   

                digitalWrite ( ledpins [ random ( 0, numleds+1 ) ], lightsw ( ) ) ;

                 delay ( 900 ) ;                   

}

boolean lightsw ( )  {

  if  ( random (0,100) > 40 ) return LOW ;

   else return HIGH ;

}

Spent the last few hours desperately trying to add this code in to make pin 11 do a flicker:

int ledPin1 = 11;


void setup()
{
pinMode(ledPin1, OUTPUT);


}

void loop() {
analogWrite(ledPin1, random(220)+135);

delay(random(100));
}

Can these two things be even combined? I'm struggling, am I beating up the wrong bush? Just want one of my leds (pin 11) to do something different to the rest...

Any help would be really appreciated
Paul

try this:

#define numleds  16
const byte Fadepin = 11;
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

byte ledpins [ ] =  { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 } ;

void setup( )
{
  for ( int  i = 1; i <= numleds;  i++ )
  {
    pinMode ( ledpins [ i ], OUTPUT) ;
    digitalWrite ( ledpins [ i ] , HIGH) ;
  }
}

void loop ( )
{
  byte pin;
  pin = random ( 0, numleds + 1 );
  if (pin != Fadepin)
    digitalWrite ( ledpins [ pin ], lightsw ( ) ) ;
  Fade(Fadepin);
  delay (900) ;
}

boolean lightsw ( )
{
  if  ( random (0, 100) > 40 )
    return LOW ;
  else
    return HIGH ;
}

void Fade(byte pin)
{
  analogWrite(pin, 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;
  }
}

Thanks, WildBill, will give this a go. Apologies for the slow reply I've been ill.

Much appreciated
Paul

Arrrgh:
Thanks, WildBill, will give this a go. Apologies for the slow reply I've been ill.

Much appreciated
Paul

Hope you are better mate.
Belated Happy New Year.
Tom.. :slight_smile:

Thanks Tom and happy new year to you too! : )