The code: The pre-placed code blocks set up the digital pins for the LED lights and the joystick's button and the analog pin A0 for the joystick's Y pin. It also initializes a variable named "movingforward" to keep track of when the robot is moving forward (value of 1), stopped (value of 0), and moving backward (value of -1). As before, the code uses a loop that reads the joystick's B pin until it gets a nonzero "high" or "1" signal, indicating that the joystick button was pushed. The code then ends. While the loop is running, the code inside the loop uses a conditional "if-elseif-else" statement to check the state of the joystick's Y value. If the Y value is greater than 712, and if the robot is not already moving forward, it sets the robot speed to positive 4 in/sec and issues a driveForeverNB command (and changes the robot's LED to green). If the Y value is less than 312, and if the robot is not already moving backward, it sets the speed to -4 in/sec and issues a driveForeverNB command (and changes the robot's LED to red). Otherwise, it stops the robot and changes the robot's LED to blue (the else clause). Connections (see diagram below): The joystick has five pins: Y, X, B, VCC, and GND. Using male-to-female wires, connect the VCC and GND pins to the 5V and one of the GND pins, respectively, in the Power section of the Arduino. Connect the B (button) pin to digital pin 2 on the Arduino. Connect the Y pin to analog pin A0. For this activity we don't need the X pin (the blue wire).
|
Grid Size: S M L | Simple View: | MinView: |
Coord: x-axis y-axis | Grid Lines: x-axis y-axis | |||
Grid: | 12x12 inches | 24x24 inches | 36x36 inches | |
72x72 inches | 96x96 inches | 192x192 inches | ||
1x1 inches | 2x2 inches | Fraction: | ||
xy-Range: | ||||
|
||||
|
||||
Quad: | 1 Quadrant | 4 Quadrants | 1&4 Quadrants | |
Units: | US Customary | Metric | Ruler: |
Labels: |
|
|
|
Font
px
|
|
Tics Lines: |
|
Width
px
|
Hash Lines: |
|
Width
px
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
|
Modify the code so that the robot uses different colors (not green and red) for its LED when moving forward or backward. |
|
|
|||||||||||
|
|||||||||||