This sketch shown how to read a single switch with pull up resistor

Dependencies:   mbed

Committer:
Hotboards
Date:
Fri Mar 18 18:12:03 2016 +0000
Revision:
0:fb95cef55dd9
first release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Hotboards 0:fb95cef55dd9 1 /*
Hotboards 0:fb95cef55dd9 2 * Hotboards_Switches_Reading_Single_Switch
Hotboards 0:fb95cef55dd9 3 * This sketch demonstrates how to read a single switch,
Hotboards 0:fb95cef55dd9 4 * example created by Pedro from (http://www.hotboards.org)
Hotboards 0:fb95cef55dd9 5 */
Hotboards 0:fb95cef55dd9 6
Hotboards 0:fb95cef55dd9 7 #include "mbed.h"
Hotboards 0:fb95cef55dd9 8
Hotboards 0:fb95cef55dd9 9 DigitalIn SW1(PC_8); // Switch connected on PC_8
Hotboards 0:fb95cef55dd9 10 DigitalOut led(PB_14); // led connected on PB_14
Hotboards 0:fb95cef55dd9 11
Hotboards 0:fb95cef55dd9 12 /*switches in this example have a pull up resistors*/
Hotboards 0:fb95cef55dd9 13 int main()
Hotboards 0:fb95cef55dd9 14 {
Hotboards 0:fb95cef55dd9 15 while(1)
Hotboards 0:fb95cef55dd9 16 {
Hotboards 0:fb95cef55dd9 17 if(SW1 == 1) /* if switch is in OFF position*/
Hotboards 0:fb95cef55dd9 18 {
Hotboards 0:fb95cef55dd9 19 led = 0; /* led OFF*/
Hotboards 0:fb95cef55dd9 20 }
Hotboards 0:fb95cef55dd9 21 else /* switch in ON position*/
Hotboards 0:fb95cef55dd9 22 {
Hotboards 0:fb95cef55dd9 23 led = 1; /* Led ON*/
Hotboards 0:fb95cef55dd9 24 }
Hotboards 0:fb95cef55dd9 25 }
Hotboards 0:fb95cef55dd9 26 }