In this tutorial I will show you how to set up your own Arduino to play Hangman by sending Morse code with a simple flashlight or the torch function from your phone.
All you need to follow along are:
An Arduino Board.
Wires to connect it all.
A Piezo element (to add sound).
Step 1: Setting Up the LED-lights
This game of Morse will make use of 4 lights to indicate the 4 letters in the word that need to be guessed and 5 lights to indicate the lives the player hast left to guess with, so first of all you'll have to connect 9 LED-lights in the usual way, as can be seen in the picture.
Step 2: Add a Makeshift Light Sensor
We can't go around throwing money at electronic stores, so I've decided to keep this project cheap, by using a simple LED-light as a makeshift light sensor.
All you need to do is connect the plus-side of the LED-light to one of your analog ports (in my case A0) and the minus-side to your ground.
Step 3: Add Your Dictionary
First things first, lets start out by adding a sort of dictionary to translate letters to their corresponding Morse codes. To do this, we simply make an array of 26 by 5, with the first slot of every secondary array having the letter and the 4 next slots having the corresponding Morse code or a space if there is no code left. So for example with the letter s, the first slot has 's', the second, third and fourth slots have a '.' and the final slot has ' '.
Step 4: Make the Lights Light Up by Default
In this step, we make sure the lights are on once the program starts and Morse the letter they represent once the user has correctly guessed them (the code for this will be written in step 6). We also make sure there are only as many lights on for the lives as the player has lives left.
Before this, make sure you have an array with the port-numbers and integers set to 0 to represent the time at which the light should go on, off and which part of the Morse the LED-light is currently showing.
Step 5: Adding the Input
Now we shall write the code to check whether the player is shining a light on the LED-light connected to our analog port A0, and if they are to translate it into the correct letter.
To make sure the reading is correct, we average out every 20 readings by the LED-light and use that number to track whether the light is off or on. To do this, at the start of the program we set NUM_AVG to the number of times we want it to check the light before averaging it out. The higher this number is, the more accurate the reading, but it may also cause delays.
msBtnPushed represents the amount of time that the light has been on for.
lastPressed is a boolean that keeps track of whether in the previous loop the light was on.
Step 6: Handling the Input
For this step, we shall take a look at what to do once enough time has passed to assume no more Morse is going to be added, and we need to check whether the Morse sent in forms a letter and whether it is a correct letter.
To do this, we need to have the string wordToGuess set to the word we want our players to guess. I personally have a random number generator to set it to a random word at the start, but you could also simply set it to a single word.
We also need to make sure the counter goes up by one every loop, otherwise the whole program wouldn't do anything. Furthermore, I have added the delay function, to make sure there's a loop every milisecond.
PS: All the Serial.println functions are just for testing purposes. They may be deleted upon completion.
The goodTimer and badTimer are simply values for if you are planning to use a Piezo element to add sound.
If you wish to do so, you must also add the code in the second picture, which plays the sounds.
Step 7: Morsing the Correctly Guessed Letters
As promised before, in this step we shall make the lights that represent the letters that have been guessed correctly Morse their letter.
For this, I have written two functions: morseThis and nextLetter.
It uses the numbers we have previously set in our timers array and changes them depending on which part of the Morse code it is currently shining.
If the timer reaches the time at which the light should go on, it simply turns on the light and runs the code to set new timers to turn the light off and on again.
If the timer reaches the part at which the light should go back off, it turns off the light and then sets the code to represent the next part of the letter. If there is no next part, it resets it to the first part.
Step 8: Reviewing Our Setup and Reset
Of course the program won't run if the lights aren't set to output and input. I also add a random number generator to add a word from a list of words I have put in an array called wordsToGuess.
Besides that, we should add the reset function and make sure all values are reset to default.
Step 9: All Variables in One Picture
Finally, you may use this to check whether you have all the necessary variables set up correctly.
Now enjoy playing Hangman in Morse!