MB7040 Range finder experiment.

Dependencies:   mbed

Fork of SonarRangeFinderV1 by Jim Belesiu

Committer:
gfmitch
Date:
Sun Mar 01 08:07:33 2015 +0000
Revision:
2:9870fda1edb6
Parent:
1:0cf1f2254f10
Modified v1-1 to correctly handle range of values read by MB7040.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Belesiu 0:28fb80eb93ca 1 // Ultra Sonice Range Finder
Belesiu 0:28fb80eb93ca 2 // Experiement #1
Belesiu 0:28fb80eb93ca 3 // Belesiu
Belesiu 0:28fb80eb93ca 4 // 2014.12.26
Belesiu 0:28fb80eb93ca 5
Belesiu 0:28fb80eb93ca 6 #include "mbed.h"
Belesiu 0:28fb80eb93ca 7
Belesiu 0:28fb80eb93ca 8 DigitalOut myled(LED1);
Belesiu 0:28fb80eb93ca 9
Belesiu 0:28fb80eb93ca 10 I2C rangefinder(p9, p10); // sda, sc1
Belesiu 0:28fb80eb93ca 11 Serial pc(USBTX, USBRX); // tx, rx
Belesiu 0:28fb80eb93ca 12
Belesiu 0:28fb80eb93ca 13 const int addr = 0xE0;
Belesiu 0:28fb80eb93ca 14 char config_r[2];
Belesiu 0:28fb80eb93ca 15 char range_read[2];
Belesiu 0:28fb80eb93ca 16 float range;
Belesiu 0:28fb80eb93ca 17
Belesiu 1:0cf1f2254f10 18 AnalogOut signal(p18);
Belesiu 1:0cf1f2254f10 19
Belesiu 0:28fb80eb93ca 20 int main()
Belesiu 0:28fb80eb93ca 21 {
Belesiu 0:28fb80eb93ca 22 int x = 0;
Belesiu 0:28fb80eb93ca 23
Belesiu 0:28fb80eb93ca 24 while(1) {
Belesiu 0:28fb80eb93ca 25
Belesiu 0:28fb80eb93ca 26 // Test serial port
Belesiu 0:28fb80eb93ca 27 myled = 1;
Belesiu 0:28fb80eb93ca 28 wait(0.2);
Belesiu 0:28fb80eb93ca 29 myled = 0;
Belesiu 0:28fb80eb93ca 30 wait(0.2);
Belesiu 1:0cf1f2254f10 31 // pc.printf("Hello Jim! - %d\n", x++);
Belesiu 0:28fb80eb93ca 32
Belesiu 0:28fb80eb93ca 33 // For MB7040
Belesiu 0:28fb80eb93ca 34
Belesiu 0:28fb80eb93ca 35 config_r[0] = 0x00; // set pointer reg to ‘cmd register'
Belesiu 0:28fb80eb93ca 36 config_r[1] = 0x51; // config data byte1
Belesiu 0:28fb80eb93ca 37 rangefinder.write(addr, config_r, 2);
Belesiu 0:28fb80eb93ca 38 wait(0.07);
Belesiu 0:28fb80eb93ca 39
Belesiu 0:28fb80eb93ca 40 config_r[0] = 0x02; //set pointer reg to 'data register'
Belesiu 0:28fb80eb93ca 41 rangefinder.write(addr, config_r, 1); //send to pointer 'read range'
Belesiu 0:28fb80eb93ca 42 rangefinder.read(addr, range_read, 2); //read the two-byte range data
Belesiu 0:28fb80eb93ca 43 range = ((range_read[0] << 8) + range_read[1]);
Belesiu 0:28fb80eb93ca 44
Belesiu 1:0cf1f2254f10 45 pc.printf("%d Range = %.2f cm\n\r", x++, range); //print range on screen
Belesiu 0:28fb80eb93ca 46 wait(0.05);
Belesiu 1:0cf1f2254f10 47
Belesiu 1:0cf1f2254f10 48 // Output voltage proportional to distance.
Belesiu 1:0cf1f2254f10 49 // The output value for the variable signal is 0 to 1.0.
Belesiu 1:0cf1f2254f10 50
gfmitch 2:9870fda1edb6 51 signal = ( range - 20.0 ) / 745.0;
gfmitch 2:9870fda1edb6 52
gfmitch 2:9870fda1edb6 53 // original code (below) did not account for the "dead" values
gfmitch 2:9870fda1edb6 54 // 0-20cm
gfmitch 2:9870fda1edb6 55
gfmitch 2:9870fda1edb6 56 // signal = range / 765.0;
Belesiu 0:28fb80eb93ca 57
Belesiu 0:28fb80eb93ca 58 }
Belesiu 0:28fb80eb93ca 59 }