reading only one interrupt when it changes its value

Dependencies:   Hotboards_switches mbed

Committer:
RomanValenciaP
Date:
Tue Mar 15 18:47:14 2016 +0000
Revision:
2:19c591b8f837
Parent:
1:af16896c2662
added original author and contact info

Who changed what in which revision?

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