| This lesson describes how to use a joystick to drive a Linkbot robot. |
|
|
In a previous lesson we learned how to use a joystick to control an LED on an Arduino. With a few modifications, we can use that code to drive a Linkbot robot. That is, we want to write a program that will do the following:
To connect the joystick to the Arduino, see this lesson. We don't need to connect the Linkbot to the Arduino, but it does need to be connected to RoboBlocky in the usual way (using the wireless dongle).
2. Read the Status of the Joystick's Y Pin The circuit diagram for the joystick and the Arduino shows that the joystick's Y pin is connected to analog pin A0 of the Arduino, To read the status of the Y pin, we use the analogRead block, set to pin A0. We use a variable named yvalue to store the result:
After pin A0 is read, the value of the yvalue variable will be an integer value from 0 to 1023.
3. Using the driveForeverNB Block to Drive the Robot The basic logic in using joystick to drive the Linkbot forward or backward is similar to what we used in controlling the LED:
When we want to drive a Linkbot we often use code blocks like driveDistance or driveTime, where we specify the distance or time to be driven. But in this case we can't specify an ending distance or time in advance. When the user moves the joystick forward, we want the robot to keep driving until the user moves the joystick back to neutral, and we don't know how long that will be. The solution is to use the driveForeverNB code block:
As the name impllies, when RoboBlocky executes a driveForeverNB block, it starts the robot driving. It then continues on with whatever code comes next, while the robot continues driving. The robot will only stop driving when RoboBlocky executes a stop command: The driveForeverNB block will drive the robot forward if the robot speed is set to a positive value (such as 4 inches/second, using the setSpeed block), and it will drive the robot backward if the speed is set to a negative value (such as -4 inches/second).
4. Putting It All Together Putting it all together, we use a conditional if-elseif-else statement to check the state of the joystick's Y value and move the robot forward or backward accordingly.
But there's one complication we have to take into account: if the robot is already moving forward, or already moving backward, we don't need or want to issue the driveForeverNB command. (Doing so might lead to some stuttering effects in the robot movement.) So we modify the logic as follows:
So by checking the current value of the moving variable, it can can tell whether and how the robot is moving. Example 1 puts it all together.
5. Adding Turns Example 2 shows how we can add turning capability to the code by reading the X value of the joystick. If the X value is greater than 712, then the code turns the robot right 90 degrees. If the X value is less than 312, then the code turns the robot left 90 degrees.
|
| joystick
|
Write a program that uses the joystick to drive a Linkbot forward and backward. Reminder: Make sure that ChDuino (for WIndows and MacOS machines) or Arduino Controller (for Chromebooks) is active before running the program. |
|
Write a program that uses the joystick to drive a Linkbot forward or backward, or turn it left or right. |
|
| 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
|
|
|
|||||||||||
|
|||||||||||