Button example.

Dependencies:   mbed

main.cpp

Committer:
eencae
Date:
2018-01-10
Revision:
2:8211254a87fd
Parent:
1:f650db6c33e4
Child:
3:cb287ef68787

File content as of revision 2:8211254a87fd:

#include "mbed.h"

DigitalIn button_A(p29);
DigitalIn button_B(p28);
DigitalIn button_C(p27);
DigitalIn button_D(p26);

DigitalOut red_led(p24);
DigitalOut green_led(p23);
DigitalOut blue_led(p22);

void init_buttons();

int main()
{
    init_buttons();  // turn off internal pull-up/pull-down resistors

    while(1) {

        // check if button A pressed
        if ( button_A.read() == 1) {

            // writing a 1 turns the LED off, 0 makes it turn on (active-low)
            red_led.write(0);  // if it is, turn the red LED on
        } else {
            red_led.write(1);  // if it isn't, turn the red LED on
        }

        // check if button B pressed
        if ( button_B.read() == 1) {

            // writing a 1 turns the LED off, 0 makes it turn on (active-low)
            green_led.write(0);  // if it is, turn the red LED on
        } else {
            green_led.write(1);  // if it isn't, turn the red LED on
        }

        // check if button C pressed
        if ( button_C.read() == 1) {

            // writing a 1 turns the LED off, 0 makes it turn on (active-low)
            blue_led.write(0);  // if it is, turn the red LED on
        } else {
            blue_led.write(1);  // if it isn't, turn the red LED on
        }


        wait(0.1);  // small delay

    }
}

void init_buttons()
{
    // PCB has external pull-down resistors so turn the internal ones off
    // (default for DigitalIn)
    button_A.mode(PullNone);
    button_B.mode(PullNone);
    button_C.mode(PullNone);
    button_D.mode(PullNone);
}