Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

• Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

• Learn & Share: Exchange tips and tutorials to enhance your skills.

• Exclusive Previews: Get early access to new product announcements and sneak peeks.

• Special Discounts: Enjoy exclusive discounts on our newest products.

• Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

ðŸ‘‰ Ready to explore and create with us? Click [here] and join today!

# Dual Joystick ModuleÂ¶

We can control PiArm in 2 parts, Arm and EoAT. In the first project, you have learned how to Test 3 EoATs of PiArmâ€™s separately.

In this project, first the arm is controlled by Angle Mode and dual joystick module. Then the control code for the three EoATs was added to this so that the dual joystick module can control both arm and EoAT.

## Arm - Joystick ControlÂ¶

PiArmâ€™s arm can be controlled in two ways: Angle Mode and Coordinate Mode.

• Angle Mode: Writes a certain angle to the three servos on the arm, thus rotating the arm to a specific position.

• Coordinate Mode: Create a spatial right-angle coordinate system for the arm and set the control point. Set the coordinates of the control point so that the arm can reach a specific position.

Step 1

You may want to simplify your program with variables, now click the Create variable button on the Variables category to create 5 variables (`HIGH`, `LOW`, `Î±`, `Î²` and `Î³`).

Note

The created variables are also stored in the Variables category.

Step 2

Set the initial values for these variables and set the servo rotation speed to 70%.

Note

For the reason of the values of the HIGH and LOW variables, please refer to Dual Joystick Module.

Step 3

Use [if else] block to do some conditional judgment cases (drag 5 [else if] blocks from the left to below the [if] block).

• [if else]: Conditional judgment block, you can create multiple conditional judgments by clicking the set icon and dragging [else] or [else if] to the right below the [if].

Step 4

The left and right joystick connections for the dual joystick module are shown below, refer to Dual Joystick Module.

• The X of the left joystick is connected to A0 and the Y is connected to A1.

• The X of the right joystick is connected to A2, and the Y is connected to A3.

Assume that the X and Y of the left joystick and the Y of the right joystick are used to control the 3 servos of PiArm respectively, now first set the judgment condition to determine whether the left and right joysticks are toggled or not.

• If A0 (LX) is greater than HIGH (3072), it means that the left joystick is toggled to the right.

• If A0 (LX) is less than LOW (1024), it means the left joystick is toggled to the left.

• If A1 (LY) is greater than HIGH (3072), it means the left joystick is toggled forward.

• If A1 (LY) is less than LOW (1024), it means the left joystick is toggled backward.

• If A3 (RY) is greater than HIGH (3072), it means the right joystick is toggled forward.

• If A3 (RY) is less than LOW (1024), it means the right joystick is toggled backward.

Step 4

Now set the rotation effect of PiArm according to the toggle of the left and right joysticks.

• If the left joystick is toggled to the right, the Arm will turn right.

• If the left joystick is toggled to the left, the Arm will turn left.

• If the left joystick is toggled forward, the Arm will extend forward.

• If the left joystick is toggled backward, the Arm will retract backward.

• If the right joystick is toggled forward, the Arm will lower down.

• If the right joystick is toggled backward, the Arm will raise up.

Note

• `Î±`, `Î²` and `Î³` represent the 3 servo rotation ranges on PiArm, refer to: Angle Mode.

• [constrain () low () high ()]: From Math category for setting the variation of a constant to a certain range.

Step 5

Store the obtained `Î±`, `Î²` and `Î³` angle values into the [Î± () Î² () Î³ ()] block, and then use the [set positon] block to make PiArm rotate this position.

Step 7

Once you click the download button, you can use the Dual Joystick Module to control PiArm.

• Left joystick toggle left or right, the arm will turn to the left or right.

• Left joystick toggle forward or backward, the arm will extend forward or retract backward.

• Right joystick toggle forward or backward, the arm will raise up or lower down.

Note

You can also find the code with the same name on the Examples page of Ezblock Studio and click Run or Edit directly to see the results.

## Shovel Bucket - Joystick ControlÂ¶

Now add the control code for the Shovel Bucket.

Note

You can also find the code with the same name on the Examples page of Ezblock Studio and click Run or Edit directly to view the code.

Once the code is run, you can control both the PiArmâ€™s arm and Shovel Bucket with the dual joystick module. But you need to install Shovel Bucket to the PiArm first.

• Push the left joystick to the left or right, the arm will turn to the left or right.

• Push the left joystick forward or backward, the arm will extend or retract.

• Push the right joystick forward or backward, the arm will be raised or lowered.

• Push the left joystick to rewind the Shovel Bucket inward.

• Press the right joystick to extend the Shovel Bucket outward.

## Hanging Clip - Joystick ControlÂ¶

Now add the control code for the Hanging Clip to the code for the control arm.

Note

You can also find the code with the same name on the Examples page of Ezblock Studio and click Run or Edit directly to view the block.

After the code is run, you can use the dual joystick module to control PiArmâ€™s arms and vertical clips at the same time. But you need to install Hanging Clip to PiArm first.

• Push the left Joystick to the left or right, the arm will turn to the left or right.

• Push the left Joystick forward or backward, the arm will extend or retract.

• Push the right Joystick forward or backward, the arm will be raised or lowered.

• Press the left Joystick to close the Hanging Clip.

• Press the right Joystick to open the Hanging Clip.

## Electromagnet - Joystick ControlÂ¶

Now add the control code for the Electromagnet to the code for the control arm.

Note

You can also find the code with the same name on the Examples page of Ezblock Studio and click Run or Edit directly to view the block.

After the code is run, you can use the dual joystick module to control both PiArmâ€™s arm and the Electromagnet. But you need to install Electromagnet to PiArm first.

• Push the left joystick to the left or right, the arm will turn to the left or right.

• Push the left joystick forward or backward, the arm will extend or retract.

• Push the right joystick forward or backward, the arm will be raised or lowered.

• Press the left joystick to turn on the Electromagnet.

• Press the right joystick to turn the Electromagnet off.