-
Book Overview & Buying
-
Table Of Contents
Design Innovative Robots with LEGO SPIKE Prime
By :
For this build, we are going to focus on writing code that allows us to use the Intelligent Hub as a remote control for the arm. This will be a program that allows you to control the robot using the gyro sensor feature of the Intelligent Hub to move the claw left, right, up, and down, and to open and close the claw.
Overall, this program is simple to develop and is created to be easily adaptable for you to remix to meet your needs. Use this sample code to make sure everything works, and then begin to tweak the code to make it work to your desired needs. Make sure you have the proper ports plugged in, and then move on to the code.
If you have not plugged in your motors yet, then let's get them plugged into the proper ports. You will plug the large motor that moves the claw to the right and left to motor port E. The medium motor that controls the opening and closing of the claw will plug into port A. The medium motor that controls the arm of the claw to go up and down will use port C. Once you have the motors plugged in, then double-check that the motors are plugged in correctly.
You can check that your motors are properly plugged in by checking the Hub Connection in the software, as seen in the following screenshot:
Figure 2.54 – Hub Connection view
It is time to write the program so that you can control the claw.
To get started with this program, let's go ahead with the following steps to make the robot come to life:
Click on New Project at the center of the main screen:

Figure 2.55 – Choosing a new project

Figure 2.56 – Creating the initial code stack

Figure 2.57 – Yaw is turning Intelligent Hub left and/or right on the z axis

Figure 2.58 – Set motors E and C to the start position
We are now going to create a new code stack for the claw to close. Go to the Events code block section and select the block when left Button pressed. Change this block to right Button pressed. Add a blue motor block named run for 1 clockwise rotations. Change the settings of the block to motor A and change the settings to 7 degrees:

Figure 2.59 – Set the claw to close
You will now copy this stack and do the opposite to open. Right-click the Event block and you will see an option to Duplicate. Change this stack to left Button on the orange event block. For the blue motor block, change the direction of the turn to counterclockwise:

Figure 2.60 – Set the claw to open
forever. In this block, add another blue motor block, moving motor E to the shortest path to position the block. Where the number 0 sits, you will add a cyan sensor block named Pitch Angle. Add that to 0 and then change the pitch to Yaw. Figure 2.61 will showcase what you should have at this point:
Figure 2.61 – Controlling arm code
If Then statement and basically duplicate it two more times to complete the code. This will program the arm to follow the motion of Intelligent Hub. Go to the Control blocks and add an If Then block into the Forever block of our code. Add a green Operator block of less than. On the left side, add a cyan Sensor block of Pitch Angle, and to the right of the equation, add -3. Under that block, add a blue motor block, Start Motor, and change to motor C and in a counterclockwise direction:
Figure 2.62 – Arm move to the left
greater than and another Operator block when the pitch is between -3 and 3. See Figure 2.63 for the rest of the code to complete the sequence:
Figure 2.63 – Complete code for arm control
When it is all complete, your code should look like this:
Figure 2.64 – Complete code for this project
And that is it! You did it. You built a nice-looking and working robot claw. Now comes the fun part, which is to remix the claw to make it your own. Let's look at some ideas.
Change the font size
Change margin width
Change background colour