David k / Mbed 2 deprecated ADS1115_IR

Dependencies:   mbed ADS1115

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Main.cpp Source File

Main.cpp

00001 #include "mbed.h"
00002 #include "Adafruit_ADS1015.h"
00003 
00004 #define SERIAL_BAUD_RATE    9600
00005 
00006 #define a_samping       500000
00007 #define b_samping       1.184
00008 //#define b_samping       1.229
00009 #define a_depan         10000000
00010 #define b_depan         1.297
00011 
00012 I2C i2c(D14, D15);
00013 Adafruit_ADS1015 ads(&i2c);
00014 Serial pc(USBTX, USBRX);
00015 
00016 float dist = 0;
00017 
00018 float call_samping(uint16_t x) {
00019   float side_distance = a_samping/pow(x,b_samping)+1;
00020     return side_distance;
00021 }
00022 
00023 float call_depan(uint16_t x){
00024     float front_distance = a_depan/pow(x,b_depan);
00025     return front_distance;
00026 }
00027 
00028 int main() {
00029     uint16_t ir;
00030     const uint16_t u16tmax = 0xFFFF;
00031     while (1) {
00032         ir = ads.readADC_SingleEnded(2);
00033         float voltage = ir * 6.2676 * 10e-5;
00034         uint16_t x = voltage/3.3 * 0xFFFF ;
00035         float dist = call_samping(x);
00036 //        pc.printf("raw = %ul volt = %.2f x=%ul dist=%.2f \n", ir, voltage, x, dist);
00037         pc.printf("%.5f     %.5f\n", voltage,dist);
00038         wait(0.08); // loop 2 sek
00039     }
00040 }