logica-bitbloq

If, equal, and/or, different from, not true…

Today we´re going to learn how the Logic  blocks work.

There are four blocks, each with different options.  Let´s see what they´re all about!

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
  • A ZUM BT 328 controller board or one compatible with Arduino UNO
  • A USB cable

The comparison block

 

Ring any bells? We have used this block on many occasions, together with the If…do (Si…ejecutar) control block. This block returns true or false, depending on the two inputs, which could be:

  • = both inputs are equal.
  • = both inputs are different.
  • < the first input is smaller than the second.
  • ≤ the first input is smaller than or equal to the second.
  • > the first input is greater than the second.
  • ≥ the first input is greater than or equal to the second.

As a result, along with the If…do block, we can set conditions so that our program executes one command or another, according to our choices. Let´s take a look at an example:

Example using the comparison block

Making an LED light up when the potentiometer value is less than 500

If the potentiometer value is less than 500, this means that the LED will light up if the value is between 0 and 499. On the other hand, the LED will go off at values between 500 and 1023 (remember than a potentiometer can have values between 0 and 1023).

The and/or block

This block can be used to check several conditions at the same time:

  • And option: Both conditions must be fulfilled for the value to be true and the action to be executed.
  • Or option: At least one of the conditions must be fulfilled for the value to be true and the action to be executed.

Now we´ll look at an example so that you can experiment and tell the difference between the And and Or options:

Example using the and/or block

Making the buzzer sound when the following conditions are met:

ejemplogicaY_en2

This time, the buzzer will only sound when both conditions are met: If the light variable is 300 or more and we press the button, the buzzer will sound. If either condition is not fulfilled, that condition is therefore not true and the buzzer will not sound. Let´s try something new – change the previous program so that you use the Or option instead of the And option. What happens now?

Now your buzzer will sound less often, as we are telling the program the following: If the light variable is 300 or more or we press the button, the buzzer will sound. This means that any of the two conditions will do. Can you see the difference?

The true/false block

This block returns the true or false value. Here´s an example to make it easier to understand:

Example using the true/false block

Making a program so that the LED lights up if the light is 300 or more, or goes off if it is less

Remember this program right? Today we´re going to do things a bit differently:

The lots_of_light variable will read the light sensor value and if it is 300 or more, the variable will have a true value, or a false one on the contrary. We can then compare the value of the variable directly using the true block, if it is true, there will be lots of light and the LED will light up.

The not block

This block allows us to negate a variable or state, for example, if we put a not on the true one: Stating that it is not true is the same as stating that it is false. Let´s take a look at a quick example:

Example using the Not block

Making a program so that the buzzer sounds if the button is pressed using the not block.

This program is a modified version of the other one we made a while ago: The button_not_pressed variable will be true as long as the button is not pressed. If we want the buzzer to sound, our condition is: If not variable button_not_pressed. This means that if the negation of the button not pressed is true, or in other words, if the button is pressed, then the buzzer will sound. As you will probably remember, there are many ways to carry out this program and this is simply an example to help you to understand how the not block works. There will be times when negating the value of a variable will prove to be very handy! Logic is fundamental to making your program work well and if you get used to logical reasoning, things will start to work like magic. The difference will be that you will be the magician and you will know exactly where it is and how the trick works. So whenever you start a project, remember this:

“There is something more important than logic: imagination” Alfred Hitchcock.