This is a kind of a follow up on my Make an Arduino Memory Game tutorial. Someone in a comment asked for an LCD display to see the score and get a really mean message to make you feel inadequate when you lose (I might have embellished a little bit that last part :)
So I took my own tutorial and followed it... (There's something quite rewarding about that. :)
The picture doesn't exactly match the diagram because I wanted to push all the wiring to the back to give space for my big fingers to reach the buttons.
And I went and added an LCD to it.
The wiring of the LCD is crazy! And it uses Sooooo many pins! (More on getting around that in a later post) But once it's connected and working, it's so pretty... And magical.
So here's the diagram.
I took the diagram from the Memory game tutorial and updated it for the LCD. All the wires in the funny greenish colour can be ignored because they haven't moved. Simply connect the other wires as displayed on the diagram. The purple ones are crossing so you are connecting the pin d4 of the screen onto pin 4 of the arduino, d5, to pin 5, etc... Neat hey? (Pin d1 to d3 of the screen aren't usd for anything here. )
The bright green wire is actually supposed to be connected to a potentiometer. I had one, I lost it. So I tried a couple of resistors until I got a contrast that I liked and settled for a 1k Ohm resistor.
The proper way to setup the contrast circuit is explained there.
Once done, your game should remain unchanged but the screen should light up.
To work with the LCD, we need the library for it. WE could write all the code that talks to the screen but someone did that for us already so why bother...
Adding a library in Arduino is done by going to Sketch => Include Library => Manage Libraries...
Search for 'liquid'. If yours also says 'INSTALLED', you're set. If not, you should see an 'Install' button in the bottom corner of the block. Click on it :)
Next, load the code for this project onto your Arduino...
And you're done! Pfew! That was tough! :)
Surprise! The game now has a high score! But the first time you load the program, the high score might be something ridiculous that you might never be able to beat.
To reset your high score:
- Connect a 10k Ohm resistor between the 5V rail of your bread board and pin A0 of your arduino.
- Reset your Arduino. No the high score should be 0
- REMOVE the resistor
You are not allowed to use this technique to steal the high score from your siblings! :)
Now go beat that 0 high score!
From this point I'm going to go through a few line of code pertaining to the LCD and the EERPOM to explain what they do so that you can try to do your own thing and customize the program.
Detailed instructionsSo first
#include <EEPROM.h>
#include <LiquidCrystal.h>
These 2 lines tell the program that we will need to access other libraries to do stuff.
The EEPROM library let's us talk to the EEPROM on the Arduino (Electronically Erasable Programmable Read Only Memory). The help files for it are here. The LiquidCrystal library's help files are here. Having added those lines, we can now use the library (And the devices they are written for):
const int rs = 12, en = 11, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
And these are the lines that setup our LCD. The first line defines all the connections between the LCD and the Arduino. That's where you plugged your LCD. If you don't like the way I plugged it on my program, for example, if you want to connect the d4 pin of the LCD to pin3 of the Arduino, you would change the first line to:
const int rs = 12, en = 11, d4 = 3, d5 = 5, d6 = 6, d7 = 7;
The second line creates an LCD object for the program to use. The concept of Objects in programming is a little advanced but simply put, an object is a black box that does complicated things while making it look simple to you. You tell it what to do and you don't have to care how. All we need to worry about for our LiquidCrystal object (now called 'lcd') is that we give it the correct pins to play with.
lcd.begin(16, 2);
In the setup method, we use this line to tell the object that it will be dealing with a 2 lines 16 columns sceen and that it should get ready for work.
And then we write stuff on the screen... Because we CAN!!! :D
lcd.print("Welcome!");
lcd.setCursor(0, 1);
lcd.print("High Score: ");
lcd.print(highScore);
the setCursor function tells the lcd object where we will be writing next.
Here 0 is the column so we will write at the beginning of the line. And 1 is the row. row 0 is on top, row 1 is the second one. A lot of programming things work that way and start counting from 0. It's easier for the computer to handle. As a prorammer, you'll get used to it.
So if you want to move the 'welcome!' to the center of the top line, you could add this line.
lcd.setCursor(0, 4);
lcd.print("Welcome!"); //this line stays where it was
Why 4?
- The screen is 16 character wide.
- 'Welcome!' is 8.
- 16 - 8 = 8 (duh! :)
- 8 / 2 = 4 (So 4 spaces on both sides)
- So position 0, 1, 2 and 3 will have to be empty and we tell the program to start at 4.
You could also do this:
lcd.print(" Welcome!");
But that's cheating :)
The EERPROM library is a lot simpler to use and at the same time a lot more difficult to manage for beginners. Memory allocation in a computer (Yes, that's what an Arduino is) is a science. But here, we just do something really simple:
EEPROM.put(0, highScore);
If we beat the high score, we just write it into the EEPROM of the Arduino in the first memory location available (remember that 0 is the first position thing...) so we don't lose it when we shut it down.
And when we start, we fetch it from the same spot so we have something to compare our score to.
EEPROM.get(0, highScore);
Finally, this piece of code is for clearing the memory.
if(digitalRead(A0) == HIGH){
for (int i = 0 ; i < EEPROM.length() ; i++) {
EEPROM.write(i, 0);
}
}
The first line detects whether we have an input on A0 (That will happen we we plug the resistor across as I described above. Then the program loops through the entire memory and writes 0 into each memory block. That's why we have to remove the resistor when we're done or every time we reboot, the memory will be cleared.
And also, because we use this line to get a random value seed,
randomSeed(analogRead(A0));
we will have sequences that are almost consistently the same colour. I tested it and got 4 x yellow, green, 9 x yellow. Not very challenging.
ConclusionsI hope you have fun with your new game and get to improve it, or come up with awesome new ideas to try out.
Comments