Maxbotix MB7077 sensor (suitable for use under water).
This sensor can be used in air or in water - although you may need to add extra gloop to make it water proof.
The sonar can output data in a variety of formats - but this library uses the analogue output from the 'AN' pin to an ADC pin on your micro controller.
The device can be powered using either 3.3v or 5v BUT your choice will depend on the reference voltage used by the ADC on your board. ie using 3.3v to power the device from a board that uses 5v as its ADC reference voltage will give wrong readings. But worse: powering the device with 5v from a board that expects a maximum ADC input of 3.3v may fry the ADC unit on your board.
The device can measure up to about 700cm with a 5v supply and up to about 600cm with a 3.3v supply.
The returned range from the device is based on the speed of sound and hence will vary depending upon the medium where it lives. For example: sound travels 4.3 times faster under water than it does through air. For this reason: you can specify in the MAKE command whether the device is under water or not as well as the ADC pin used to read the values:-
Maxbotix_MB7077 sonar = MAKE_Maxbotix_MB7077(ADC0, TRUE);
The sensor should be initialised in appInitHardware using:-
The sensor can then be read using:-
and then the distance in cm will be in sonar.distance.cm and will be adjusted to be correct for 'in air' or 'in water'.
If your robot is amphibious then you can change the 'inWater' member variable at runtime to reflect the current environment for the device:-
sonar.inWater = TRUE; // Sonar is under water
sonar.inWater = FALSE; // Sonar is in air