Ultrasonic sensor mis-fire?

My students have built a Skeeball Game. They are using Ultrasonic sensors to detect the ball entering a hole. 3 of the 7 sensors are “acting up”. If you look at the test code, we are not supposed to get an alert if the distance detected is not less than 2 inches. But sensor 2, 4 and 7 are randomly returning 0.00 (See attached image). Before I go replacing the sensor I wanted to make sure the code is correct with the library and sensor I am using, or other suggestions:

Code:
#include <Ultrasonic.h>

//pin declarations
int BST1 = 31;//Ball Sensor 1 Trigger
int BST2 = 33;//Ball Sensor 2 Trigger
int BST3 = 35;//Ball Sensor 3 Trigger
int BST4 = 37;//Ball Sensor 4 Trigger
int BST5 = 39;//Ball Sensor 5 Trigger
int BST6 = 41;//Ball Sensor 6 Trigger
int BST7 = 43;//Ball Sensor 7 Trigger

int BSE1 = 30;//Ball Sensor 1 Echo
int BSE2 = 32;//Ball Sensor 2 Echo
int BSE3 = 34;//Ball Sensor 3 Echo
int BSE4 = 36;//Ball Sensor 4 Echo
int BSE5 = 38;//Ball Sensor 5 Echo
int BSE6 = 40;//Ball Sensor 6 Echo
int BSE7 = 42;//Ball Sensor 7 Echo

//init sensors
Ultrasonic ultrasonicBS1(BST1,BSE1);//initializes ultrasonic sensor for hole 1
Ultrasonic ultrasonicBS2(BST2,BSE2);//initializes ultrasonic sensor for hole 2
Ultrasonic ultrasonicBS3(BST3,BSE3);//initializes ultrasonic sensor for hole 3
Ultrasonic ultrasonicBS4(BST4,BSE4);//initializes ultrasonic sensor for hole 4
Ultrasonic ultrasonicBS5(BST5,BSE5);//initializes ultrasonic sensor for hole 5
Ultrasonic ultrasonicBS6(BST6,BSE6);//initializes ultrasonic sensor for hole 6
Ultrasonic ultrasonicBS7(BST7,BSE7);//initializes ultrasonic sensor for hole 7

void setup() {}

void loop(){

UltraSonicTest(ultrasonicBS1, 1);
UltraSonicTest(ultrasonicBS2, 2);
UltraSonicTest(ultrasonicBS3, 3);
UltraSonicTest(ultrasonicBS4, 4);
UltraSonicTest(ultrasonicBS5, 5);
UltraSonicTest(ultrasonicBS6, 6);
UltraSonicTest(ultrasonicBS7, 7);
}

void UltraSonicTest(Ultrasonic ultrasonicTest, int x)
{
float indistance;
long microsec = ultrasonicTest.timing();
indistance = ultrasonicTest.CalcDistance(microsec,Ultrasonic::IN);//this result unit is inches
if(indistance <2){
Serial.print("Sensor “);
Serial.print(x);
Serial.print(”: distance - ");
Serial.println(indistance);

}

BTW: The sensor I am using is the HC-SR04 ultrasonic sensor.

lotsoffear:
My students have built a Skeeball Game. They are using Ultrasonic sensors to detect the ball entering a hole.

Terrible choice. Wrong tool for the job.
Optical methods are much better, such as a break beam sensor across the opening. Faster, no interference with each other, no issues with too short distance, etc. Just a very simple on/off signal. This can be as cheap as a bright LED shining onto an LDR (probably fast enough for this - if not use a photodiode or phototransistor), with a little bit of shielding to prevent problems from ambient light. With a well chosen series resistor this can be read as a digital signal. Ambient light shouldn't be an issue as the ball would block it just the same as it falls through the hole.

3 of the 7 sensors are "acting up". If you look at the test code, we are not supposed to get an alert if the distance detected is not less than 2 inches. But sensor 2, 4 and 7 are randomly returning 0.00 (See attached image).

The minimum distance those sensors can detect is about 2 cm. You're barely above that minimum distance. Secondly, you're not allowing any time between readings of different sensors, and that's asking for interference as they will hear the echoes of other sensors. You must allow for some time between readings, try 10-50 ms.

To make sure an individual sensor works: take it out, test it individually using a proper target (e.g. a wall) at a reasonable distance (within range: 2-300 cm).

The students already tested each individual sensor. I believe you were correct about the sensors hearing the echoes of other sensors. We put in just a minuscule "delay(2);" between each read and it works perfect!

As for the wrong sensor: Yes I agree. The hardest thing to do as a teacher is let students "make mistakes" or not choose the best tool(or sensor in this case) for a job. That said, I did present them with alternatives but, this is their project and this is the one the wanted to go with. If they get further issues with timing/readings then they may decide to go with the optical approach.

I will also show them this post tomorrow.

Thank you for your input and help!

That's great progress!

For letting them make mistakes: agreed. On the other hand, it's a good idea to stimulate them to actually try out two or three promising methods and find out which one works best, then make the choice. Just picking one when you have no experience whatsoever with any of the methods is poor engineering.

The sensors will probably be able to do the job (if the balls are big/slow enough the sensors should be fast enough to sense them) - it's just that there are better methods available.

They actually did try out one light option. I think they choose the Ultrasonic because they have used it in robotics and were more familiar with it. Hopefully the sensors will work but if not lesson learned :-). Thanks again for your valued input!