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.
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 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.