Blinking LED’s is a staple example of beginner Arduino programming. In this tutorial, we’re going to step it up a notch and use both arrays and the random()
method to determine which LED to turn on and off for a duration.
Materials for Random LED project
- Arduino Uno (or similar controller)
- Breadboard
- 4X LED’s in various colors (We’re using Red, Yellow, Green, and White)
- 4X 220-ohm resistors
- Jumper Wires
- USB Cable to download the program
- Arduino IDE & laptop
Here’s a kit you can use which has all the components.
Amaze your friends with this randomized LED generator
This project is not only fun, but it’s rather straightforward. We will wire the LED to a digital input pin on our Arduino. Then, we’ll send a signal to the pin we want to trigger. This will serve as the “random” LED.
Grab your materials, and let’s wire up the circuit.
Define the process before writing any code
We’re going to start by mapping out the process of how this device should work.
First, we want to determine which LED should turn on. We will use the random()
method from the Arduino Library.
Next, we want to determine which color has been selected. We can use an array of Strings for each color name to identify the selected LED. We can initialize the Serial Monitor and then print out the color once it’s been selected.
Finally, we’ll turn the chosen LED on and off using the same strategy as the Blink example sketch, digitalWrite(...)
and delay(...)
.
For clarity, I’m going to wrap this all up into a separate method and then call the method in loop()
. You’re more than welcome to include the code in loop()
, but I think it looks cleaner writing a separate method.
Translate your process into Arduino code
Now that we have ourselves a roadmap, we’ll open up the Arduino IDE, and translate the process into code.
Start by mapping your LED’s
I always recommend creating global variables for all Inputs and Outputs connected to your Arduino board. Then when you want to use that sensor or device, it’s already defined within your code!
/* Random LED Generator Last Revision: 6/7/18 Written by Learn Robotics www.www.learnrobotics.org -------------------------- Pin Wiring: =========== OUTPUTS - BUILT-IN LED: 13 - RED LED: 12 - YELLOW LED: 11 | PWM CAPABLE - GREEN LED: 10 | PWM CAPABLE - WHITE LED: 9 | PWM CAPABLE */ // OUTPUTS int red_led = 12; int yellow_led = 11; int green_led = 10; int white_led = 9; /* Create an array (size 4) to store color names led_colors[0] = "white" led_color[1] = "green" led_color[2] = "yellow" led_color[3] = "red" */ String led_colors[4] = {"white", "green", "yellow", "red"}; void setup() { // put your setup code here, to run once: //configure outputs pinMode(red_led, OUTPUT); pinMode(yellow_led, OUTPUT); pinMode(green_led, OUTPUT); pinMode(white_led, OUTPUT); Serial.begin(9600); //enable the serial monitor }
Also, call pinMode(...)
for each LED. They’ll be OUTPUTS in this case, since we are sending data from the Arduino to the outside world. Then, initialize the Serial Monitor, by calling
Serial.begin(9600);
We’ll use this to print out the color on the Serial Monitor.
The color of the LED will be stored into an array of Strings.
String led_colors[4] = {"white", "green", "yellow", "red"};
An array stores values in an indexed list. That means, we can call the array variable, led_colors using an index number 0-3. In computer programming, you start counting indexes at 0. Therefore, white is 0, green is 1, yellow is 2, and red is 3. So, if you want to get the value green, you’d reference led_colors[1]
. We’ll expand on this concept to print out the color name once we obtain the randomly generated LED pin.
Select a Random LED and identify the color from the array
The random number we’ll select will be between 9 and 13 because we have LEDs plugged into these pins. Then, we’ll map the selected number 9-13 to the range 0-3, which will form the index of the color_map array. Next, we can print out the color using the mapped index, our led_color array, and the Serial.print(...)
command. Finally, we turn the selected LED pin HIGH (on), delay for 1/2 second, then LOW (off), for 1/2 a second.
This is the code we obtain once we piece it all together.
/* Select a random LED to turn on Print out the name of the color in the Serial Monitor */ void random_led() { // pick a pin number 9-13 because we have LEDs on those pins... int random_led = random(9, 13); // generate our map of the random LED to the map of our array int color_map = map(random_led, 9, 12, 0, 3); Serial.print("Color is... "); // Use the random array number to print out the color Serial.println(led_colors[color_map]); //turn the LED on, then off for 1/2 second digitalWrite(random_led, HIGH); delay(500); digitalWrite(random_led, LOW); delay(500); }
Call the random_led()
method in loop()
. Then upload the code to your Arduino & test it out!
https://www.instagram.com/p/BjS_6qmgVS4/?taken-by=learnrobotics
Snag our Arduino Project eBook!
If you liked this tutorial, you’re going to want to check out our Arduino Projects Workbook. Build over 20+ hours worth of Arduino Prototypes that are built with Arduino and common electronics. You won’t want to miss this!
Have question? Drop it in the comment section!
And don’t forget to tag us in your Random LED Dance Party @learnrobotics on Facebook & Instagram!