I don’t know which extremes will be 0 and 1023,
however, so you may need to adjust the program to match
this up if I guessed wrong.
be 511. It probably won’t be, but I can show you how to
deal with that in code.
I don’t have a joystick shield but I can read a spec
sheet and schematic, so I know that the two joystick
directions are connected to the analog inputs A0 and A1.
Listing 1 is the sketch that I came up with that does
what you want to do.
The ITEAD documentation says that the X axis is connected
to A1 and the Y axis is connected to A0.
Because you can have both the X and Y axes at
extreme values at the same time, both axes are checked at
the same time. We would like to believe that the center of
the joystick would be exactly the center of the ADC possible
values, but it really isn’t likely. When using joysticks,
programmers like to program a “dead zone” around the
center of the joystick. This code line
is that dead zone:
if ((Xval > 500)&& (Xval <
520)&& (Yval > 500)&& (Yval <
Here, I basically give 10 ADC
“counts” to either side of the exact
center as my dead zone that I will
call the center of the joystick.
Depending on your hardware, you
may need to make it wider, or you
may get lucky and be able to make it
narrower. It just depends.
That is all there is to it! The
Arduino “sandbox” makes it easy to
write quick code that does what you
want without needing to know all
the nasty little details that go into
writing embedded software.
I made some guesses and
assumptions with this code. You may
need to tweak it a little if the digital
output lines I chose are being used
by another shield or for other things
in your final program.
Well, that’s it again for another
month. I hope you were inspired,
and go out and make something
new and fun for yourself! As usual,
keep those questions coming to
roboto@servomagazine com and I’ll
do my best to answer them. SV
Did You Know Preferred
Subscribers get access to all
the digital back issues of
SERVO Magazine for free?
Call for details
12 SERVO 03.2016