Introduction: How to Get Started With Variables in Codeblocks

How to teach codeblocks to students? How to explain the importance of variables and parametric design? Let's try to do it with this lesson. Let's begin!

Supplies

Tinkercad Codeblocks. A 3D Printer if you want to print the decoration.

Step 1: Getting Know the Variables

These decorations were all generated from the same code. How is it possible? Thanks to the variables. Variables can be changed at the beginning of the program or even when it is running. The variable blocks are located in the "Math" section and are three: "Create", "Set", "Change". When I create a variable, a block referring to it is added in the "data" section. I can use this block inside other blocks so as not to have to write a number every time but to use that of the variable.

Step 2: Choose a Basic Shape

Choose a shape that allows you to change the number of sides (to find out just take the block and click on the arrow to find out which parameters can be changed). For example, I chose the tube.

How to understand better the shape, manually change the number of sides.

Step 3: Analize the Decoration

Watch the animation of the complete decoration. What happen?

  1. The shape is created
  2. The shape rotates around the center of the axes, increasing the degrees of rotation each time.

Step 4: The Variables and the Loop

How many variables are there?

There are two: the number of sides and the angle of rotation.

Create two variables and call them "sides" and "angle"

The number of sides must be greater than 3 (do not go beyond 12 for now). The starting angle, on the other hand, is 0.

Add the repeat block by inserting the variable sides. Insert inside the shape with the side block corresponding to the number of sides.

Step 5: Rotation

The shape rotates around the z axis, according to the variable angle. The center does not have to be all at 0 otherwise the shape continues to rotate on itself. For example, change the y by the same number as the shape's radius.

Each time you create a shape, this rotates by a progressive angle, so as to form a full circle angle.

Then the variable modification block is used (the variable that is modified is the angle) and a mathematical operation is entered, it is 360 ​​/ sides.

Step 6: Explore!

Try changing the number of the “sides” variable to see different results!

To get other results try to insert the variable also in other parameters of the blocks, for example in the radius or in a coordinate of the rotation pin.

Or if you want the number to change randomly every time the program is running, you can add the "random beetween ..." block in correspondence with the variable number.