Hardware Reference
In-Depth Information
Use outputs to indicate the client's status.In the following
code, you can see that in addition to indicating when the
sensors are triggered, you'll also indicate whether the client
is connected or disconnected. If this client had a more
complex set of states, you'd need more status indicators.
can build either, or both, or use the principles from them
to build your own. Building both and comparing them will
give you an idea of how the same protocol can result in
very different behavior. One of these clients is much more
responsive than the other, but the responsiveness has
nothing to do with the communications protocol. It's all in
the sensing and in the player's action. Both of the clients
use logic shown in Figure 5-2.
X
For this project, I built two clients. They have different
methods of physical interaction and different input
sensors, but they behave the same way to the server. You
Client #1: A Joystick Client
The first client is a fairly traditional game controller, shown
in Figure 5-3. It's got a joystick to control the left/right
action, and a pushbutton to log in or out of the server.
There are also four LEDs to indicate the controller's state.
When it's logged in, the white LED is lit. When it's sending
a left command, the red LED is lit. When it's sending a
right command, the green LED is lit. When you're pressing
the connect/disconnect button, the yellow LED lights up.
MATERIALS FOR bOTH CLIENTS
» 1 Arduino Ethernet board or Arduino board with
the Ethernet shield
» 1 Ethernet connection to the Internet
» 1 100-ohm resistor
» 3 220-ohm resistors
» 1 2-axis joystick (for joystick client)
» 1 accelerometer (for tilt board client)
» 1 perforated printed circuit board
» 4 LEDs
» 1 pushbutton
» 1 project enclosure
» 1 sheet extra-thick cardboard (for tilt board
client)
This client uses one axis of a two-axis joystick as its
main input. The joystick contains two potentiometers:
one for up/down movement and the other for left/right
movement. You'll only need the output from the latter.
Connect the pin marked L/R+ to +5V, the one marked
GND to ground, and the one marked L/R to analog input
0 of your Arduino (the full circuit is shown in Figures
5-4 through 5-8). Then write a short program to read its
output using analogRead(), just as you did with the Monski
Pong sensors in Chapter 2 and the cat sensors in Chapter
3. Once you've got it working, note the values that indicate
left and right, and save them for use in the client program.
Both the perfboard and breadboard layouts are shown so
you can choose whichever method you like.
You'll notice that the connection switch is wired differently
from a normal switch. Instead of a 10-kilohm pulldown
resistor, it's got a 100-ohm resistor and an LED connecting
it to ground. This way, when you push the button, the LED
lights up. There's no need to add any code to make this
happen—the pushbutton will send current through the
LED automatically, and you'll get local feedback on when
the button is being pressed.
You'll need an enclosure for the circuit. You can buy a project
case from your favorite electronics retailer, but you can also
get creative. For example, a pencil box from your friendly
neighborhood stationery store will work well. Drill holes
in the lid for the switch and the LEDs, cut a hole for the
joystick, cut holes in the side for the Arduino and Ethernet
jacks, and you're all set. If you're skilled with a mat knife, you
can also make your own box from cardboard or mat board.
Figure 5-7 shows the box I made, and Figure 5-9 shows the
template from which I made it. If you're lucky enough to
have access to a laser cutter, use it; if not, a mat knife and a
steady hand can do the job.
X
These two clients are laid out on printed circuit proto-
typing boards (commonly known as perf boards). The
perfboards make it possible for the circuits to be a little
smaller and more physically robust. They're a bit harder
to assemble than a solderless breadboard circuit, though.
 
Search WWH ::




Custom Search