First Arduino Assignment
After our first Arduino session we were asked to use what we had learnt about loop functions and pin output to make a sequence of LEDs illuminate like a traffic light. In the UK the traffic light sequence is this:
To create this sequence will require 3 LEDs and 4 functions on a loop:
- Red on
- Red & Yellow on
- Green on
- Yellow on
~ Loop
I’m going to start with the ‘Blink’ example program as it has the functions I need, they just need to be edited to work. This is what I created (I’ve added grey text to explain what each line does):
This is how I set up the hardware:
The result:
Making It Harder
I found that pretty easy so I decided to try an make it a bit more complicated. The traffic light sequence I’ve just done is for a road junction but there are lots of others – one I’d like to try is a Pelican crossing.
Pelican crossing
These crossings replace the second stage (red and amber together) with a flashing amber stage that coincides with the green pedestrian light flashing. Drivers are required to give way to pedestrians on the crossing.
This requires me to add pedestrian lights (red and green) and to edit the code to include flashing:
I added the correct LEDs and wiring, lights for cars on the left, pedestrians on the right:
As a Pelican crossing has a stage where the amber for the car flashes with the pedestrian green I required to include a loop sequence within the main loop – otherwise I would of had to do each flash as a line of code over and over. This is done by adding a ‘for loop’ (lines 17~24) as seen here.
Unfortunately I can’t get the code, the ‘for loop’ is being skipped and I do not know why. The hardware is connected correctly and the code verifies. Myself and other students have tried to edit it making small adjustments like adding delays but so far the programs skips from line 15 to 25.
Fortunately what I want to do can also be done by instead of adding a ‘for’loop just add the code lines repeatedly the same number of times I wanted it to loop.
So I removed the ‘for’ loop and added this line of code which makes the amber car light and the green pedestrian light flash:
I then copy and pasted it 8 times to get the LEDs to flash 8 times.
This is the susssful result:
This is the code: