Model Scenery Tutorials

Comprenshive Tips. Hints And Techniques For Budget Minded Diorama Maker, Model Train Enthusiast And Wargammer

The Arduino code and documentation for randomly blinking LEDs.

This shows you how to blink LEds two ways.  One method uses arrays and the other a string.

randomly blink leds

 

Click on the image to watch a video of the different ways to blink the LEDs.

The number of LEDs that you can blink depends on the number of pins on your board.  For an Arduino Nano or Uno you can use pins 2 – 13.  For a Mega you have, 52 to use.  If you use the method that defines the LEDs in an array, you can increase the numbers by using an array.

You can also expand the capability of a Nano or Uno by using shift registers.  Read about doing that here. Blink LEDs with shift registers

Coding Methods

We provide information about two basic coding methods.

The first method uses the typical random code that in a form similar to  this random(5, 11); To have the sketch work the highest pin number entry must be one higher than the last pin that operates randomly. So with this example, pin 10 controls the last led in the string. And pin 11 CAN’T be connected to anything.

The second method uses an array that is in a form similar to this int leds[6] = {5, 6, 7, 8, 9, 10}; The lights operate just as they do above, however you do not have to leave pin11 unconnected.  So it gives you more room on your board.

Materials Required

Light emitting diodesLEDs

Use anything from SMD LEDs to 5 mm.

 

 

arduino nanoArduino Nano

Can use any of the Arduino boards.  We choose to use the Nano because it is cheap.

 

 

prototype board

Prototype Board

Use perf board, as shown here, or Vero strip board.  We prefer Vero board.

 

Resistors

The number needed equates to the number of LEDs that you use.  Calculate the resistance value using the data supplied with your LEDs and an online LED Ohms Law calculator

 

 

The parts make a standard multi-functional LED control board.  You can get for such a control board in a discounted package at PoandsBest Electronics.  You can get it with and without LEDs.

The Codes

Blink With An Array

For the sake of illustration  in addition to the blinking LEDs, I have included in the code one LED that will stay on 24/7

Note that when you make the array, you list only those pins that you wish to blink.  So, for example, your array can look like (2, 4, 7,  8, 12, 13) .

CODE:

/*Blink LED array This sketch is efficient in the you do not have to leave pin 11 unconnected as you would normally if you used code random(5,11)
This sketch created and copyright by Gary C. Granai https://www.facebook.com/gary.granai and is included in the Arduino For Model Railing Library at https://steamtraininfo.com/arduino-projects

You are free to use this sketch and amend it for your own personal use as long as these credits remain intact. Using it in any manner or form for commercial purposes is prohibited.*/

int leds[6] = {5, 6, 7, 8, 9, 10}; //the lights on these pins will blink
#define on 11 //this light will stay on 24/7

void setup() {
for (int i; i < sizeof(leds) / sizeof(int); i++) {pinMode(leds[i], OUTPUT);  //sets the array pins as outputs

pinMode(on, OUTPUT);
digitalWrite(on, HIGH); //turns the light on to remain on 24/7
delay(10);
}

void loop() {
digitalWrite(leds[random(0, sizeof(leds) / sizeof(int))], HIGH); //lightd on
delay(random(30, 150));
digitalWrite(leds[random(0, sizeof(leds) / sizeof(int))], LOW); //lights off.

}

END CODE

Random Blink

This is the traditional popular random blink code. It has one disadvantage when compared to the code above in that you must leave the pin immediately after the LED string empty. If you connect it to anything the random script will not work.

CODE

/*radom delay random blink 8 leds
This sketch created and copyright by Gary C. Granai https://www.facebook.com/gary.granai and is included in the
Arduino For Model Railing Library at https://steamtraininfo.com/arduino-projects
You are free to use this sketch and amend it for your own personal use as long as these credits remain intact.
Using it in any manner or form for commercial purposes is prohibited.*/

int randNumber;
void setup() {
/* This sets 8 leds on pins D2 to D9 to output */
for (int i = 2; i <= 10; i++) {
pinMode(i, OUTPUT); // sets pins to output
}
randomSeed(analogRead(A0)); /*sets the pin to create “static so the the initial LED to light is different
each time through the loop */
}

//***************************************************
void loop() {
BlinkRandomly();
}

//**************************************************
void BlinkRandomly() {
randNumber= random(25, 200);
int LightLED = random(2, 10); //randomly selects LED to light. Must use +1 over the last pin number
//(ie: 9 + 1 = 10)
int dlay = randNumber; // sets the blink rate in milliseconds
digitalWrite(LightLED, HIGH);
delay(dlay);
digitalWrite(LightLED, LOW);
delay(dlay);
}

END CODE

Woki Demonstration

The are from the Wokwi code simulator.  They make demonstrations quite clear.

 

Blinking 8 LEDs randomly

 

Blinking 8 LEDs randomly using an array.

 

 

Live Demonstration

This video shows LEDs blinking on a control board.

randomly blink ledsClick on the image to watch a video of the different ways to blink the LEDs.

 

 

 

These projects are of the many that you can find at Arduino For Model Railroaders sketchbook.

 

Questions, Comments, Help

If you have questions, comments or need help, contact us.

Materials

And once again, you can get the necessary parts at PolandsBest Electronics.

Scroll to top