In Codeblocks, you may put expressions in slots that hold a value. We illustrate this using a Box Shape and adding a Move codeblock to traverse the Box along the X-axis. The value of the Move’s X parameter will contain an expression.
By using Codeblock’s simulator (pressing the Run button), the value of the expression can be seen as the distance the Box moves along the x-axis.
The expression we use is simple: 22 ⁃ 6 * 2
This can be evaluated as a) 22 ⁃ (6 * 2) = 10 or
b) (22 ⁃ 6) * 2 = 32
We show how to implement both a and b below.
Sign-in to Tinkercad. Go to Codeblocks and start a New Design.
Drag in a Box shape and press the Run button to execute Codeblock’s simulator. You should see something similar to this:
Drag in a Move codeblock to get:
We want to set the value of X: to the expression 22 ⁃ (6 * 2).
Since we have two operators, we need to use two AddOperator codeblocks. The AddOperator codeblock is found under the Operators button. Click on the pull-down menu to select the operation you want to execute.
Drag this into the slot for X: in the Move codeblock above.
The new value for X: is shown below as the expression 0 + 0
Now we can set the first slot to 22 and use the drop-down to set the operator to minus. We now have:
We need another AddOperator codeblock in the slot after the minus sign. We drag it in and get:
We put in 6 * 2 to complete the expression as shown below.
If you have good eyes and look closely at the expression you can see that 6 * 2 is enclosed in an oval. That oval is inside another oval that encloses the whole expression. The inner-most operations are executed first.
In the case above, 6 * 2 is executed first.
I hope that Tinkercad improves the visibility of these ovals.
Using the Simulator to Visualize Movement Along the X-axis
We can use the simulator to see that the Box moves the value (10 units) of the expression 22 ⁃ (6 * 2).
I added another Move codeblock to raise the box so it sits on the workplane surface.
Set the Speed of the simulator to its lowest value.
Now press the Run button to watch the box move-up 10 units and then move 10 units to the right.
Press the link below to see the simulation on a YouTube video.
The Codeblocks simulator is a useful tool for debugging your Codeblocks!
Inserting the expression (22 ⁃ 6) * 2
We add an AddOperator codeblock in the X: slot of a Move Codeblock.
Now we set the operator to times and put in the value 2 in the slot after the times operator.
Add another AddOperator codeblock to the first slot.
Put in the values 22 ⁃ 6
Your codeblocks should now look like:
Note that 22 ⁃ 6 is enclosed in the innermost oval and thus is done first.
You can use the Codeblocks simulator (as shown above) to visualize the movement along the X-axis.