If Statements!

Decisions, decisions.

Introduction

Programs are much more fun when they can decide what they are going to do to suit the situation. In this activity you will learn about if statements by building a simple security device which will allow you to know if someone has used your things or not.

What we are going to build

In the movie Indiana Jones and the Raiders of the Lost Ark there is a scene where Indiana is recovering a Golden Idol from a temple in Peru. The idol sits on a pillar which detects if it has been moved and enables a series of measures to try and prevent the idol theif from leaving the temple. In this activity we are not quite going to reproduce this but we are going to make a simple device which we can place on top of an item. The micro:bit will be able to detect if the item has been taken or not and will let us know.

We are going to code the micro:bit to make use of its built in accelerometer to detect if it has been moved or not. We can then place our micro:bit on top of the item we don't want others to use. The micro:bit won't be able to stop them from taking it but it will be able to tell us if they have.

Here is an algorithm representing what you will build in the rest of this tutorial:

Security Device

  1. ON Start
  2. Show a start up animation
  3. Set the variable armed to false so that the device begins as off.
  4. Set the accelerometer range to 2g
  5. ON button A pressed
  6. Wait for 1 second
  7. Set the variable armed to true
  8. Show a happy face icon to indicate the device is armed and running.
  9. ON button B pressed
  10. Set the variable armed to false
  11. Show a square icon to indicate the device is off.
  12. FOREVER
  13. IF the variable armed is true then
  14. Set the variable acceleration to the current acceleration strength.
  15. IF the value of acceleration is less than 980 then
  16. Show the unhappy face icon
  17. ENDIF
  18. IF the value of acceleration is greater than 1050 then
  19. Show the unhappy face icon
  20. ENDIF
  21. ENDIF

Concepts

In this activity we will be making use of the following concepts which you should have learnt about before:

  • Variables
  • Input through buttons
  • Output via the screen and the use of icons

We will be introducing the following concepts:

  • Decisions (If statements)
  • Detecting acceleration
  • Techniques to calibrate your code

Before we begin

Before we start actually building our security device we need to build a quick little program to help us find out what values to expect from our accelerometer. We need to test for these values as every micro:bit will be slightly different and we will need values specific to our device.

We are going to use the acceleration block to obtain our micro:bit's acceleration. Knowing what values to expect is important if our program is going to work properly. Create the program below and copy it onto your micro:bit.

acceleration reconnaisence

Make sure you change the default values for the two pink blocks to the values specified in the picture above.

If you plug the battery in and set your micro:bit on a table you should see a value repeating on the screen which is probably slightly larger than 1000.

Let the micro:bit run for a while and take note of the largest and smallest values which scroll across the screen. Note these down somewhere as we'll be using them later on.

A value of 1000 might seem odd when the micro:bit is sitting perfectly still. Surely if it is not moving the value should be zero. What we have to realise is that gravity is always affecting everything. It's why we aren't floating around right now. When stationary, every object experiences 1 unit of gravity and that is what the accelerometer in the micro:bit is recording. Because the accelerometer is reasonably fine grained it can record the acceleration in thousandths of a unit. The accelerometer is not perfect and every micro:bit will be slightly differect in the results it returns. The results will fluctuate a little too because of that innacuracy. This is why we want to test our micro:bit first rather than just assuming values.

Keep in mind this technique of writing a simple program to just continually print values out from a particular input. It is an effective way to help you understand how a particular component on the micro:bit works and get an idea of what sort of values to expect from it.

So why did we need to change the range to 2g (from 1g) and the acceleration to strength (from x)?

The range cuts off the maximum value that the acceleration block will return. With it set to the default of 1g if the micro:bit experiences anything stronger than that it will just return 1g. A lot of the time this is fine but in our case here we want to know if it does go higher. (if the micro:bit is dropped the value will decrease, if the micro:bit is lifted the value will increase).

The accelerometer has several values you can set for it. With the default value x it will only tell you about acceleration of the micro:bit left and right. By changing it to strength it will tell us the general acceleration of the micro:bit in any direction.

On Start - Setting things up

Let's start building our security device.

Two variables are required for this program:

  • armed - which we will use to control if the security device is active or not.
  • acceleration - which we will use to record the acceleration at a given point in time.

First off let's open up the Variables panel in the editor and Make both of these variables.

Now create the following:

The Start block

Notice that we moved the show number block from forever to an on shake event. We did this so that if we want to check our readings for the accelerometer again later on we can easily switch the blocks between our forever and on shake.

Moving blocks into an unused event block like this is an effective way to put code aside that we might want later on but don't need right now.

3 blocks have also been added to our on start event. The first two show icon blocks to give our device a nice intro animation to let us know that our device is on and working properly. The third block initialises the variable armed to false so that when we add the relevant code later on for our security device it won't start operating until we tell it do so.

On button presses - Arming and De-arming the device

We will use the button presses to arm and de-arm the micro:bit security device. Add the following code to your project.

On Button A

First off we set a delay of 1 second before we do anything else. This is because we will inevitably move the micro:bit in pressing the button. If we started checking straight away we would trigger the device (It's very hard to press one of the buttons without moving the micro:bit).

After the wait we change the value of the variable armed to true which will enable checking of the accelerometer later on.

Finaly, print a happy face to indicate that the device is running and so far all is good.

Now add a second block for the B button:

On Button B

By setting the value of the variable armed to false the code lower down will stop checking for movement.

An icon is also printed to let the user know that the device is no longer active.

Forever - Checking for movement

The Forever block takes whatever commands are within it and repeats over and over again continuously whilst the micro:bit is running. We will make use of it to continually check the movement (or acceleration) of the device.

Add the following code to your program (see the image further below for how to create the else if):

Forever

The values 980 and 1050 in the picture above are the values I used for my micro:bit. You need to replace them with a value 50 below your smallest value and 50 above your largest value that you recorded in your little experiment at the beginning.

50 is just a starting point though. You will need to experiment with the values to get the level of sensitivity you are happy with. To make your device less sensitive, move the values away from your recorded numbers. To make the device more sensitive, make the values closer to your recorded numbers.

We start off with an IF block. An IF block has two components, a condition and a set of commands. The commands will only be run if the condition is true. In our code we have said that we only want to check the accelerometer if the variable armed is true. In this way we can control from our button presses if the security device is running or not.

Within the main IF block we check if our device has moved or not. If it has moved then we are unhappy.

First off we record the current acceleration of the micro:bit and store it in the variable acceleration. Following on from this we check IF the value of that variable is above or below the range of values we recorded earlier when our micro:bit was sitting stationary on the table.

This IF block has been modified to have an else if section. If the first condition is false then the code directly under it will not run. Instead the system will check if the next condition is true and if it is then it will be run instead. To modify the IF block click on the little blue gear in its top left corner and drag the else if block under the IF on the white panel. Once you've done this, click on the blue gear again to close it.

Else If

In the first section of the IF block we want to see if the value of the acceleration is less than a particular value. We should set this value to be about 50 less than the lowest value we recorded in our earlier testing. We won't make it exactly the same as then it would be too precise and the chances of accidentally triggering the device would be very high.

In the second section of the IF block (else IF) we do the same thing but this time we check if the value of the variable acceleration is larger than the largest value we recorded earlier. Again we should add about 50 to the value to give it a buffer.

Summary

If Statement
Allows you to perform certain actions only if a certain condition has been met.
Accelerometer
Returns the effect of acceleration due to gravity on the micro:bit.
Testing code
A small piece of code which you write which is not part of your final program but is used to help you understand something.

The completed program

This is what your completed program should look like.

Full program

Activities

Now let's play about and further our abilities.

  • Maybe you can create a case for your micro:bit to make it look a bit more professional.
  • If all you have to do to de-arm the device is press button B it's not very secure. Can you improve your program so that a sequence of button presses is required to de-arm the device?