Simple library for reading distance from SRF02 Ultrasonic Sensor in I2C mode. With the use of imperial readings too
Fork of SRF02 by
Revision 3:78c8202f8929, committed 2016-04-15
- Comitter:
- el15pjt
- Date:
- Fri Apr 15 13:24:10 2016 +0000
- Parent:
- 2:ad1613b4145c
- Commit message:
- Menus added. Bug in buzzer still!
Changed in this revision
SRF02.cpp | Show annotated file Show diff for this revision Revisions of this file |
SRF02.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r ad1613b4145c -r 78c8202f8929 SRF02.cpp --- a/SRF02.cpp Fri Mar 18 15:03:50 2016 +0000 +++ b/SRF02.cpp Fri Apr 15 13:24:10 2016 +0000 @@ -15,6 +15,34 @@ } +int SRF02::getDistannceInch() +{ + char data[2]; + + // need to send CM command to command register + data[0] = CMD_REG; + data[1] = INCH_CMD; + int ack = i2c->write(SRF02_W_ADD,data,2); + if (ack) + error(); // if we don't receive acknowledgement, flash error message + + // this will start the sensor ranging, the datasheet suggests a delay of at least 65 ms before reading the result + wait_ms(70); + + // we can now read the result - tell the sensor we want the high byte + char reg = RANGE_H_REG; + ack = i2c->write(SRF02_W_ADD,®,1); + if (ack) + error(); // if we don't receive acknowledgement, flash error message + + // if we read two bytes, the register is automatically incremented (H and L) + ack = i2c->read(SRF02_R_ADD,data,2); + if (ack) + error(); // if we don't receive acknowledgement, flash error message + + // high byte is first, then low byte, so combine into 16-bit value + return (data[0] << 8) | data[1]; +} int SRF02::getDistanceCm() { char data[2];
diff -r ad1613b4145c -r 78c8202f8929 SRF02.h --- a/SRF02.h Fri Mar 18 15:03:50 2016 +0000 +++ b/SRF02.h Fri Apr 15 13:24:10 2016 +0000 @@ -73,7 +73,7 @@ * */ int getDistanceCm(); - +int getDistannceInch(); private: /** Hangs in infinite loop flashing 'blue lights of death' *