Stoplight LED Game
Imagine having a sweet, battery operated, handheld entertainment device that you could bring around anywhere(and no, we're not talking about a phone). That's exactly what this pocket device aims to achieve! Bring around this Stoplight LED game that will be a fun tool to play with while also learning important basics of computer science and design! Since I recently graduated with a computer science degree, one of my favorite and most useful topics was how bit shifting works so I thought to share this with everyone else to try. Lets jump right in!!
Supplies
Test the Game!
To start the game, press the start simulation button. Once the simulation has started, press the pushbutton and the game will start. Try to press the button when the green LED is lit up. If you miss, the game will end then you will need to press the button again to start the game again. Unfortunately, we cannot use code blocks to do bit shifting so I had to programmatically create it.
Tinkercad is a fantastic tool that I use for every project whether it's testing my program or building 3D models for my inventions. Attached in the link below is where you can go and download my project. It should come with my code as well. I have also attached the .ino file that the Attiny85 uses.
https://www.tinkercad.com/things/0hZeQxe8G9D
Downloads
Game Theory and How It Works
This game may be confusing for first time users who have not had any computer science experience. I will be explaining in layman's terms how these components work together so we can see our end results.
- Battery
- The battery first has 3 volts. We supply this to a 120Ohm resistor so we can reduce the voltage for the project. Otherwise the 3 volts has to much voltage for the 74HC595 shift register when we start using it.
- After the resistor, we go to a 1 amp switch. This will turn the project on and off.
- ATTiny85
- This will be the brains of the operations. We will have to program the attiny85 using the provided code in the project. It is important to note the orientation of the attiny85. The top of it has an intend which is shown in the pinout diagram. That will help you know what pins to use.
- Attached to this step is the pinout diagram of the attiny85
- We need to connect pin 8 to the power rail supplied by the battery.
- We need to connect pin 4 to the ground of the battery
- Next, we can connect pin 2 to one side of a pushbutton. The next closest rail on the button will be connected to ground. In the code we check to see when we receive a ground signal and that will be how we control the user input in our game
- Pins 5, 6, 7 will be connected to the 74HC595 and I will explain how they work in the next section
- 74HC595
- This is the 8-bit serial in, parallel out shift register with a maximum power consumption of 80-µA. For a more detailed explanation, here is the datasheet. Our goal with this is to send in a signal one bit at a time to control many LEDs with just three outputs. Here is the website I used for some of the details about these pins.
- Pins 1-7 are our output pins, meaning we can connect these to the 80ohm resistors then to an LED. We can tell the shift register what LEDs we want to light based on what pin they are connected to.
- Pin 8 should be connected to ground.
- Pin 16 is power and is connected directly to the power rail.
- Pin 14 is the DS pin(Serial Data). We will tell this pin which of the 8 LEDs to light up. This should be connected to pin 5 of the attiny85. In the code, we will set the bit to which LED we want to light up and it will be sent through this signal.
- Pin 13 is the OE(Output Enable) pin which can be connected to ground. It needs to be held at low for normal operations so we don't use the output data.
- Pin 12 is the STCP(Latch) pin. This pin will connect to pin 6 on the attiny. This will update and control the next LED and tell it to be set or to shut off.
- Pin 11 is the SHCP(Clock) pin. It is essentially the clock from the micro controller.
- Finally, pin 10 is the Master Reset. This needs to be geld at high and connected to the power rail so the operations can perform normally and so we don't need to clear all of the operations.
I hope you learned a bit about how each of these pins work. Let's move onto the code to see how to program it!.
Code the Project
For this step, we will be programming the ATTiny85 so it can produce the logic we need for the game.
For the setup function, we have 3 outputs and one input_pullup. The three outputs are the latchpin, datapin, and clockpin. If you recall from the previous step, we will have to connect 5, 6, 7 to the shift register. The buttonPin will be connected to pin 2 on the attiny85. When pressed, it will send the ground signal to pin. In our loop, we are constantly looping to see if the user is pressing the button or not. This is what the digitalRead function does. Now we have 3 different gamestates we will be looping through depending on what stage of the game you are in.
- Gamestate 0:
- This is while we are at the start menu. We wait for a press and once we receive one the next gamestate/animation will start
- Gamestate 1:
- This will run the first simple animation. It will start from both ends of the LED's and light them up towards the middle. Once the middle one is lit up, the next gamestate will start,
- Gamestate 2:
- While the game is playing, it checks to see when the user has pressed the button or not. If they have, it checks what LED the game is currently on. If it is on the middle LED, we win and the game switches to the next mode and speeds up the game. If we lose, the LEDs turn off and the game switches back to mode one.
- Also while we are in game two, it is constantly updating the shift register by opening up the latch pin and allowing us to enter in the bit that needs to be set then closing the latch pin.
Attached in this link is an amazing step by step tutorial on how to program to an ATTiny85 using an Arduino Nano. This intractable is not ab out how to program one of these so I did not want to take time rewriting the wheel:
http://solosodium.github.io/2017-08-07-program-attint85-with-arduino-nano
Solder Together the Project
For this process, we need to solder the components together. Luckily, this step has been made simple because we know everything will work due to prototyping on tinkercad. We just have to slide the individual pieces on the breadboard and solder them down according to this diagram. For this, I used a PCB board that I was able to put my pieces in place and solder them.
3D Print the Project
For this process, I used my Ender 3D printer to print out the enclosure for the project. Now since we have the project put together, we just need a casing around it to finish it off. I have tagged in the image what each component is and where the items need to be placed. After assembling the project, we can use our screws to screw together and hold the components inside of the container.
End Result
Here is a YouTube video showing the full process from start to finish. I fully programmed, soldered, wired, 3D printed, and assembled the pocket project! Overall, this project is a quick and simple one that can provide as a great learning experience. Using shift registers is an important part of computer science and understanding the basics of how computers think. I learned this in my computer science class and fell in love with how complex these little components are. Once you have mastered how these registers work, continue working with them and daisy chain the registers to build much larger projects!
Overall, thank you for your time and I hope you enjoyed. Let me know how your projects turn out. I would love to see how others have modified it to their likings. You can add more gamemodes, LEDs, or even a two player functionality to the game!