Read an analog value using ADC.

Dependencies:   mbed

Committer:
bcostm
Date:
Thu Mar 12 08:15:56 2015 +0000
Revision:
1:0490a15c76e4
Parent:
0:c2d1ad5059da
Simplification.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcostm 0:c2d1ad5059da 1 #include "mbed.h"
bcostm 0:c2d1ad5059da 2
bcostm 0:c2d1ad5059da 3 AnalogIn analog_value(A0);
bcostm 0:c2d1ad5059da 4
bcostm 1:0490a15c76e4 5 DigitalOut led(LED1);
bcostm 1:0490a15c76e4 6
bcostm 0:c2d1ad5059da 7 int main() {
bcostm 1:0490a15c76e4 8 float meas;
bcostm 1:0490a15c76e4 9
bcostm 1:0490a15c76e4 10 printf("\nAnalogIn example\n");
bcostm 1:0490a15c76e4 11
bcostm 1:0490a15c76e4 12 while(1) {
bcostm 1:0490a15c76e4 13 meas = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
bcostm 1:0490a15c76e4 14 meas = meas * 3300; // Change the value to be in the 0 to 3300 range
bcostm 1:0490a15c76e4 15 printf("measure = %.0f mV\n", meas);
bcostm 1:0490a15c76e4 16 if (meas > 2000) { // If the value is greater than 2V then switch the LED on
bcostm 1:0490a15c76e4 17 led = 1;
bcostm 1:0490a15c76e4 18 }
bcostm 1:0490a15c76e4 19 else {
bcostm 1:0490a15c76e4 20 led = 0;
bcostm 0:c2d1ad5059da 21 }
bcostm 0:c2d1ad5059da 22 wait(0.2); // 200 ms
bcostm 0:c2d1ad5059da 23 }
bcostm 0:c2d1ad5059da 24 }