Tomo Yamanaka / Mbed 2 deprecated Distance_Sensor_Sample

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 AnalogIn ain(A0);
00004 DigitalOut led1(LED_RED);
00005 Ticker flipper;
00006 int f_sen = 0;
00007 
00008 void flip() {
00009     float tmp1 = 0;
00010     float tmp2 = 0;
00011     float data;
00012     float distance;
00013 
00014     tmp1 = ain;
00015     tmp2 = ain;
00016     data = (tmp1 + tmp2) / 2.0f;
00017 
00018     if ((0.121 <= data) && (data <= 0.970)) {
00019         distance = 26.663 * pow((data * 3.3),-1.25);
00020         printf("Distance : %4.3f[cm]\n",distance);
00021         f_sen = 1;
00022     } else {
00023         printf("Distance : ------[cm]\n");
00024         f_sen = 0;
00025     }
00026 }
00027 
00028 int main() {
00029 
00030     printf("Sensor start!\n");
00031     flipper.attach_us(&flip, 500000);//500ms 
00032     while(1) {
00033         if(f_sen == 1){
00034             led1 = 1;
00035         } else {
00036             led1 = 0;
00037         }
00038         wait(0.5);
00039     }
00040 }