Microbit Sensors & Circuits
Intermediate
60 mins
Teacher/Student led
115 points
What you need:
Chromebook/Laptop/PC
Microbit

Creating a Microbits Alarm System

Embark on a journey to create your own Microbits Alarm System. Begin by setting up a new project, then establish threshold variables for sound and light. Learn to arm and disarm your alarm using Microbits buttons. Define an alarm function and set up triggers to activate your alarm when thresholds are crossed. Finally, test your system and adjust as needed.
Learning Goals Learning Outcomes Teacher Notes Lesson Files

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Create a New Project

    Start by creating a new project. Visit the Microbits MakeCode editor at the following link: https://makecode.microbit.org/. Click on 'New Project' and give it a name, such as 'Microbits Alarm System'.

    2 - Setup Threshold Variables

    In this step, you will create two variables: 'soundThreshold' and 'lightThreshold'. These variables will hold the threshold values for sound and light. When the sound or light detected by the Microbits sensors exceeds these thresholds, your alarm system will be activated.

    To setup these variables, you need to add the following code:

    let soundThreshold = 128
    let lightThreshold = 128

    The values '128' are the initial threshold levels for sound and light. You can adjust these values based on your environment and how sensitive you want your alarm system to be.

    3 - Arm the alarm

    In this step, you will create a variable called 'armed' and set it to false at the start. This variable will be used to determine if the alarm system is active or not. When you press the A button on your Microbit, it will set this variable to true, indicating that the alarm system is armed and ready to detect movement or sound.

    Start by declaring the 'armed' variable at the top of your code, outside any functions, and set its initial value to false:

    let soundThreshold = 128
    let lightThreshold = 128
    let armed = false
    

    Next, you'll create a function that will be triggered when the A button is pressed. Inside this function, you'll set the 'armed' variable to true. Add the following code:

    input.onButtonPressed(Button.A, function () {
        armed = true
    })

    The 'input.onButtonPressed' function listens for the A button being pressed. When this happens, the code inside the function is executed, which in this case sets the 'armed' variable to true. Now, whenever you press the A button, your alarm system will be armed.

    4 - Disarm the alarm

    In this step, you will create a way to disarm the alarm. You'll do this by setting the 'armed' variable to false when the B button on the Microbit is pressed. This means that even if the sensors detect movement or sound, the alarm won't go off because it's not armed.

    Add the following code:

    input.onButtonPressed(Button.B, function () {
        armed = false
    })

    This code uses the 'input.onButtonPressed' function to check if the B button has been pressed. If it has, the function inside the brackets is run. This function sets the 'armed' variable to false, effectively disarming the alarm.

    5 - Define Alarm Function

    In programming, a function is a block of reusable code that performs a specific task. Functions provide better modularity for your application and a high degree of code reusing. In this step, we will define a function to sound and flash the alarm. This function will be called whenever the alarm is triggered.

    Add the following code:

    function soundAlarm () {
        music.play(music.builtinPlayableSoundEffect(soundExpression.soaring), music.PlaybackMode.UntilDone)
        basic.showIcon(IconNames.Skull)
    }
    

    This code defines a function named 'soundAlarm'. Inside this function, we use the 'music.startMelody' function to play an alarm melody and 'basic.showIcon' function to display a skull icon on the Microbits LED display whenever the function is called.

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of DigitalSkills.org. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more