Build Your Own Escape Game Artefacts! Part 3

Have you ever wanted to build your own escape game artefacts using low voltage electronics? Look no further! In the upcoming months, look out for a short series of articles on how you can approach creating small, but effective artefacts for your own game designs.
Previously…
In part 2, I spoke about the Arduino Uno microcontroller and getting to grips with the IDE. Part 3 focusses upon evolving our psuedocode into real code.
You Will Need
Fundamental Equipment
1x Arduino Uno (or open-source copy)
1x Arduino Uno USB connector
A laptop or desktop computer
A download of the Arduino IDE
Specific Equipment
1x TM1637 4-digit, 7-segment display timer – (Look here for examples)
So Far…
- We have planned our objective:
We want to code a timer that counts down from 60:00 minutes to 00:00 minutes.
- We have designed our coding workspace (IDE) to have 4 functions or ‘containers’.
Library, Display, void setup() and void loop().
Next we will look at what each of these functions will contribute to the objective, alongside adding some real C++ code to it!
Library
Libraries are files embedded to the IDE that add more functionality and ease of use. For our objective, we will be using just one library file; the TM1637Display.h by Avishay Orpaz. This file includes a series of commands that we will use in our code, to allow the Arduino Uno tell the TM1637 display timer what to display in real time.
First off, we need to pull the file into our workspace. We can do this by selecting
Tools > Manage Libraries…
Next, we need to install the latest version (v1.20), make sure you choose the correct file, I’ve highlighted below to help you navigate.
Because I’ve already installed it, there is no install button for me. One for you, should appear in the right hand corner once you hover your mouse over. Once this has installed, we need to return to our workspace and under the // Library comment, type in:
#include <TM1637Display.h>
This now sets us up ready, to tell the Arduino Uno (and subsequently the display timer), what to do.
Display
There are 2 things we need to set up in this Display function.
- Declaring the clock and data in-out (DIO) pins.
- Declaring the length of the timer (60:00 minutes).
If you look on the back of your TM1637 display timer, you will notice that you will have 4 pins to connect via dupont cables, to the Arduino Uno; CLK, DIO, VCC and GND.
CLK = Clock, DIO = Data in-out, VCC = Power, GND = Ground
Power and ground pins don’t need to be declared, just the clock and DIO pins. In other words, we need to tell the Arduino Uno what number pins on the digital side (see The Arduino Uno from part 2) of the microcontroller will be connected, to the CLK and the DIO. As a rule of thumb, we don’t use pins 0 and 1; they are for transmitting and receiving signals, and is best not to interfere with them.
For this exercise, we are going to declare pin 2 as the CLK and pin 3 as the DIO. Return to your workspace and under the // Display comment, type in:
const int clkPin = 2;
const int dioPin = 3;
TM1637Display display(clkPin, dioPin);
const = Constant, ie: non-changing
int = integer, the number of the pin (eg: 2)
Now that you have successfully declared your CLK and DIO pins, next; we will declare the length of the timer. It is to be pointed out that whilst the timer will display in minutes and seconds, the length of time in the IDE must be declared in milliseconds.
Return to your workspace, and underneath your CLK and DIO declarations, type in;
unsigned long timeLimit = 3601000;
unsigned = positive value numbers only, prevents the timer from going past 00:00
long = a number with a large value
3601000 milliseconds = 60 minutes and 1 second. The reason for the additional second is that it takes 1 second for the TM1637 display to power up after the Arduino Uno does.
void setup()
There is only one thing to set up in the void setup() function; brightness of the TM1637 display.
Within the void setup() curly brackets, type in:
display.setBrightness(4);
void loop()
Finally, we will add the code to operate the meat of the artefact; the countdown mechanism.
This will be fairly larger in volume, compared to our current codebase.
Within the void loop() curly brackets, type in:
unsigned long timeRemaining = timeLimit – millis();
while(timeRemaining > 0) {
int seconds = (timeRemaining / 1000) % 60;
int minutes = timeRemaining / 60000;
display.showNumberDecEx(seconds, 0, true, 2, 2);
display.showNumberDecEx(minutes, 0b01000000, true, 2, 0);
if(millis() < timeLimit) {
timeRemaining = timeLimit – millis();
}
}
I appreciate that this may look confusing and alienating, so I’m going to do my best here to relay that code into pseudocode.
unsigned long timeRemaining = timeLimit – millis();
This is declaring a large, non-negative, real-time number called timeRemaining which is equal to the timeLimit (which we’ve already declared) minus the time passed. Ie: The value of timeRemaining will reduce by one second, every second and will show on the display.
while(timeRemaining > 0) {
Whilst the timeRemaining figure is larger than 0
int seconds = (timeRemaining / 1000) % 60;
This is declaring the seconds part of the timer as an integer and is equal to timeRemaining divided by 1000 milliseconds (1 second). The % 60 prevents the timer from using a number in the seconds part of the display that is equal to or larger than 60.
int minutes = timeRemaining / 60000;
This is declaring the minutes part of the timer as an integer and is equal to timeRemaining divided by 60000 milliseconds (1 minute).
display.showNumberDecEx(seconds, 0, true, 2, 2);
display.showNumberDecEx(minutes, 0b01000000, true, 2, 0);
These are commands to tell the TM1637 display how to show the timer to us humans in a way that is readable.
if(millis() < timeLimit) {
timeRemaining = timeLimit – millis();
If there is more than 00:00 on the display, remove 1 second off the timer, per second.
End Of Part 3
Next time, we will be connecting the Arduino Uno to our TM1637 display timer and testing out our code!
See you next time and thanks for reading!
0 Comment
[…] part 3, we created real code out of our pseudocode and placed it into our IDE. Part 4 will involve testing […]