9 + 3 makes 12, one carried over…
Today we´re going to learn how the blocks of the Maths tab work. Short of hands for crunching numbers? Then make bitbloq do the work for you! Let´s have a look at what the maths blocks are made up of:
For today´s examples, you will need:
- 1 x ZumBloq LED
- 1 x ZumBloq potentiometer
- 1 x ZumBloq buzzer
- 1 x ZumBloq push-button
- 1 x ZUMbloq light sensor
- 1 x ZumBloq joystick
- 1 x ZumBloq mini servo
- 1 x ZumBloq LCD
- A ZUM BT 328 controller board or one compatible with Arduino UNO
- A USB cable
A number block
This is one of the blocks that you will be using the most in bitbloq. This block enables you to write a numerical value, for example, to compare its value in an if…do block, to assign an element directly to a pin, to set a time or to make calculations… this block has a vast range of uses.
Example using the number block
Making the buzzer sound for 300 ms
We have used the number block to make the buzzer sound for 300 ms. The number block is known internally as a whole number. This means that it has no decimal places. It´s important to remember this when making calculations.
The array block
Remember how we used this block with the joystick? An array is nothing more than a container or vector that unites three individual variables. Examples are the X and Y coordinates and the button of our joystick, the hours, minutes and seconds of a clock, etc. There´s something really important to remember when working with vectors. The first element of a vector corresponds not to index 1, but in fact, to index 0. This means that if you want to access the first element of a vector, you need to put 0 instead of 1, and if you want to access the second, you put 1 instead of 2, etc. The diagram will help make this clearer for you:
Let´s have a look at an example that uses the array block:
Example using the array block
Displaying the joystick values on the LCD screen
In this case you will see the X and Y values and the button of the joystick on your LCD screen. By using the array block, you will have this information stored in an organised way, which you can easily retrieve.
The random between… and… block
It can be very useful to have a random value sometimes. Why don´t you have a go at creating a Simon says game with this block? Or maybe you want to make a robot dance or move spontaneously… there are many possibilities.
The map blocks
These blocks can be used to change the range of a component and adapt it to another scale. For example, the potentiometer returns a whole number between 0 and 1023, the mini servo between 0 and 180 and the LED between 0 and 255. If we want to use a potentiometer together with a mini servo between 0 and 180 degrees, the ideal thing would be to used the map block to synchronise both scales. The first block allows us to map a variable going from 0 to 1023 to the range we want. The second block gives us a more freedom by allowing us to select the range of both the input and output variable. Let´s see why mapping is useful:
Example using the map block
Mapping the potentiometer to the range of the mini servo (o – 180 degrees)
We are therefore dividing the range of the potentiometer, from 0 to 1023, into 180 parts, so that roughly every 6 potentiometer values, the mini servo will go forward by one degree. Without this mapping, we wouldn´t be able to control the mini servo and the potentiometer correctly. This program is equivalent to the one below, which uses the advanced mapping block:
The +, -, x, /, ^ calculation blocks
You can use these blocks to make simple mathematical calculations, such as adding, subtracting, multiplication, division or exponentiation. You can use them in the number block or you can also make calculations using variables.
Example using the calculation blocks
Using the addition operator to light up an LED when the sum of the value detected by the light sensor plus 20 units is less than 100
Other mathematical calculation blocks
Example using other mathematical calculation blocks
Making a program that returns the logarithm with base 10 of a variable
In some programs it can be useful to make a mathematical calculation for modifying certain data. For example, we might want the program to calculate the logarithm with base 10 of the value of a variable.
The remainder block
Can you remember how to divide manually? If you can, you´ll remember that there´s often a remainder leftover, and you had to keep dividing, getting a decimal number. So this block returns the remainder of a division. Why would I want to use the remainder of a division? Have a look at the next example:
Example using the remainder block
Making the program return the value of the remainder from dividing the random number by 2 on pressing the button If the number is even (meaning that the remainder is equal to zero), the LED will light up. If the number is odd (remainder equal to 1), the buzzer will sound.
You can check if a number is even or odd by diving it by 2. If it the remainder is zero on dividing it by 2, the number will be even. If on the other hand, the remainder is a one, it will be an odd number. As you can see, we have also used the random block to generate a series of random numbers between 0 and 2000. So that´s the maths blocks for you. Maybe some of them don´t seem all that useful or you might think that you´ll never need to make such complex calculations. But as you become more and more of an expert and you start programming like a pro, you will start wanting more… I assure you!