Input Shield

advertisement
Input Shield
Once you are able to get the ball to move across the screen, it is a simple
implementation in order to get the paddles to move up and down on the
screen too. The toggle switch (joystick) on the input shield is what you
will use to control the user’s paddle.
 To do this, you must know the values at which the joystick is in
the up, down, and middle positions.
o Do this by writing a simple Arduino sketch that will read the
values given off by the joystick when it is moved, and print
those values on the serial monitor.
o The joystick uses analog pin 5, so use that for reading it’s
values.
o Then use the serial monitor to see for which values the
joystick is in the up position, middle position, and for which
values the joystick is in the down position.
 Once you know the values for all three of those unknowns, make a
variable, like you did for the ball, that is called something like
pxdir (paddle X direction). Also make a variable that will stand for
the X coordinate of the paddle (X will do nicely). These variables
will be used to update the status of the paddle’s position in order
that a new paddle can be drawn.
 Depending on which way you have your interface set up (which
direction with relation to the pixels of the screen is up and down)
will determine how the joystick values affect the pxdir value. The
way we have our Pong game set up is that when the joystick is up,
pxdir is set to 1, when the joystick is down, pxdir is set to -1, and
when the joystick is in the middle, pxdir is set to 0.
o By adding pxdir to X and updating the X variable:
 X = pxdir + X
o You can update the paddle’s position just by using the
variable X as the x pixel in fillRect(x, y, height, width, color)
instruction.
So once more, you will use the provided code to find the value of input
from the joystick, using the serial monitor, when it is up, down, and in
the middle. Use those values in your Pong code to set the paddle X
direction variable to either 1, -1, or 0. Use these values in conjunction
with the x pixel of the User’s paddle to update the position of the paddle
in the game.
Download