PROJECT REPORT
Prepared by
Clancy Jembia
Andrew Huntley
Narong Yi
SPRING 2017
For Professor Tippens
ABSTRACT
Candy Counterstrike is a small target game. The aim of the game is to hit a moving target and at
the end of the game, candy will be dispensed depending on the number of times the target was hit.
The game also saves high scores which can be reset. There are visual indicators like 7-segment
displays and an LCD screen which provide a user-friendly interface. The game is designed around
the Raspberry Pi which is the brain of the system that incorporates many components like switches,
LEDs, an LCD display, a DC motor, a Servo motor, and a force sensor to model an entertaining
experience. These are all embedded in a wooden enclosure which serves as the body of the project.
The programming of this project was done completely in C.
1
Candy Counterstrike
TABLE OF CONTENTS
ABSTRACT ...................................................................................................... 1
1. INTRODUCTION ...................................................................................... 6
4. CONCLUSIONS .......................................................................................29
4.1. SUMMARY .................................................................................................................. 29
4.2. PROBLEMS ENCOUNTERED ........................................................................................... 29
4.2.1. Testing switches ..................................................................................................... 29
4.2.2. Motor design .......................................................................................................... 29
4.2.3. Pin damage ............................................................................................................ 30
4.2.4. Overcomplexity ...................................................................................................... 30
4.2.5. Construction ........................................................................................................... 30
2
Candy Counterstrike
5. APPENDICES ..........................................................................................32
5.1. WIRING DIAGRAM ....................................................................................................... 32
5.2. FULL SCHEMATIC ........................................................................................................ 38
5.3. PARTS LIST ................................................................................................................. 45
5.4. CODE ........................................................................................................................ 49
3
Candy Counterstrike
LIST OF FIGURES
Figure 6: RESET, MEMORY RESET and red EMERGENCY STOP buttons ........................... 13
Figure 9: limiting switch on rail of target and front face switches ............................................... 16
Figure 13: schematic of shift register and BCD to 7-segment decoder for a pair of 7-segment
displays ................................................................................................................................. 18
Figure 14: LCD face and PCF8574 backpack with contrast potentiometer ................................. 20
Figure 19: Servo motor attached to hold candy from falling on the ramp through the opening .. 24
Figure 20: motor and target coupling with limit switches ............................................................ 26
LIST OF TABLES
5
Candy Counterstrike
1. INTRODUCTION
This project is the summary of what we learned to get this far into digital 3. The experience to
build this project is what reinforces what we learned in this program. Of course, learning the
raspberry pi microcontroller is another experience on its own. This project takes in everything you
learn and is set up to be built in a timely manner simulating jobs in the real world. The project is a
success and a valuable experience for our future careers.
Dyspraxia is a brain based condition that makes it difficult for people to develop the basic motor
skills needed to get through the day. Tasks such as brushing your teeth, writing, or even walking
are all difficult to perform for people with this condition. [3] It is not something that can be
outgrown by kids; dyspraxia usually requires treatment in the form of physical therapy through
simple tasks
According to studies from Rochester University, simple video games that challenge the player to
aim for small dots or targets are presented before them. A marked increase in their ability to track
the dots was noted, but more remarkably, the test players motor skills increased in other tasks
such as driving, reading smaller print, and finding friends in a crowd. Daphne Bavelier, UR
professor of brain and cognitive science expert, recognizes this as probabilistic inference; it is the
ability for humans to constantly make decisions based on probabilities that they are constantly
calculating and refining in their minds [2].
Studies using a group of students from St. Marys High School noted that after a week of playing
games, the test subjects had a 14.87% increase in dexterity when using the Perdue Pegboard Test
which is typically used as a neuropsychological test for precise movements involving the hands,
fingers, and arms. [1]
Candy Counterstrike acts as a tool to hone your ability to perform certain tasks faster. As you
become better at playing under time constraints and a quicker target, you will be rewarded with
more candy from the dispenser. This emphasizes practice on quickening your motor skills to be
able to accurately hit the target under more straining circumstances.
As a whole, candy dispensers in some form are in the market everywhere; you can easily find them
in many public places including the mall, school buildings, and other shops. Despite this, the size
6
Candy Counterstrike
of most dispensers are typically at least 8 feet tall and take up quite a bit of room. You will very
rarely find one that doesnt occupy so much room. Candy Counterstrike is a much smaller unit
capable of providing bite size candy for those skilled enough to hit the target displayed.
Not only does it provide dispensing for candy, but also help in developing motor skills for those
in need. For children and adults alike, we hope to create a fun experience that rewards the user for
putting in the effort to improve.
7
Candy Counterstrike
2. OPERATING INSTRUCTIONS
Candy Counterstrike should be setup on a flat surface, preferably with its back to a wall. Before
powering on, make sure the closing lid is in place to protect the inner components of the project.
When playing the game, the user should stand about 6 inches from the front face of the structure
with his projectile in hand. Watch out for objects in the background which could be damaged from
miss hits.
After taking the proper precautions, make sure the power cord is connected to an appropriate
source then press the black switch located on the right side of structure to launch the game.
8
Candy Counterstrike
After powering up the system, the project will go through a debug phase where the visual indicators
will be tested before displaying a welcome message. After pressing OK on the welcome screen,
the main menu will be displayed. Navigation on this screen is done with ease using the UP,
DOWN, and OK buttons.
9
Candy Counterstrike
After selecting the play a game option from the main menu, a message will appear asking you
to select your settings. The settings are selected using the switches labeled TIME and SPEED.
These are three state switches which allow for very flexible difficulty settings. The number of
points per hit also vary depending on the settings chosen.
Basically, the more time you get and the faster the game is, the more points you get.
After the settings have been selected, the green PLAY button is hit to start the game. Once this
button is pressed, the following will happen:
- LCD display will display chosen (time and speed) settings, points per hit and a hit counter.
- GAME START LED will light up.
- TIME LEFT 7-segment display will start counting down from the selected time.
- The target will start moving (if a non-stationary speed was selected).
While the GAME START LED is on, any hit on the target will increment the SCORE counter on
the top-left 7-segment display and flash the HIT LED.
Once the time is up, the LCD will display the score and prompt the user to input their name which
is done with ease using the UP, DOWN and OK buttons. After the name has been inputted, candy
will be dispensed from the candy dispenser depending on the score and the high scores will be
shown on the LCD.
10
Candy Counterstrike
11
Candy Counterstrike
The high scores option from the main menu simply displays the high scores stored in the game.
When the game runs for the first time, a default set of high scores are loaded. While on this screen,
scrolling through the scores is easily done using the UP and DOWN buttons. The OK button is
pressed from here to continue.
2.5. ABOUT
The ABOUT option from the main menu is simply meant to show some information about the
project. When this option is selected, the LCD displays the names of the developers, the instructor
and the course information. The OK button is pressed from here to go back to the welcome screen.
12
Candy Counterstrike
These buttons located on the front face of the structure are used for control of the system.
At any time, the RESET button can be pressed to reinitialize the system. Once this button is
pressed, the system will go all the way back to the debug and welcome phase. Holding this button
will cause the pi to reboot.
If the user wishes to reset the scores to the default scores, they can hold the MEMORY RESET
button for 3 seconds. The scores will be reset and the system will reinitialize.
In case of malfunction or any other kind of emergency, the EMERGENCY STOP button can be
pressed. This will stop all functioning and freeze the system. From this point, the reset can then be
pressed to reinitialize the system.
13
Candy Counterstrike
3. TECHNICAL SPECIFICATIONS
3.1. RASPBERRY PI
At the heart of this embedded system is the Raspberry Pi. This is the device that was used to
interface all the different components and bring the project to life. The Raspberry pi for this project
was a Raspberry Pi 3 running the latest Raspbian release. Due to the high number of components
to be interfaced and the GPIO pin limitation of the Pi, several techniques and technologies had to
be used to work around this limitation. The software was done entirely in C with great help from
the WiringPi and Pigpio libraries. The wiring of each section can be seen in more detail in the
appendix.
3.2. POWER
Designing a power supply for this system was key because under-powering such a system will
cause the project to fail. At the beginning of the project, a rough power calculation estimate was
done to determine approximately how much power would be needed for the system.
14
Candy Counterstrike
Based on these calculations, a 12V3A=36W supply was chosen to supply the project and account
for any unforeseen power demands.
The 12V supply was converted to 5V for the pi using a 5V-3A DC-DC converter. Fuses were used
to protect the rest of the circuit from surge currents. The wiring of the power board can be seen in
the figure below:
15
Candy Counterstrike
3.3. SWITCHES
The switches are an integral part of the project as they are the main way in which the user interacts
with the project. Given how many they are (15 in total), it would be impossible to connect them
directly to the Raspberry Pi and have enough pins for the rest of the project. As a result, we chose
to use the MCP23017 which is an i2c port expander IC.
In terms of software, the WiringPi GPIO utility was useful in setting up the port expander once its
address was found. The switches were all connected one pin to ground and the other to an input
pin, (figure 11), with the internal pull-up resistors enabled. This made for an active low input logic
on the front board switches. The limiting switches on the motor rail were connected in a similar
manner, only directly to the GPIO pins of the Pi. The high speed of the processor made it easy to
miss a press of a button. As a result, a delay sub routine was used to continuously test a switch.
Refer to the appendix for more details on wiring and code.
16
Candy Counterstrike
17
Candy Counterstrike
3.4. LEDS
In terms of LEDs, there were 4 7-segment displays (2 for time and 2 for score count) and 2
indication LEDs making a total of 30 LEDs. Again, connecting all these directly to the Pi would
make the project impossible. As a result, we decided to use the 74HC595 shift register with the
74LS47 negative logic BCD to 7-segment decoder chip as the 7-segment displays were common
anode.
The 74HC595 accepts serial input from the Pi and puts it out in parallel on its 8 output pins. Four
of these outputs were input to a 74LS47 which takes BCD input and outputs the correct 7-
segment signals. Given that we had four 7-segments, two shift registers and four BCD to 7-
segment decoders were used. Figure 13 shows the schematic for these parts. In the code, the
switches and LEDs were combined.
Figure 13: schematic of shift register and BCD to 7-segment decoder for a pair of 7-
segment displays
The following snippet of code shows how these components were initialized.
18
Candy Counterstrike
19
Candy Counterstrike
A 20x4 (20 column 4 row) LCD was chosen for this project as an interface to users. It displays
key information and options which users can select using the UP, DOWN and OK navigation
buttons.
To reduce the pin consumption, the LCD was hooked up with i2c with the pcf8574 IC mounted as
a backpack on the LCD. Mounted on the front panel of our project, the viewable screen pokes
out of the wooden board. At the back of the LCD is a potentiometer used to vary the contrast of
the LCD to an acceptable value. The wiring of the LCD can be seen in figure 15.
Figure 14: LCD face and PCF8574 backpack with contrast potentiometer
The following snippet of code shows the setup of the LCD module. The rest of the code can be
seen in the appendices section. In the case of the LCD, the WiringPi library was of key importance
as it comes with many of the necessary functions built in. Note that the LCD operated at a voltage
of 5V which means that it cannot input data to the Pi as that would exceed the 3.3V input limit and
possibly damage a Pi pin. As a result, the RW pin was set permanently to high.
20
Candy Counterstrike
The target was implemented using a force sensing resistor. The force sensor used in this project is
simply a resistor whose value changed as it was pressed. Being a variable resistor, its resistance
(or amount of force applied on it) could be converted to an analog voltage signal by using a simple
voltage divider configuration. The analog signal was then fed to the Pi through the MCP3008 chip
which is an 8-channel 10-bit ADC with SPI interface. the wiring can be seen in figure 17.
Parameter Range
FSR resistance Infinity (open circuit) - 253
Input voltage 0 3.5V
Reference voltage 0 3.3V
As seen from the table, the input voltage closely matches the voltage reference but goes a little
above it. This was no issue since the force sensing resistor was only used to pick up the slightest
hits which meant the higher voltage (force) values were of little interest.
The following snippet of code shows how the ADC was setup. More details in the appendix.
22
Candy Counterstrike
The target pad consists of an ADC pressure sensor that tracks any target hits from a projectile.
Once a hit is detected, the HIT light brightens and the score on the 7 segment LED increments
The target is built with the sensor placed on a wooden board that resembles the shape of the target
pad. This board is then attached to a plastic tube where the wires are attached from the sensor
wired through the tube and then connects to our middle board on the top of the project.
For the function of dispensing candy, precise movement was needed to control exactly how much
to give out. For this reason, a servo motor was used to accurately position a panel that would
control the flow of candy depending on the number of points scored. For this reason, we decided
to use a servo motor. With this, it was possible to control precisely the number of spins; with the
Pigpio library, control of the position with PWM signals was a mundane task. The connection to
the Pi was done quite simply as seen in figure 18.
23
Candy Counterstrike
The more complicated part was mounting this motor as part of the design for the candy dispenser.
The Candy Dispenser is the reward function of this project. When the user satisfies the conditions
of the game, candy is served through a dispenser. This dispenser is being controlled by a servo
motor. The number of spins on the servo motor is determined by the users score and time allotted
which also correlates to how much candy can come out. When the dispenser is not in use, it is in
its reset position which blocks candy from falling out.
24
Candy Counterstrike
The Candy Dispenser is a reward function. After the game is done, candy is dispensed depending
on the players score. The number of spins on the servo motor determines how much candy will
come out. The reward system is summarized in the following table:
The candy dispenser is located away from most electrical devices. It is built with a ramp that will
stop candy from being stuck in the back. It is supported by walls so the candy cannot spill out into
the project. The servo motor is located next to a hole on the wall where a piece of wood is glued
onto the base to keep the right height. The servo motors propeller is attached to a rectangular piece
of wood which acts as the blades that dispense the candy. When the dispenser is not in use, it is in
its reset position which blocks candy from falling out.
The code associated with the servo motor was combined with the DC motor code. More details
about the schematic and code can be found in the appendix.
25
Candy Counterstrike
3.8. DC MOTOR
The DC motor in the project is used to make the motor move. The motor is mounted on a flat
platform and firmly held in place. The rotor of the motor is then coupled to a threaded rod through
a wooden block. Attached to the threaded rod are two bolts which carry the target. As a result,
when the motor spins, the bolts carry the target linearly. Limiting switches are attached at the ends
of the targets path. These switches are used to keep the target from going out of bounds.
Due to the inductive nature of the motor as an electric load, a high current is needed to drive it
properly. Such a high current coming out of the Pi will more than likely cause damage to the Pi
(we tried this). Consequently, we decided to use the LD293D which is an h-bridge motor driver
with a maximum output current of 600mA. Given that the operating current of the motor we
chose was 400mA max, this was largely sufficient.
In terms of software, controlling the motor simply meant outputting a high on one of the motor
driver inputs with the other one low. To control the speed, however, we used a PWM signal to
drop the effective voltage. The following snippet of code shows how the motor was controlled.
The wiring of the motor can be seen in the appendix.
26
Candy Counterstrike
3.9. WIRING
The wiring of the project was done on boards whose functions are documented in the appendix
section. Appropriate soldering techniques were used as well as the following color coding scheme:
The interconnections between boards was done using ribbon cable connectors of various colors.
These are all documented in the wiring diagram under the appendices.
27
Candy Counterstrike
28
Candy Counterstrike
4. CONCLUSIONS
4.1. SUMMARY
Candy Counterstrike was intended to be a game that tests the users ability to accurately aim and
shoot a projectile at a target. Through the use of several navigational buttons and switches
combined with an LCD terminal, a lucrative system that was designed to keep track of the users
scores and to dispense a reward based on the number of points scored was developed.
The formation of Candy Counterstrike was an overall success. A functioning target that accurately
recorded the users current score and time was created. All components were fully functional
except for a few problems encountered.
One of the first problems encountered was testing switches. Due to the high speed of the processor,
it was easy to miss when a button was pressed. As a result, we resorted to using a delay sub-routine
to continuously test an input. A delay that was too long, however, would cause awkward waiting
times so the delay had to be optimized depending on the function.
The delay we ended up using worked well for most functions but could be better. In our opinion,
it would be best to use an interrupt sub routine to test switches to provide the optimal user
experience.
The motor used at the end of the project was not the motor we initially selected. The first one was
a 6V motor with little torque. Although its speed was high, it was not able to move the target. From
here, we realized that the speed isnt the only thing important when picking a motor; the load
29
Candy Counterstrike
torque is important as well. We ended up upgrading the supply form 5V to 6V and getting a
stronger motor.
The afore-mentioned inappropriate motor design caused damage to the Pi which we had hard times
detecting. Some pins of the Pi were somehow damaged and some serial components (ADC and
shift registers) malfunctioned. This caused the force sensor to be unresponsive and the motor
direction could not be reversed while a set of 7-segment displays displayed garbage. After days
of frustration not knowing where the problem was, we were able to solve the problem by doing
several multi meter tests and ultimately replacing the Pi. For a long time, we doubted the code and
soldering while the actual hardware was faulty. Multi meter testing came in very handy during
these times.
4.2.4. OVERCOMPLEXITY
If you noticed, many different combinations of technologies (SPI for the ADC and 7-segment
displays, I2C for the LCD and port expander). Although this afforded us the opportunity to work
with more tools and learn more, everything would be simpler and faster if one uniform solution
were applied, preferably I2C due to the low number of pins needed (2 pins from Pi).
4.2.5. CONSTRUCTION
One problem our group found especially challenging was constructing the project. Neither of us
really had any experience building such structures so doing it for the first time was not an easy
task. We were able to manage by using the wood shop at the architecture building which we found
to be a great resource: without it, this project wouldnt have been the same. Construction material
could be found at Home Depot.
30
Candy Counterstrike
This problem came up when coding the actual game. We needed to make the game run for the
number of seconds chosen by the user while testing the sensor and limiting switches at the same
time. The problem with this is that the timer is done using the delay() function from the GPIO
library. During a delay, the microprocessor just does nothing so it is impossible to delay and test
inputs at the same time in a single function. To solve this problem, we resorted to using threading
where one function would handle the timing and another function would handle the testing. The
two would communicate using a global variable: when the testing starts, the testing function sets
it and when the time is up, the timing function resets it. This can be seen in more detail in the code
appendix.
To manage high scores, a file was used where new scores would be written and could be retrieved
later. We could successfully write and read from the file but the problem was that after reboot or
re-initialization, the file would get corrupted and contain garbage. Consequently, what we did was
create the file (if it didnt exist) and write default scores to it at every run. We plan on doing more
research on this since we were pressed for time toward the end of the project but we believe the
key is using a .txt file instead of a .dat file which we used.
The design we used for our moving target was not optimal: the target made a lot of noise when
moving and did some front and back jerking. In addition to that, it was also very slow. The target
pad also had a sensitivity issue where it would not record the slightest hit. The solution to all this
is to use better equipment to design the moving parts perfectly and use a more sensitive sensor or
even a Hall Effect Sensor with metal projectiles.
31
Candy Counterstrike
Increasing the size of the candy dispenser may be warranted for future designs; only candy
approximately 2-3 inches in size can properly fit. In addition to this, a design that does not
implement a large winding rod would allow for quicker and smoother movement of the target
while needing less torque to actually move the target.
5. APPENDICES
Switch board
32
Candy Counterstrike
J1 1 External 12V
2 External GND
J2 1 5V
2 GND
J3 1 5V
2 12V
3 GND
Raspberry Embedded system used for control.
pi J1 1 5V
2 GND
J8 and others See Raspberry Pi specifications
Breakout Main board which interfaces different components like the pi and LEDs
board J1 1 5V
2 12V
3 GND
J2 See Raspberry Pi data sheet
J3 1 GND
2 ADC Digital in
3 ADC Serial Clock
4 ADC Chip select
33
Candy Counterstrike
5 3.3v
6 Led 2 control
7 ADC Digital Out
10 Led 1 Control
J4 Connector to the 74HC595, an 8-bit serial-in/parallel-out shift
register to interface 7-segment displays for time
2 5V
3 GND
4 Shift register 1 data
5 Shift register 1 latch
6 Shift register 1 clock
J5 Connector to the 74HC595, an 8-bit serial-in/parallel-out shift
register to interface 7-segment displays for score
2 5V
3 GND
4 Shift register 2 data
5 Shift register 2 latch
6 Shift register 2 clock
J6 Wires for LCD display interfacing. See LCD datasheet.
J7 Connectors for the momentary and fixed switches
34
Candy Counterstrike
35
Candy Counterstrike
36
Candy Counterstrike
2 5v
3 SCL
4 SDA
Switch Interface circuit for front panel switches as well as servo motor for candy
and servo distribution
motor J1 1 5V
board 2 GND
3 Signal for servo
J2 1 5V
2 Signal for servo
3 GND
1 Up button for selection
2 Ok button to validate selection
3 Down button for selection
4 Game start button
5 Emergency stop button
6 Speed selection switch
7 Duration selection switch
8 Memory reset button
9 Soft reset button
37
Candy Counterstrike
38
Candy Counterstrike
39
Candy Counterstrike
40
Candy Counterstrike
41
Candy Counterstrike
42
Candy Counterstrike
43
Candy Counterstrike
44
Candy Counterstrike
45
Candy Counterstrike
46
Candy Counterstrike
47
Candy Counterstrike
Total: $275.72
48
Candy Counterstrike
5.4. CODE
49
Candy Counterstrike
50
Candy Counterstrike
51
Candy Counterstrike
52
Candy Counterstrike
53
Candy Counterstrike
54
Candy Counterstrike
55
Candy Counterstrike
56
Candy Counterstrike
57
Candy Counterstrike
58
Candy Counterstrike
59
Candy Counterstrike
60
Candy Counterstrike
61
Candy Counterstrike
62
Candy Counterstrike
63
Candy Counterstrike
64
Candy Counterstrike
65
Candy Counterstrike
66
Candy Counterstrike
67
Candy Counterstrike
68
Candy Counterstrike
69
Candy Counterstrike
70
Candy Counterstrike
71
Candy Counterstrike
72
Candy Counterstrike
73
Candy Counterstrike
74
Candy Counterstrike
75
Candy Counterstrike
76
Candy Counterstrike
77
Candy Counterstrike
78
Candy Counterstrike
79
Candy Counterstrike
80
Candy Counterstrike
81
Candy Counterstrike
82