Basic Lesson 1 Go Forward or Backward

Share for us


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:

  1. one wheel is rotating clockwise, and the other rotates clockwise at a lower speed;
  2. one wheel is rotating clockwise and the other wheel stops;
  3. one wheel is rotating clockwise and the other is rotating anticlockwise.

Programming Steps

Step 1

Click the Create variable button in the Variables category.

Step 2

Input the variable’s name: speed.

Step 3

Drag a set speed to block from Variables category into the Start block.

Step 4

Get a number 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).

Step 5

Click on PiMobile category and drag a Motor set Motor1 speed to 75 block into the Forever block.

Step 6

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.

Step 7

Long press the Motor set block and then click Duplicate to get an identical block.

Step 8

Click on the drop-down box next to Motor1; select Motor2.

Step 9

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).

Step 10

Next, let the car move backward. Referring to the previous steps, duplicate the Motor set Motor1 speed to block again.

Step 11

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 “”.

Step 12

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).

Step 13

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.

Step 14

Code for car to turn left is as follows.

Step 15

Finally, let the car stop moving.

Step 16

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.