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!
GAME - Iron Collection¶
In this project, prepare 3 shapes of iron pieces: triangle, circle and square, PiArm will randomly say a shape, you need to control PiArm to put the corresponding shape of iron pieces into the corresponding box in the specified time, you will not be able to control PiArm when the time is over.
Programming¶
Step 1
Create five variables (α
, β
, γ
, flag
, shape
) and and set initial values for them. Then initialize the PiArm rotation speed and Electromagnet pin.
Note
α
,β
andγ
represent the 3 servo rotation ranges on PiArm, refer to: Angle Mode.
Step 2
Drag 2 D-pads from the remote control page to control PiArm, a button to start the game, and a Digital Tube to display the time.
Step 3
Create a function named [magnet] to enable the left and right control of the D-pad B to turn the electromagnet on and off.
Step 4
Create a function named [control] to implement the Arm of PiArm to be controlled by the D-pad A and D-pad B.
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.
[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].
If the UP button (â–²) of D-pad A is pressed, the Arm will extend forward.
If the Down button (â–¼) of D-pad A is pressed, the Arm will retract backward.
If the LEFT button (â—€) of D-pad A is pressed, the Arm will turn left.
If the RIGHT button (â–¶) of D-pad A is pressed, the Arm will turn right.
If the UP button (â–²) of D-pad B is pressed, the Arm will raise up.
If the Down button (â–¼) of D-pad B is pressed, the Arm will lower down.
Step 5
Create the function [say_shape] to have PiArm speak a random shape.
Step 6
The main flow of the code: when button E is pressed, the timer starts and PiArm will say a random shape. flag
is used to represent the start of the countdown and you can control PiArm.
Step 7
Create a function named [timing] to use for timing. The game time is set to 60 seconds, after the time is up, PiArm will say Game over and you will no longer be able to control it.
Here the [time] block is used for timing, in Forever, when button E is pressed, the timing starts and [time - startTime] represents how many seconds have passed since then.
Step 8
Let the [timing] function run in a separate thread. This allows you to control PiArm while counting down.
The complete code is as follows: