Read a single switch using Hotboards_switches library

Dependencies:   Hotboards_switches mbed

Fork of input by Roman Valencia

main.cpp

Committer:
RomanValenciaP
Date:
2016-03-15
Revision:
2:d4a7439b72dd
Parent:
1:ce1c787ed6fd

File content as of revision 2:d4a7439b72dd:


/* Library:     Hotboards_switches.h
 * Project:     input
 * File:        main.cpp
 * Author:      Diego Perez
 * Modified by: Roman Valencia 
 * Contact:     http://www.hotboards.org/
 *
 * The simplest program, read an input on pin PB_4
 */
 
#include "mbed.h"
#include "Hotboards_switches.h"

// Creates a single sw object, this interrupt will give us a LOW(0) value when close
// because our dip switch works with pull-ups
Hotboards_switches sw( PB_4 );
// If your dip switch will gave you a HIGH(1) value when close, then we need to create
// the sw object with an extra parameter: Hotboards_switches sw( PB_4 , 1 );
// In any case the function will return a HIGH(1) value any time the sw is closed

//For this example we will use the USB serial port, here we initialize it
Serial pc(USBTX,USBRX);

int main()
{
    while(1)
    {
        // When the interrupt is close (or ON) the function will return a true value
        // it doesn´t matter if our input is configured with pull-ups(LOW) or
        // pull-downs(HIGH)
        if( sw.read() )
        {
            pc.printf( "sw = close (on)\n\r" );
        }
        else
        {
            pc.printf( "sw = open (off)\n\r" );
        }
        // Wait 1 second, just to not query so often
        wait( 1 );
    }
}