Read a single switch using Hotboards_switches library

Dependencies:   Hotboards_switches mbed

Fork of input by Roman Valencia

Committer:
RomanValenciaP
Date:
Tue Mar 15 18:47:03 2016 +0000
Revision:
2:d4a7439b72dd
Parent:
1:ce1c787ed6fd
added original author and contact info

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RomanValenciaP 0:6e25e921deef 1
RomanValenciaP 2:d4a7439b72dd 2 /* Library: Hotboards_switches.h
RomanValenciaP 2:d4a7439b72dd 3 * Project: input
RomanValenciaP 2:d4a7439b72dd 4 * File: main.cpp
RomanValenciaP 2:d4a7439b72dd 5 * Author: Diego Perez
RomanValenciaP 2:d4a7439b72dd 6 * Modified by: Roman Valencia
RomanValenciaP 2:d4a7439b72dd 7 * Contact: http://www.hotboards.org/
RomanValenciaP 1:ce1c787ed6fd 8 *
RomanValenciaP 0:6e25e921deef 9 * The simplest program, read an input on pin PB_4
RomanValenciaP 0:6e25e921deef 10 */
RomanValenciaP 0:6e25e921deef 11
RomanValenciaP 0:6e25e921deef 12 #include "mbed.h"
RomanValenciaP 0:6e25e921deef 13 #include "Hotboards_switches.h"
RomanValenciaP 0:6e25e921deef 14
RomanValenciaP 0:6e25e921deef 15 // Creates a single sw object, this interrupt will give us a LOW(0) value when close
RomanValenciaP 0:6e25e921deef 16 // because our dip switch works with pull-ups
RomanValenciaP 0:6e25e921deef 17 Hotboards_switches sw( PB_4 );
RomanValenciaP 0:6e25e921deef 18 // If your dip switch will gave you a HIGH(1) value when close, then we need to create
RomanValenciaP 0:6e25e921deef 19 // the sw object with an extra parameter: Hotboards_switches sw( PB_4 , 1 );
RomanValenciaP 0:6e25e921deef 20 // In any case the function will return a HIGH(1) value any time the sw is closed
RomanValenciaP 0:6e25e921deef 21
RomanValenciaP 0:6e25e921deef 22 //For this example we will use the USB serial port, here we initialize it
RomanValenciaP 0:6e25e921deef 23 Serial pc(USBTX,USBRX);
RomanValenciaP 0:6e25e921deef 24
RomanValenciaP 0:6e25e921deef 25 int main()
RomanValenciaP 0:6e25e921deef 26 {
RomanValenciaP 0:6e25e921deef 27 while(1)
RomanValenciaP 0:6e25e921deef 28 {
RomanValenciaP 0:6e25e921deef 29 // When the interrupt is close (or ON) the function will return a true value
RomanValenciaP 0:6e25e921deef 30 // it doesn´t matter if our input is configured with pull-ups(LOW) or
RomanValenciaP 0:6e25e921deef 31 // pull-downs(HIGH)
RomanValenciaP 0:6e25e921deef 32 if( sw.read() )
RomanValenciaP 0:6e25e921deef 33 {
RomanValenciaP 0:6e25e921deef 34 pc.printf( "sw = close (on)\n\r" );
RomanValenciaP 0:6e25e921deef 35 }
RomanValenciaP 0:6e25e921deef 36 else
RomanValenciaP 0:6e25e921deef 37 {
RomanValenciaP 0:6e25e921deef 38 pc.printf( "sw = open (off)\n\r" );
RomanValenciaP 0:6e25e921deef 39 }
RomanValenciaP 0:6e25e921deef 40 // Wait 1 second, just to not query so often
RomanValenciaP 0:6e25e921deef 41 wait( 1 );
RomanValenciaP 0:6e25e921deef 42 }
RomanValenciaP 0:6e25e921deef 43 }