This time I will show the basics of how to connect a push button to the traffic light simulator circuit and how to read it from within an Arduino sketch. If you haven’t read parts 1, 2 and 3 yet, I recommend you do so first as it will make it easier to follow the examples.
For this demonstration, we’ll use a simple push button like this. They have four pins, connected together two by two. Usually the two pins on either side are connected together, and pushing the button will connect them to the two pins on the other side. If you’re in doubt, use a multimeter to check which pins are connected together.
In the previous two parts, I have shown how to build a 2-way traffic light and how to write a sketch to control it.
This time we’ll rewrite the sketch to use state tables.
In microcontroller programming, we are often dealing with a set of well defined states. State tables describe what each state means, rules for transitioning between the different states, for what is allowed and what is expected. Keeping this in a set of tables helps keep the code simple by avoiding a big, tangled mess of if-else statements. This, in turn keeps the code smaller so we can do more with the rather limited memory on the microcontroller.
This is a great little project for learning about Arduino programming as well as LEDs and resistors.
I’ll show how to build a traffic light simulator on a breadboard and write an Arduino sketch to control it. This first part is about building the circuit, while the second part will focus on the sketch.
And: No soldering required.
You will need the following parts. I’ve provided links to product description pages, in case you’re not sure what to get.
Recently, when building a weather station based on Adafruit’s Huzzah ESP8266 breakout board, I needed the ability to send messages that should be retained on the MQTT server.
I am using mosquitto on my home server, acting as a message broker. My weather stations send messages to it and these are then forwarded to the subscribers who have signed up for them. At the moment, these subscribers are a weather logging system, a current weather display panel on the different computers at home, and, occasionally, a command-line client used for troubleshooting.
As the weather stations are battery powered, they will not run always on, always connected, but they wake up every few minutes, check the weather, and transmit the results to the MQTT server before they go to sleep again.
Any subscribers will receive these messages as soon as they are sent. However, when a subscriber starts running and connects to the MQTT server, it will not receive any messages until the relevant weather station sends one. By default, there’s no concept of the most recent values. This is kind of inconvenient for the weather panel application on the computers, as they will not show any values until the next time the weather station wakes up. Depending on the battery levels and configuration, that could be quite a while…