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

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 /* This program reads output from built-in analogue to digital converter (ADC) */
00003 
00004 /* Define some useful constants */
00005 #define ON 1
00006 #define OFF 0
00007 
00008 /* Create objects for input/output */
00009 AnalogIn pot_reading(p15); /* ADC input is connected to Pin 15 */
00010 
00011 /* Create names for lights */
00012 DigitalOut light1(LED1); /* Control LED1 using light1 */
00013 DigitalOut light2(LED2); /* Control LED2 using light2 */
00014 DigitalOut light3(LED3); /* Control LED3 using light3 */
00015 DigitalOut light4(LED4); /* Control LED4 using light4 */
00016 
00017 /* Main loop */
00018 int main() 
00019 {
00020     float reading;  /* A variable is needed to store ADC reading */
00021     
00022     /* Define a while loop that runs forever (or until the MBED is reset) */
00023     while(1)
00024     {
00025         reading=pot_reading; /* store reading from the ADC into variable 'reading' */   
00026         if(reading > 0.5) /* Checks whether value stored in 'reading' is greater than 0.5 */
00027         {
00028             light1=ON; /* light1 is turned on when ADC reading exceeds 0.5 */
00029         }
00030         else
00031         {
00032             light1=OFF; /* light1 is turned off when ADC reading is less than 0.5 */
00033         }       
00034         wait(0.1); /* Pause for 0.1 seconds before taking another reading */
00035     }
00036 }