Multiple led's random on off

trying to light pins 2-13 on Nano board to random turn on / off led's for model railraod buildings.

need help with sketch , code ?

thx
gk
usa

Hi, @gk99
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

Can you please tell us your electronics, programming, arduino, hardware experience?

A good start is to look at the random statement in the Arduino C++ code.
https://www.arduino.cc/reference/en/language/functions/random-numbers/random/
If you limit the random to go between 2 and 13 inclusively, you can then use the random number to select the output you want to turn ON or OFF.

That would be a good start.

Tom.. :smiley: :+1: :coffee: :australia:

What kind of LEDs ?

Just flashing 11 LEDs, nothing else ?


Read these:

https://www.arduino.cc/reference/en/language/functions/random-numbers/random/

https://www.arduino.cc/reference/en/language/functions/random-numbers/randomseed/

20 ma LED’s – red,blue,white,yelloe.

I’m new to Arduino , can not find any sketches for lighting led’s in random , what I want to do is light led’s one at a time with a delay between each one/ using Arduino

Could use some help on writing a sketch for this …..

Just frustrated

Gk usa

Lights 1 random LED. This may be a starting point. It is for 10 LEDs but you can add more. And the on times are random. You can adjust that, too.

const byte NUM_LEDS = 10;
const byte ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

void setup()
{
   Serial.begin(115200);
   randomSeed(analogRead(A0));
   for (int n = 0; n < NUM_LEDS; n++)
   {
      pinMode(ledPins[n], OUTPUT);
   }
}

void loop()
{
   static unsigned long timer = 0;
   static unsigned long interval = 1000;
   if (millis() - timer >= interval)
   {
      timer = millis();
      allOff();
      byte thisLed = random(0, NUM_LEDS);
      digitalWrite(ledPins[thisLed], HIGH);
      interval =  random(1000, 5001);
   }
}

void allOff()
{
   for (int n = 0; n < NUM_LEDS; n++)
   {
      digitalWrite(ledPins[n], LOW);
   }
}

Multiple LEDs random.

void loop()
{
   static unsigned long timer = 0;
   static unsigned long interval = 1000;
   if (millis() - timer >= interval)
   {
      timer = millis();  
      unsigned int thisLed = random(0, 1 << NUM_LEDS);
      Serial.println(thisLed, HEX);
      for(int n = 0; n < NUM_LEDS; n++)
      {
         digitalWrite(ledPins[n], bitRead(thisLed, n));
      }
      interval =  random(1000, 5001);
   }
}

Hi,
Do you also want to turn them OFF randomly, or just keep turning them on until they are all lit?

Do you want the time between lighting LEDs to be constant and the selected LED to be random?
OR
Do you want to flash one random LED ON and OFF one at a time?

Tom... :smiley: :+1: :coffee: :australia:

Tom

Turn them off randomly and repeat.

Time between off and on randomly.

Can be off and on one at a time or whatever.

Just trying to have building lights light to show that people inside buildings are home.

Thanks for your patience.

I am a master model builder , maybe I can build you something for you help ?

Thx

GK

USA

Hi,
Okay mate, I build in cardboard.

I see what you need.

I did a engineering shop a while ago, with Nano.

The arc welder is random, and the shop has dodgey electrics, the office lights blink when ever the welder arcs up.

Tom.. :smiley: :+1: :coffee: :australia:

3 Likes

Tom

Awesome , I like it …..

Whats the code sketch ?

Thx

GK

USA

tHANKS FOR THE HELP

wORKING GREAT

gk USA

Tom, don’t look directly at the welding arc light !

Very cool model!

Hi,
@gk99 goodto hear, the code is pretty basic in operation, I used delays as it was not important to use "blink without delay" method of creating time intervals.

@groundFungus, I used to model with kits a long time ago, during model railway period etc.
My dad managed a toyshop, I deal for a model mad teenager.
That model is from a downloaded template, in CornFlakes packet cardboard with the brickwork supplied in the template, just print in paper and stick to you favourite medium.

I took it up when Covid started, I have also made space, cars, dinosaurs and stuff for the grandkids.

Tom... :smiley: :+1: :australia: