Skip to content

User button

Description

SensorIO has one button that can be used by firmware.

Code example

Compiling example

This example is based on Mbed OS and requires a simple setup. For full description check Code Setup page.

Button driver

Button does not need external driver - only Mbed OS built-in InterruptIn class is required.

Code

Pin definitions

1
2
    USER_BUTTON = PC_13,
    BUTTON1     = USER_BUTTON,

Example

In this example user button is used to turn LD2 on and off.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "mbed_events.h"
#include "DigitalOut.h"
#include "InterruptIn.h"
#include "Serial.h"

mbed::Serial console(SERIAL_TX, SERIAL_RX, 115200);
events::EventQueue scheduler;
mbed::DigitalOut green(RGB_GREEN);

void ledStatus(int status)
{
    console.printf("Green Led is %s\r\n", (status == 1) ? "ON" : "OFF");
}

void buttonPressed()
{
    // toggle led state
    green = !green;

    // log led state via Serial (via EventLoop)
    scheduler.call(&ledStatus, green);
}

int main(int argc, char **argv)
{
    console.printf("This is SensorIO\r\n");

    mbed::InterruptIn button(USER_BUTTON);
    // attach function to button press
    button.fall(&buttonPressed);

    // running event loop
    scheduler.dispatch_forever();
}

Example main.cpp file can be downloaded from here.