To the NewPing Library moderators:

Hello, and thank you (in advance) for your patience.

Hardware used: Genuine Arduino Uno R3 (made in USA); external power supply - 'wall wart' style 120V in, 5V at 2A out; Arduino IDE ver 1.8.5; breadboard - Jameco JE24.

I have looked through this forum, as well as the main Arduino ones, and have not seen my question asked. I apologize if I have been obtuse.

Briefly, I have used an HC_SR04 with the NewPing library. Using the sonar.ping_cm() result(s), I can get and print a reasonably consistent distance from my table to the wall (164 cm, or ca. 64 in).

Please see attachment "test_us-with-servo_05.ino"

My data output has a few instances like this:

Distance to target: 165 cm 64 in

Distance to target: 165 cm 64 in

Distance to target: 165 cm 64 in

Distance to target: 165 cm 21 in

Distance to target: 165 cm 21 in

Distance to target: 165 cm 64 in

Distance to target: 165 cm 64 in

Distance to target: 165 cm 21 in

My first question is: why does the distance in centimeters appear to be stable, but the distance in inches (the output of sonar.ping_in()) occasionally get to 1/3rd of what it ought to be - 21 instead of 64 inches? Note that the table support for the HC_SR04 and Arduino is large and unmoving, as is the wall at which the SR04 is pointed. The processing load on the Arduino should be minimal.

Larger data file attachment is "test_us-with-servo_05.txt"

So, despite the odd behavior of the distance in inches, the results for distance in cm seems stable enough for my purposes.

My goal though, was to use the distance function in conjunction with a continuous revolution servo motor, an FS90R from FEETECH. The idea is to have the servo rotating clockwise unless an object comes within the 'thresHold' distance, at which point the servo stops moving.

Adding statements to include the <Servo.h> library, and setting up the servo on Arduino digital pin 7 with range statements did not change the previous results.

However, as soon as my code began to communicate with BOTH the HC_SR04 and the FS90R, the accuracy of the HC_SR04 results was destroyed.

Code with HC_SR04 and FS90R in it: attachment "test_us-with-servo_07.ino"

Here is a sample of the output now:

Distance to target: 43 cm 17 in

Distance to target: 50 cm 10 in

Distance to target: 11 cm 11 in

Distance to target: 50 cm 19 in

Distance to target: 60 cm 15 in

Distance to target: 44 cm 17 in

Distance to target: 40 cm 16 in

Distance to target: 31 cm 19 in

Distance to target: 10 cm 12 in

Distance to target: 29 cm 17 in

Distance to target: 42 cm 20 in

Distance to target: 44 cm 13 in

Distance to target: 59 cm 16 in

Distance to target: 44 cm 19 in

Distance to target: 41 cm 13 in

Distance to target: 51 cm 17 in

Distance to target: 44 cm 19 in

I noticed that as bad as the distance in cm results are (the wall is still 164 cm or 64 inches away), the problem with the distance in inches is far worse - every distance is stated incorrectly for the inch values, whereas before, this only happened a few times. How does this relate to the occasional distance in inches problem we saw originally - i.e., without any servo code?

Larger data file attachment is: "test_us-with-servo_07.txt" If you look at the larger data file, you will notice I put a book in front of the SR04 a couple of times to verify the FS90R stopped rotating.

In fact, the servo DOES stop moving when the 'thresHold' value is reached [there is a slight stutter every 20 seconds or so as well], but the distance results seem completely unreliable. What have I done incorrectly? I thought I used the NewPing library correctly, but I am not pleased with my results. I have spent a fair amount of time attempting to correct this problem, but it seems as soon as I interact with both devices, my programming skills aren't up to the challenge.

I would appreciate any suggestions. Sorry for the long-winded explanations. Thank you.