in this tutorial I'm going to teach you
how to control a traffic light
for this tutorial you need: an Arduino
the traffic light
and for jumper wires male-to-female
as an alternative you can
also use a breadboard
and four male-to-male jumper-wires
the first step is to connect your jumper
wires with a PCB of our traffic light
there are four labels over here
the are "R", the "Y" the "G" and the "GND"
"R" is red, "Y" is yellow,  "G" is green
 and "GND" is the ground
I've connected the red wire to the "R",
the orange wire to the "Y"
the brown wire to the "G" 
and the black wire to the "GND"
normally when we use LEDs we have to use a resistor
in this tutorial we don't have to
because on the PCB here, these tiny squares, 
are already resistors
they look different than we
used to have in our previous tutorials
this is because by machinery the tiny
resistors can be mounted very easily
where for us, with our hands, it's really
hard to do so
the next step is to connect a traffic light to the Arduino
I've connected the black wire to the ground
marked "GND" on our Arduino
for the lights I have the red to the 8
the orange to the 9 and brown to pin 10
where brown is the green light
make sure you use them in the same order
because it's a lot easier to understand that the
pin numbering is the same as going from
the lights from top to bottom
now that our circuit is ready we can now
have a look at the code
let's have a look at the code
and the code is actually pretty simple
we're not using any external libraries
we're first, on line 20, set pin number 8 as an OUTPUT
and we do the same for 9 and 10
and we also set all the pins to LOW
so we make sure that all the LEDs are off
then inside our loop we control our 3 lights
the red, the green and orange/yellow
so what I do, I write to the red pin HIGH,
 so it goes on
we wait for 5 seconds
and then we turn it off
then, we write to pin number 10 for the green light 
and we wait 5 seconds
and then turn it off
and then the same for the orange/yellow light
you can now check if this code runs for your Arduino
but today I'm going to do something different
I have four steps of
optimizing this code
so while you check if it runs on your Arduino
you can pause this video
and then we're going step by step optimising this code
and after step 4, I'll show you 
how it looks on my Arduino
when we look at the code
it's quite hard to tell 
which pin belongs to which color of LED
so what we're going to do is optimise this a bit
so in step number two
I've defined three constants here on line 18-21
I call them PIN_RED, PIN_YELLOW 
and define the correct pin number
now when we look at line 25-27
we say PIN_RED is an output, PIN_YELLOW is an output and PIN_GREEN is an output
the same we do on line 30-32
here we have the RED to GREEN
and set them all to LOW
this is a lot easier than here 
when you see 8, 9 and 10 in the old in the old code
also in our loop it's a lot more clear what will happen
because here it says PIN_RED is HIGH
instead of pin 8
so this gives a lot more structure to our code
but we can do more 
which I'll show you in step number three
if we look at the code of step two 
we still see here some numbers
so what I did, I define in step number three 
some more constants for the delay
so here: DELAY_RED, DELAY_YELLOW, DELAY_GREEN
and I defined the 5000 for 5 seconds 
and 1000 for 1 second
and also implemented it in the code
as you can see over here
it's now delay, delay, delay
there's also a function call here allOff()
which turns all the LEDs off
this function is writing to pin red, yellow and green
write LOW to them
now you can think: okay
this is one line, and this is three lines
but we only use it once
that's true,
but if you're using the traffic light in your project
there might be a point
where you want to turn off all the LEDs
for example in Germany, when it's red
and you go to green
then first you have the step
green AND yellow together
then you can use the allOff() to turn all the lights off instead of having to do it all manually
there is another step we can take
and I want you to look at the code, and see if you can figure out what we can do even more
and I'll show you in step four what we can do
so I went one step further and maybe
you thought about this
but I want to remove all the repetitive code
so what I did, I simplified the loop function
now you see here toggleLED() 
with two parameters
PIN_RED and DELAY_RED
if we go back to step three you see a lot of
the same here
this is the same as this
this is the same as this on line 44-46
the only difference is the pin number and the delay
so if you see this pattern of a repetitive code 
but just slightly different configuration
what you can do is create a function
and that's what I did
so here is my function
I defined two parameters: led_pin and led_delay
and I took basically the same code 
as in step number three
So I took this code
but instead of having the PIN_RED and DELAY_RED
I said led_pin
which is part of the function parameter
and led_delay which is the other 
function parameter
so now a code is a lot cleaner
because you just say toggleLED() on PIN_RED
with a delay for red, green and yellow
the nice thing is if you show
the code to someone else
he or she can immediately understand 
that toggleLED() will toggle a LED
and with pin and delay one can also kind of guess 
what it does
there's one other step you can do
but I skip that for now 
that is that here you set the pin mode
for all the pins to output
you can even simplify that 
by creating a function
which sets the pin modes 
just like the allOff()
but that might be a little bit overdone 
since this is inside a setup()
and it's a lot easy to see in the setup what's going on then when I put here a function
and have to scroll down
so now it's time for me to upload the code to the Arduino
and I'm going to show you what it looks like
I've uploaded the code
and as you can see
a traffic light is cycled through the process
from red to green...
and then to yellow, to red
this is exactly the way that we've programmed it 
so our sketch is working
now you know how to control your traffic lights
if you have any questions
leave them in the comments below
and if you want to download the course material
you'll find a link in the description of this video
I see you in the next episode!
you
