Arduino 002: Multi-Mode Flashing Lights

This will help you:

Use loops and interrupts to control many LEDs which can be changed with button

In this activity, you'll make 5 (or more) LED's flash in different orders to produce cool effects. You'll learn about for loops to reduce the amount of code, and interrupts to change modes when a button is pressed.

Time: 1-3 hours / Level: B3

You should already:

You Will Need:

  • A computer

  • An Arduino

  • An A-to-B USB cable

  • A half-size breadboard

  • Pushbutton switch

  • 10k ohm potentiometer (optional for speed adjustment)

  • 2x 10k Ohm resistors

  • 5-10 LEDs

  • 5-10x 100-220 Ohm resistors (see this guide on resistors for LEDs)

Get the code and resources for this activity by clicking below. It will allow you to download the files from a Google Drive folder. Unzip the folder and save it in a sensible location.

Step 1: Flashing Lights

Assemble the circuit as pictured in circuit_basic.png. You can connect LEDs to pins 3-13, but the example uses 5 LEDs to start. You can use any colored LEDs that you want. Notice that they have a long leg and a short leg - the long leg should be connected to the digital pin, and the short leg should be connected to the ground line (with a wire from GND running to it.) The resistor can either be between the digital pin (power) and the LED, or between the LED and ground. Each resistor should be around 220 Ohms.

[image]

Plug the Arduino into your computer and upload flashing_basic.ino. Read the code to understand what is going on. Then, convert the multiple pin commands to a for loop that iterates through each pin number. The linked reference shows an example under "Example Code", which is similar to what you want but you will be varying the pin number, not the value.

Step 2: Switching Modes

Open the file flashing_button.ino. It uses the button as an interrupt to do something whenever the button is pressed, in the middle of the other code's execution. Read about attachInterrupt() here. Expand the existing structure to create more flashing modes. Make sure to change the 2 in blinkMode = (blinkMode + 1) % 2; to however many modes you have. Once you have a few different modes, try moving them all to functions to clean up your code and prevent errors while editing it. Read about functions here.

Step 3: Varying Speed

Modify the circuit as pictured in circuit_extra.png so that you have a potentiometer with a 10k Ohm resistor connected to the A0 (analog 0) pin. Then, run flashing_speed.ino. See how twisting the potentiometer changes the speed. Read the code and try to understand what is going on.

[image]

Step 4: Finishing Off

Take a video of your circuit in use, showing off all the modes or just your favorite ones. Share it via the submission button below to get it featured on the Circuit Board!

Return any parts to the supply area, and please sort things by the correct value so the next person can find what they need. Thanks!