Let's get movin'!
Hopefully you warmed up with the first sketch so, let’s create another one. This sketch will make your Wheelson move in a random direction every second.
The first steps are the same as with the first sketch. Click on “New Sketch”, choose “Wheelson” as your device and “Block” as the Sketch type. After clicking “Create” the drawing board will open.
Let’s begin with a variable.
Click on the “Variables” section on the left side of the screen and then click on the “Create variable…” button. Once a new window appears, name your variable. Let’s name it “directionTime”. Click “Save”. With the variable created, choose the “set ‘directionTime’” variable and drag and drop it right below the “Arduino run first” text.


Now we’ll fill the empty space on the right side of the “set” function with a block from the “Time” section. Find the “current elapsed Time (milliseconds)” block and drop it into the empty slot.


We’ll add another variable. Just like before, create a new variable and give it a name. Let’s call this one “driving”. After creating the variable, drag and drop this new “set” variable right below the first “set” variable we placed. It should look like this:

The right spot in the “set” variable is empty so let’s fill it with the “false” block from the “Logic” section.


Let’s move on and add the “if” block from the “Logic” section. Add this block below the “Arduino loop forever:” text.

Next we’ll have to add a comparison block from the “Logic” section. Put this comparison block in place of the “true” block that’s in the “if” block. Then, select
the greater than or equal to symbol (≥) from the drop-down menu.


This comparison block is comparing two values so let’s set the first one on the left. Find the block with a “-” symbol between two numbers in “Math” section and drop it in the left value of the comparison block.


Good job! Now let’s set the values for this “Math” block we just inserted. In the “Time” section, find the block “current elapsed Time (milliseconds)” block - the same one we just a few moments ago and drop it in the left slot of the “Math” block. Just like this:

Next, go to the “Variables” section and find the block “directionTime” we’ll add it into the second empty slot of the “Math block.


Finally, let’s change the value we’re comparing this to from ‘10’ to ‘1000’ so see if the time that's passed is bigger or equal to 1000 milliseconds or 1 second.

We’ll add another “if” block now. You know the drill - find the “if” block in the “Logic” section. Then, drag and drop it into the empty slot of the first “if” block we added.

Let’s add the “driving” variable to the “if” block. You’ll find this in the “Variables” section.

It’s time to create a new function. Open the “Functions” section and click on the “Create a Function” button.

A new window will open up. Let’s change the function’s name to “changeDirection” and click the “Done” button.
This new function will then automatically show up on the drawing board.


Let’s create a new variable and call it “speedLeft” then, drag and drop the set variable inside the “changeDirection” function we just created.

Then, from the “Math” section get the “random integer from” block and drop it in the “set” variable.

We’ll also change the values in this “random integer from” to “20” and “120” to instruct Wheelson to move at a speed of any random number from 20 to 120.
Check the photos below to see how it looks like.
Bear in mind that Wheelson’s speed goes from -127 to +127. Negative values will turn Wheelson’s wheels backward, and positive values will turn them forward. To stop the wheels from turning use value “0”.

Now, create another new variable, call it “speedRight” and repeat these steps until you have something like this:

We’ll now take an “if” block from the “Logic” section and drop it right below the “set” variables we just added.

Let’s add a comparison block to the “if” block. Find a comparison block with “=” symbol in the middle. Drag and drop it into the “if” block.


Open the “Math” section on the left side of the screen and find the “random integer from” block again.
Drag and drop it in the value on the left side of the comparison block.
Change the values of the “random integer from” to “0” and “1” and compare it to “1”. Just like this:

Moving on, we’ll add a “set” variable in the empty space of the “if” block. If it isn’t already selected, select the “speedLeft” variable from the drop-down menu.

From the “Math” section, drag and drop a block with the “x” symbol in the middle.


We’ll add the “speedLeft” variable in place of the first value, and “-1” in place of the second value of the “Math” block we just added.

Let’s add another “if” block and repeat these steps but this time with the “speedRight” variable instead of the “speedLeft” variable. Just like this

Now, open the “I/O” section and find the “set motor speed” block. Drag and drop it right below the “if” block we just finished with.


We’ll replace the value “100” in this block with the variable “speedLeft”.

Add three more “set motor” blocks and select the correct values from the drop-down menus just as shown below:

We don’t want your Wheelson to go crazy moving around randomly so we’ll set him up to start moving only after we press a button. Let’s choose to trigger the movement when the middle button on Wheelson is pressed.
Open the “I/O” section and drag and drop the “when ‘button’ gets” block to the drawing board. Then, select the “middle” button from the drop-down menu.


Grab a “set” variable and place it in the empty slot of this purple block we just added. Choose the “driving” variable from the drop-down menu.

Next, we’ll add a block from the “Logic” section. Find the “not” block and place it in the “set” variable.

Fill in the empty space of the “not” block with the “driving” variable like so

From the “Logic” section, let’s add an “if” block right below the “set” variable, and add the “not” block with the “driving” variable. Take a look at the image below:

Open the “I/O” section and find the “set motor speed” block. Drag and drop it inside the “if block. Set the speed value to “0” to stop Wheelson's motors.

Repeat this step three more times for the remaining motors just as shown in the image below.

We’ve finished with the “changeDirection” function so let’s return back to the “Arduino loop forever:” text and drag and drop the “call changeDirection” block from the “Functions section in the empty slot of the “if” block.


Now, grab the “set” block from the “Variables” section, drop it right below the “if” block and choose the “directionTime” variable from the drop down menu. Also, right next to this, add the “current elapsed Time (milliseconds)” block from the “Time” section.

This is how your sketch should look after finishing.

Save and name your sketch then, click the “Run” button to test the code we’ve just created.
If you’ve done everything correctly, your Wheelson should move in a random direction every second after you press the middle button.
Is everything working? Great! Let’s move on to another example.
If the project doesn't work when you try to Run it, go through the sketch one more time.