This program senses the analogue voltage of a potentiometer using the MBED LPC1768

Dependencies:   mbed

Committer:
rmcwilliam101
Date:
Mon Jun 29 12:26:07 2015 +0000
Revision:
0:7186208c7c28
This program senses the status of a tilt switch circuit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rmcwilliam101 0:7186208c7c28 1 #include "mbed.h"
rmcwilliam101 0:7186208c7c28 2 /* This program reads output from built-in analogue to digital converter (ADC) */
rmcwilliam101 0:7186208c7c28 3
rmcwilliam101 0:7186208c7c28 4 /* Define some useful constants */
rmcwilliam101 0:7186208c7c28 5 #define ON 1
rmcwilliam101 0:7186208c7c28 6 #define OFF 0
rmcwilliam101 0:7186208c7c28 7
rmcwilliam101 0:7186208c7c28 8 /* Create objects for input/output */
rmcwilliam101 0:7186208c7c28 9 AnalogIn pot_reading(p15); /* ADC input is connected to Pin 15 */
rmcwilliam101 0:7186208c7c28 10
rmcwilliam101 0:7186208c7c28 11 /* Create names for lights */
rmcwilliam101 0:7186208c7c28 12 DigitalOut light1(LED1); /* Control LED1 using light1 */
rmcwilliam101 0:7186208c7c28 13 DigitalOut light2(LED2); /* Control LED2 using light2 */
rmcwilliam101 0:7186208c7c28 14 DigitalOut light3(LED3); /* Control LED3 using light3 */
rmcwilliam101 0:7186208c7c28 15 DigitalOut light4(LED4); /* Control LED4 using light4 */
rmcwilliam101 0:7186208c7c28 16
rmcwilliam101 0:7186208c7c28 17 /* Main loop */
rmcwilliam101 0:7186208c7c28 18 int main()
rmcwilliam101 0:7186208c7c28 19 {
rmcwilliam101 0:7186208c7c28 20 float reading; /* A variable is needed to store ADC reading */
rmcwilliam101 0:7186208c7c28 21
rmcwilliam101 0:7186208c7c28 22 /* Define a while loop that runs forever (or until the MBED is reset) */
rmcwilliam101 0:7186208c7c28 23 while(1)
rmcwilliam101 0:7186208c7c28 24 {
rmcwilliam101 0:7186208c7c28 25 reading=pot_reading; /* store reading from the ADC into variable 'reading' */
rmcwilliam101 0:7186208c7c28 26 if(reading > 0.5) /* Checks whether value stored in 'reading' is greater than 0.5 */
rmcwilliam101 0:7186208c7c28 27 {
rmcwilliam101 0:7186208c7c28 28 light1=ON; /* light1 is turned on when ADC reading exceeds 0.5 */
rmcwilliam101 0:7186208c7c28 29 }
rmcwilliam101 0:7186208c7c28 30 else
rmcwilliam101 0:7186208c7c28 31 {
rmcwilliam101 0:7186208c7c28 32 light1=OFF; /* light1 is turned off when ADC reading is less than 0.5 */
rmcwilliam101 0:7186208c7c28 33 }
rmcwilliam101 0:7186208c7c28 34 wait(0.1); /* Pause for 0.1 seconds before taking another reading */
rmcwilliam101 0:7186208c7c28 35 }
rmcwilliam101 0:7186208c7c28 36 }