Arduino class library for 4 pins, ultrasound range sensors

Ultrasound_Sensor.jpg

UltrasoundSensor.zip (2216Bytes)

This Arduino class library, gives some basic functions for activating and reading an Ultrasound sensor.

A small example, also included in the ZIP file.

#include "UltrasoundSensor.h"

UltrasoundSensor RangeSensor = UltrasoundSensor(3,4);

void setup() {

  Serial.begin(9600); 
}

void loop() {
  RangeSensor.Ping();
  delayMicroseconds(60); // Adviced delay for the HC-SR04 ultrasound range sensor.
  Serial.write("Range: ");
  Serial.print(RangeSensor.PingCM(),DEC);
  Serial.write("cm\r\n");
}

There is an example in the

There is an example in the UltrasoundSensor.ino file.

Also there is an example in the top comment of the UltrasoundSensor.h and UltrasoundSensor.cpp file.

One thing missing is an explanation to the parameters for the class constructor UltrasoundSensor(Echo pin, Trigger pin).

Ill edit the text, with more changes.

 

 

Example on how to use this library?

The interface of the class UltrasoundSensor may be tricky to use. There is a time cohesion between the functions.

Calling the PingCM without having called static Ping before does not behave as one might expect.
To have it correct it looks like you first have to call the function Ping to then call the function PingCM.

This is not that easy to use.

So from my point of view it is nessessary to add to this tip an example code on how to use this library. Like a unit test.

Accidental or Essential Complexity?

Fine. Thank you for the explanation.

Do you have any reasons not to have a simple function PingCM? Without a prior Ping and a must-have delay.

Here some thoughts about it: There are two challenges with the interface. First, the time cohesion (mandatory function call sequence). Second, the mandatory delay time value between the function calls.

If the function call sequence is not Ping, delay, PingCM then the interface does not work. If you call Ping, delay(1), PingCM the interface might work. So here the reliability of the class library is on stage.

Therefore any user of this interface must know about the function call sequence and about the specific delay time value.

Would’nt it be possible to move all this into the implementation and give the interface two functions: PingCM and PingINCH ?

…or you could call them MeasureDistanceInCM and MeasureDistanceInInch

Now the function sequence and the time value are implementation details.

Well, since i only know the
Well, since i only know the hc-sr04 data, i’m not sure that the delay pf 60us wont be different on anorher sensor, hence the sequene for calling.

You seem to a a really good ideer there to make another nice class interface for an ultrasound sensor, go create. This ia how i want it to work, i can write you one, but then there will be a fee included.

Nils is right. Make things

Nils is right. Make things as simple as possible and have a setup function that tells the class what kind of ultrasonic sensor it works with. Then, depending on the model, you can have different delays inside the PingCM or PingINCH functions so the user is not confused. 

Well, for my projects i
Well, for my projects i would like to have the delay outside the class, as im not using delaymicroseconds in my code, but is rather using a non blocking delay. So other code has a chance to do other functions while i wait for the echo.

It’s about learning and teaching

It’s about learning and teaching. About feedback and friendship. Not about complaining and selling.

Duplicate
Duplicate

easy

just change the pinnumbers and remove ‘const int…’ from the original posted at arduino.cc under ping

worked for me!