Introduction: Tutorial 2: 2 Flashing Lights, Alternating

This is the 2nd tutorial in the series. In this project, we add 2 lights, and make one off and one on at a time, so that they flash, alternating.

The wiki for the LED can be found here: https://docs.google.com/document/d/16PjgJa6EOTU2Pw7gFAdgUt8tYK8eJsXtQvOHXAlh_6w/edit?usp=sharing.

If you need the LED Library, make a folder called LED in your Arduino Libraries folder, and add these 2 files below to that folder.

Supplies

FROM YOUR KIT:

1 x Arduino with Grove Shield

1 x USB-B (Arduino end) to USB-A (laptop end).

1 x USB-A to USB-C adaptor (if using a MAC with no USB-A port)

2 x Grove LEDs

2 x Grove cables


BYO:

1 x Laptop with Arduino IDE software

Step 1: Build the Circuit

A PDF of the instructions can be found below.

Step 2: Code

The code for this project is below:

//Add Libraries
#include "Led.h"

//Global Variables

//Create Objects
Led redLED(2);
Led greenLED(3);

//Setup
void setup() {
}

//Main Loop
void loop() {
 redLED.on();
 greenLED.off();
 delay(500);

 redLED.off();
 greenLED.on();
 delay(500);
}


Note, the first bit includes the Library so we can use the methods/functions for any object belonging to the class 'Led'.

#include "Led.h"


The next part creates 2 objects, the Red LED, and the Green LED. Both objects are of the class 'Led'. The red LED is attached to PIN 2, while the green LED is attached to PIN 3.

Led redLED(2);
Led greenLED(3);


In the main loop, we simply alternate between one LED being on and the other being off.

 redLED.on();
 greenLED.off();
 delay(500);

 redLED.off();
 greenLED.on();
 delay(500);


Make sure you save it into your Arduino Project folder with an appropriate name (e.g. 'Tutorial 2' or '2 Flashing Lights').

Step 3: Upload

Time up upload the code onto the Arduino and watch it work. If you need to refresh how to upload a program onto the Arduino Board, check out the PDF below.

Step 4: Challenge - Make Traffic Lights

OK, so now here is your challenge. Using all 3 of your LED Socket Kits, and a red, green and orange light... make a traffic light set. The order will need to be green... orange... red. In a sped up version, maybe do 3 seconds green, 1 second orange, 3 seconds red.

If you get really stuck, a solution is provided by going to this Instructable here: https://www.instructables.com/Arduino-Traffic-Lights/