In this lesson, let’s learn how to program on Ezblock Studio to test whether the basic operation of the car is right, including go forward, backward, left and right, etc.
Set the speed value of motor to positive number, then the wheel will rotate clockwise; otherwise, it will rotate anticlockwise. Thus, when the two front wheels rotate clockwise, the car will move forward; if the two front wheels rotate anticlockwise meanwhile, the car will reverse. To make the car turn left or right, you can use any of three methods:
- one wheel is rotating clockwise, and the other rotates clockwise at a lower speed;
- one wheel is rotating clockwise and the other wheel stops;
- one wheel is rotating clockwise and the other is rotating anticlockwise.
Click the Create variable button in the Variables category.
Input the variable’s name: speed.
Drag a set speed to block from Variables category into the Start block.
Get a number 0 block from Math category and put it to the right of set speed to block, and change the value to 50. (The speed of motor is 50).
Click on PiMobile category and drag a Motor set Motor1 speed to 75 block into the Forever block.
Under the Variables category, find and drag the speed block to the position of 75, so that we can set the speed of the car.
Long press the Motor set block and then click Duplicate to get an identical block.
Click on the drop-down box next to Motor1; select Motor2.
Click into Basic category, then find the delay block. Drag it to the area under the Motor set Motor2 speed to block, and change the value to 1000. In this way, the car will move forward at the speed of 50 for 1000 ms (1s).
Next, let the car move backward. Referring to the previous steps, duplicate the Motor set Motor1 speed to block again.
Enter Math category, then get a square root 9 block and put it in front of speed block. After that, click on the drop-down box, and choose “–”.
Choose the Motor set Motor1 speed to -speed block to duplicate a same one, and then change the Motor1 to Motor2. Add on a delay block to let car go backward for 1000 ms(1s).
In the same way, write the code to let the car turn right.
Note: Motor1 is on the right and Motor2 is the left one when we face the tail of the car.
Code for car to turn left is as follows.
Finally, let the car stop moving.
Save your project，connect the Bluetooth and click Flash to compile and download the code. After “download successfully” appears, the car will move forward, backward, turn left and right for 1s circularly.