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.

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:

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:

I am building HO Scale coal dumper with a linear threaded rod that I am turning with a 6vdc N20 motor. It will position a coal car to the dumper by pushing it on the linear track with a servo arm . The servo SG90 will move arm 90 degrees to go between cars to position them for the next coal dumping movement. How can I have the servo go 90 degrees with a push button that I can push 1 time for the arm on servo to go down and then push another button for servo arm to come up ?
I have a mega 2560 and Nano Boards.
THX
GK

I left an answer in your other post for exactly the same question. Posting the same question in multiple places is "cross posting". Cross posting is discouraged because it wastes members time if the question gets the same answers from multiple members. Do not cross post.

Hi all...I know I am a little late to the party but I just got introduced to Arduino as a light controller for my model railroad in December and am hoping you can assist with adding random on/off lights in my buildings.

Let me set the stage, I currently have a 6 story building that is on fire, I am using a Nano, the PWM pins 3, 5, 6, 9, 10, 11 to create the fire affect using yellow and red LEDs. This works great and I have this first part dialed in. My issue is that I still have 6 digital pins (2, 4, 7, 8, 12, 13) available that I would like to use to randomize room lights in the fire station building that sits near the burning building. Currently in the sketch below I have them set to always on which works fine.

// LED Fire Effect
int ledFire1 = 3; //yellow led
int ledFire2 = 5; //red led
int ledFire3 = 6; //yellow led
int ledFire4 = 9; //yellow led
int ledFire5 = 10; //red led
int ledFire6 = 11; yellow led

// Fire Station Lights - static red sconces and staic rooms lights
int ledStationR1 = 2; //red led - static on
int ledStationR2 = 7; //red led - static on
int ledStationW1 = 4; //white led - room1
int ledStationW2 = 8; //white led - room2
int ledStationW3 = 12; //white led - room3
int ledStationW4 = 13; //white led - room4


void setup()
{

pinMode(ledFire1, OUTPUT);
pinMode(ledFire2, OUTPUT);
pinMode(ledFire3, OUTPUT);
pinMode(ledFire4, OUTPUT);
pinMode(ledFire5, OUTPUT);
pinMode(ledFire6, OUTPUT);
pinMode(ledStationR1, OUTPUT);
pinMode(ledStationR2, OUTPUT);
pinMode(ledStationW1, OUTPUT);
pinMode(ledStationW2, OUTPUT);
pinMode(ledStationW3, OUTPUT);
pinMode(ledStationW4, OUTPUT);
digitalWrite(ledStationR1, (HIGH));
digitalWrite(ledStationR2, (HIGH));
digitalWrite(ledStationW1, (HIGH));
digitalWrite(ledStationW2, (HIGH));
digitalWrite(ledStationW3, (HIGH));
digitalWrite(ledStationW4, (HIGH));

}

void loop() 
{

analogWrite(ledFire1, random(120)+135);
analogWrite(ledFire2, random(120)+135);
analogWrite(ledFire3, random(120)+135);
analogWrite(ledFire4, random(120)+135);
analogWrite(ledFire5, random(120)+135);
analogWrite(ledFire6, random(120)+135);
delay(random(150));

}



I have found the below code and am happy with the randomization of the 4 lights when run by itself.

#define numleds  4                                                     

byte ledStation [ ] =  {4,8,12,13} ;

void setup( )  {                   

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

    pinMode ( ledStation [ i ], OUTPUT) ;

    digitalWrite ( ledStation [ i ] , HIGH) ;

   }

}

void loop ( )  {   

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

                 delay ( 5000 ) ;                   

}

boolean lightsw ( )  {

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

   else return HIGH ;

}

The issue that I am running into is that since both sketches are based on delay(), the station lights never run since the ledFire() delay is so short that the ledStation() never gets called to run it's sequence. Below is my attempt at combining the 2 sketches. Does anyone have any suggestions on how to make the window randomization AND fire animation run on the same unit?

// LED Fire Effect
int ledFire1 = 3;
int ledFire2 = 5;
int ledFire3 = 6;
int ledFire4 = 9;
int ledFire5 = 10;
int ledFire6 = 11;

// Red Static Fire Station Lights
int ledStationR1 = 2;
int ledStationR2 = 7;

// Fire Station Random Window Lights
#define numleds 4
byte ledStationLights [ ] = {4,8,12,13};



void setup()
{
for (int i=0; i <=numleds; i++) {
  pinMode(ledStationLights [i], OUTPUT);
  digitalWrite(ledStationLights [i], HIGH);
}
pinMode(ledFire1, OUTPUT);
pinMode(ledFire2, OUTPUT);
pinMode(ledFire3, OUTPUT);
pinMode(ledFire4, OUTPUT);
pinMode(ledFire5, OUTPUT);
pinMode(ledFire6, OUTPUT);
pinMode(ledStationR1, OUTPUT);
pinMode(ledStationR2, OUTPUT);
digitalWrite(ledStationR1, (HIGH));
digitalWrite(ledStationR2, (HIGH));
ledFire();

}

void loop() 
{
  
  }

int ledFire() //creates a fire effect
{
  
  analogWrite(ledFire1, random(120)+135);
  analogWrite(ledFire2, random(120)+135);
  analogWrite(ledFire3, random(120)+135);
  analogWrite(ledFire4, random(120)+135);
  analogWrite(ledFire5, random(120)+135);
  analogWrite(ledFire6, random(120)+135);
  delay(random(150));
  ledFire();
  
}

int ledWindowLights() //creates random window lights effect
{
  digitalWrite(ledStationLights [random (0, numleds+1)], lightsw());
  delay (5000);
  ledWindowLights();
  
}
boolean lightsw() 
{
  if (random (0,100) > 40) return LOW;
  else return HIGH;  
}


Any help or suggestions are greatly appreciated.

Thanks,
Bryan

Learn to do without, blocking, delay().

Non-blocking timing tutorials:
Blink without delay().
Beginner's guide to millis().
Several things at a time.

thx alot -gk

some time ago I bundled several LED effects into one library [url]https://werner.rothschopf.net/microcontroller/202202_tools_led_en.htm[/url].

All are non blocking based on millis() and can be combined to run "near" parallel.

A sketch for you might look something like following:

/*
  Flicker LED and random LED (konventional)

  https://forum.arduino.cc/t/multiple-leds-random-on-off/884967/17

  by noiasca
*/

#include <Noiasca_led.h>               // download library from http://werner.rothschopf.net/microcontroller/202202_tools_led_en.htm

FlickerPin flickerLed[] {3, 5, 6, 9, 10, 11};     // UNO PWM pins 3, 5, 6, 9, 10, 11
constexpr byte stationLed[] {2, 7};

void setup() {
  for (auto  &i : flickerLed) i.begin();  // you have to call the .begin() method for the flicker LED
  for (auto  &i : stationLed) pinMode(i, OUTPUT);
}

void loop() {
  for(auto  &i : flickerLed) i.update();   // you have to call update() for the flicker LED
  for (auto  &i : stationLed)
  {
    if (random (10000) == 42) // once in n loops change state of LED
    {
      if (digitalRead(i) == LOW) digitalWrite(i, HIGH); else digitalWrite(i, LOW);
    }
  }
}