Introduction: How to Code Your Own Claw Machine
I, along with many other people, have childhood memories of walking into a movie theater, grocery store, or arcade and seeing a large glass box filled with toys. There was always that one toy either buried or perched right on top that I wanted. All I had to do was get some money from my parents and move the claw over the toy and press the button. In my childish innocence, it seemed so simple.
However, as many of you can probably guess, I soon found out I was wrong. It was nearly impossible to grab any toy, let alone the one I wanted. Since I have such "fond" memories of the claw game, I decided that I wanted to have one of my own, and give everyone else the power to create their own. Since I have a background in FPGA design I decided to turn the Basys 3, a Digilent FPGA, into the brain of one of these devices. This Instructable goes over the steps and files to create the acrylic enclosure for the claw game. First you'll need to cut and 3D print the pieces, then you'll assemble the structure. Being that this is such a big project, I’m going to be making Instructables for each piece, and putting them into one collection. You can view this collection and the Instructables that I’ve created thus far in The Claw. If you are interested in my journey of the claw game, more about why I made it, or where it is going go to the Digilent Blog.
Step 1: Gather Tools
Step 2: Understand the Code
The main component to the claw game code is the state machine.
If you don't know what a state machine is you should check out this blog post. In short a state machine is hardware language structure that allows you to code a series of actions in one module. You know what you want to do, but you don't know how to code it. A state machine is a way to organize your thoughts with a standard code structure.
In the image in this step you can see the state machine for the claw machine.
The claw machine starts and resets to the wait_state. This state exists simply to wait for the start button to be pressed.
Once the start button is pressed and it is properly debounced the next state is the start state.
From here the joystick data, represented by direction, enters into one of the four direction states. In these direction states the corresponding motor is enabled.
From the start state if the button is pressed again it will enter the loop where the claw drops and grabs a prize, or doesn't, then returns to the wait state.
Step 3: Understand More About the Code
The state machine is only on part of the code. It figures out what direction the motors should go, and when the claw should be open and closed based on the joystick input.
As important as that is it's also important to be able to interface with all the parts. For example the stepper motor interface that converts the signal from a direction, to the 4 stepper motor signals required to drive them, and the servo controller that changes the claw open/ close signal to the PWM output the servo requires, and the joystick controller, that takes the joystick data and converts it to a direction. There's also a debouncing module that takes the signal from the limit switches and buttons and makes sure a stable signal gets sent to the state machine.
You can see how all of these are connected in the block diagram.
Step 4: Download the Code and Program the Flash
To this step I've attached the code in two forms. The source files alone, in case you aren't using Vivado, and the Vivado project.
Download the code, generate the bit file and program the flash of your Basys 3. Directions for programming the flash can be found here.
Step 5: Revel in Your Sucess
Once the board is programmed you can view the state code, found in the claw state machine code, on the first 3 LEDs.
Once the Electrical System is in place, the acrylic box is built, and the frame for claw movement is built, you will have your very own giant claw game.
All of the instructions for these steps will be posted in The Claw, Instructable collection.