Mistake on this page?
Report an issue in GitHub or email us

Alarm tutorial

This guide reviews the steps required to build and run a basic alarm application on an Mbed OS platform.

Please install Mbed CLI.

Import the example application

From the command-line, import the example:

mbed import mbed-os-example-alarm
cd mbed-os-example-alarm

If using the Mbed Online Compiler, use the **Import into Mbed IDE" button below:

/* mbed Microcontroller Library
 * Copyright (c) 2018 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 */
#include "mbed.h"

// Time constants in seconds
#define HOUR   60 * 60
#define MINUTE 60

// Globals
DigitalOut alarm_out(D2, 0);
DigitalOut alarm_led(LED_RED, 1);
DigitalOut hour_led(LED_GREEN, 1);
DigitalOut min_led(LED_BLUE, 1);

InterruptIn inc_time(BUTTON1);
InterruptIn sel(BUTTON2);

LowPowerTicker alarm_event;

volatile uint64_t delay        = 0;
volatile uint8_t  hour_count   = 0;
volatile uint8_t  min_count    = 0;
volatile uint8_t  select_state = 0;

// Timer Callbacks
void inc_select(void)
{
    if (select_state < 2) {
        select_state++;
    } else {
        // Use select button to disable alarm
        alarm_out = 0;
        alarm_led = 1;
    }
}

void set_time_leds(void)
{
    if (select_state == 0) {
        hour_led = !hour_led;
    } else {
        min_led = !min_led;
    }
}

void inc_delay(void)
{
    if (select_state == 0) {
        delay += HOUR;
        hour_count++;
        hour_led = !hour_led;
    } else {
        delay += MINUTE;
        min_count++;
        min_led = !min_led;
    }
}

void trigger_alarm_out(void)
{
    alarm_out = 1;
    alarm_led = 0;
}

/* Use buttons to select the alarm time. Cycle through hours in an incrementing
 * fashion using Button1. Press select, and increment through minutes. Press
 * select one more time to begin the alarm timer.
 *
 * The Time LEDs blink in time with the button inputs to show the
 * currently selected alarm time. Once you press select a second time to begin
 * the timer, the LEDs will blink out the configured delay in hours and
 * minutes before going into a low power sleep mode.
 *
 * Once the alarm fires, pressing the select button will turn the alarm off
 * until the next time it fires.
 *__________________________________________________________________________
 * You may also use the RTC (hardware or software through the Time API) to
 * set a real world time and set an alarm for a specific timestamp rather
 * than on a delay. However, this requires manually setting the time on each
 * reset, or an internet connection to automatically collect the
 * time.
 */
// Main thread
int main()
{
    // Configure interrupt in pins (button controls)
    sel.rise(inc_select);
    inc_time.fall(set_time_leds);
    inc_time.rise(inc_delay);

    // Sleep while waiting for user input to set the desired delay
    while (select_state < 2) {
        ThisThread::sleep_for(10);
    }

    // Once the delay has been input, blink back the configured hours and
    // minutes selected
    for (uint8_t i = 0; i < hour_count * 2; i++) {
        hour_led = !hour_led;
        ThisThread::sleep_for(250);
    }

    for (uint8_t i = 0; i < min_count * 2; i++) {
        min_led = !min_led;
        ThisThread::sleep_for(250);
    }

    // Attach the low power ticker with the configured alarm delay
    alarm_event.attach(&trigger_alarm_out, delay);

    // Sleep in the main thread
    while (1) {
        sleep();
    }
}

Compile

Invoke mbed compile, and specify the name of your platform and your favorite toolchain (GCC_ARM, ARM, IAR). For example, for the ARM Compiler 5:

mbed compile -m K64F -t ARM

Your PC may take a few minutes to compile your code.

Program the board

  1. Connect your Mbed device to the computer over USB.
  2. Copy the binary file to the Mbed device.
  3. Press the reset button to start the program.
  4. Press Button1 for the number of desired hours to delay. Press Button2 to cycle to minutes, and repeat the previous step for the number of desired minutes.
  5. Press Button2 again to start the alarm.
  6. Press Button2 again once the alarm triggers to silence it. Both an LED and a digital out pin will go high on the alarm trigger and go back low on an alarm reset.

Troubleshooting

If you have problems, you can review the documentation for suggestions on what could be wrong and how to fix it.

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.