Reader small image

You're reading from  Creative DIY Microcontroller Projects with TinyGo and WebAssembly

Product typeBook
Published inMay 2021
PublisherPackt
ISBN-139781800560208
Edition1st Edition
Tools
Right arrow
Author (1)
Tobias Theel
Tobias Theel
author image
Tobias Theel

Tobias Theel works as the Technical Lead and DevOps for a German FinTech startup fino and since 2020 he has also started working for RegTech startup, ClariLab, as Lead Software Engineer. Being a software architect and an expert for Go and TinyGo alongside C# and Java, he is also iSAQB certified. Theel is a highly enthusiastic community contributor and is among the top 10% responders in C# and Unity3D as well as top 20% responders in .NET, Go, and Visual Studio on StackOverflow. When not programming for fino or ClariLab, he can be found developing games, mainly at game jams such as the Ludum Dare Jam, where he develops games from scratch within 72 hours. As an active speaker at tech talks and a participant for numerous hackathons, Theel loves to share his knowledge of software development with fellow enthusiasts.
Read more about Tobias Theel

Right arrow

Building traffic lights

We know how to light up a single LED, and we also know how to light up an LED using a button input. The next step is to build a circuit using three LEDs and to write the code to light them up in the correct order.

Building the circuit

To build the circuit, we need the following components:

  • Three LEDs (preferably red, yellow, and green)
  • Three 220 Ohm resistors
  • Seven jumper wires

We start by first setting up the components using the following steps:

  1. Connect GND from the Arduino to any ground port on the power bus.
  2. Place the first (red) LED with the cathode in G12 and the anode in G13.
  3. Place the second (yellow) LED with the cathode in G15 and the anode in G16.
  4. Place the third (green) LED with the cathode in G18 and the anode in G19.
  5. Connect F13 with D13 using a 220 Ohm resistor.
  6. Connect F16 with D16 using a 220 Ohm resistor.
  7. Connect F19 with D19 using a 220 Ohm resistor.
  8. Connect F13 to Ground on the power bus using a jumper wire.
  9. Connect F16 to Ground on the power bus using a jumper wire.
  10. Connect F19 to Ground on the power bus using a jumper wire.
  11. Connect port D13 to A12 using a jumper wire.
  12. Connect port D16 to A12 using a jumper wire.
  13. Connect port D19 to A12 using a jumper wire.

Your circuit should now look similar to the following figure:

Figure 2.6 – The traffic lights circuit – image taken from Fritzing

Figure 2.6 – The traffic lights circuit – image taken from Fritzing

We have now successfully set up the circuit. Now we can continue to write some code to control the LEDs.

Creating a folder structure

We start off by creating a new folder named traffic-lights-simple inside the Chapter02 folder. Also, we create a main.go file inside the new folder and start off with an empty main function. Your project structure should now look like this:

Figure 2.7 - Folder structure for the circuit

Figure 2.7 - Folder structure for the circuit

Writing the logic

We have successfully set up our project structure to continue. We are going to implement the following flow:

RED -> RED-YELLOW -> GREEN -> YELLOW -> RED

This is a typical flow for traffic lights with three bulbs.

We are going to configure three pins as output, and afterward, we want to endlessly loop and light up the LEDs in this flow.

Inside the main function, we write the following:

  1. Initialize a new variable named outputConfig as PinConfig using the PinOutPut mode:
    outputConfig := machine.PinConfig{Mode: machine.
                    PinOutput}
  2. Initialize a new variable named redLED with the value machine.D13 and configure it as output:
    redLED := machine.D13
    redLED.Configure(outputConfig)
  3. Initialize a new variable named yellowLED with the value machine.D12 and configure it as output:
    yellowLED := machine.D12
    yellowLED.Configure(outputConfig)
  4. Initialize a new variable named greenLED with the value machine.D11 and configure it as output:
    greenLED := machine.D11
    greenLED.Configure(outputConfig)

We have now initialized our variables to act as output pins. The next step is to light up the LEDs in the correct order. We basically have four phases, which just need to repeat in order to simulate a real traffic light. Let's go through these one by one:

  1. We are going to handle the phases in an endless loop:
    for {
  2. For RED-Phase, turn on the red LED and wait for a second:
        redLED.High()
        time.Sleep(time.Second)
  3. For RED-YELLOW-Phase, turn on the yellow LED and wait for a second:
        yellowLED.High()
        time.Sleep(time.Second)
  4. For GREEN-PHASE, turn off the yellow and red LEDs and turn on the green LED and wait for a second:
        redLED.Low()
        yellowLED.Low()
        greenLED.High()
        time.Sleep(time.Second)
  5. For YELLOW-Phase, turn off the green LED and turn on the yellow LED, then wait for a second and turn off yellow again, so we can start cleanly with RED-Phase again:
        greenLED.Low()
        yellowLED.High()
        time.Sleep(time.Second)
        yellowLED.Low()
    }

The complete content of the function is available at the following URL:

https://github.com/PacktPublishing/Programming-Microcontrollers-and-WebAssembly-with-TinyGo/blob/master/Chapter02/traffic-lights-simple/main.go

Note

Don't forget to import the time and machine packages.

We have now assembled and programmed a complete traffic lights flow. The next step is to combine everything we have built to complete our project.

Previous PageNext Page
You have been reading a chapter from
Creative DIY Microcontroller Projects with TinyGo and WebAssembly
Published in: May 2021Publisher: PacktISBN-13: 9781800560208
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Tobias Theel

Tobias Theel works as the Technical Lead and DevOps for a German FinTech startup fino and since 2020 he has also started working for RegTech startup, ClariLab, as Lead Software Engineer. Being a software architect and an expert for Go and TinyGo alongside C# and Java, he is also iSAQB certified. Theel is a highly enthusiastic community contributor and is among the top 10% responders in C# and Unity3D as well as top 20% responders in .NET, Go, and Visual Studio on StackOverflow. When not programming for fino or ClariLab, he can be found developing games, mainly at game jams such as the Ludum Dare Jam, where he develops games from scratch within 72 hours. As an active speaker at tech talks and a participant for numerous hackathons, Theel loves to share his knowledge of software development with fellow enthusiasts.
Read more about Tobias Theel