analog example

Committer:
tulanthoar
Date:
Fri Apr 21 16:10:55 2017 +0000
Revision:
1:e6039396a107
Parent:
0:740306e460c0
switch to mbed-os

Who changed what in which revision?

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