I am building a project which includes around 15 ultrasonic sensors as well as a large amount of other inputs and outputs. Since the ultrasonic sensors rely on time for distance readings (time from ping being sent to it being received) the execution of each ultrasonic sensor reading creates a large amount of delay and blocks all other inputs and outputs for that amount of time as the loop() is being held up.
What is the solution to this kind of a problem. Is there a way for me to have the sensors on their own control chip or on a separate system? Thanks.
The
NewPing library has an example sketch that does exactly this, ping 15 sensors without locking you out from doing other things. It does this with a combination of scheduled polling and timer interrupt pinging of each sensor. The example sketch can be
found here.
With this sketch, you still have plenty of processing time to do other things (it's like multitasking). This sketch will ping each sensor with a 33ms delay, so all sensors will ping about twice a second and then arrive at the oneSensorCycle() function where you would process these results. Due to the speed of sound and the output power of these sensors, that's about as fast as you can do this while guaranteeing no cross-sensor ping. You could get creative by changing the order of the sensor triggers (for example, on opposite sides) that may allow you speed this up a bit. But, you're really hitting the limit of acoustic physics here. Triggering all the sensors at once sounds like a good idea, until you consider all the noise and echos you'll get. It works, just not consistently reliable.
So, if getting ping results from all sensors twice a second is fast enough, the example sketch above will work just fine and give you enough processing time to do something with these results.
Keep in mind that this sketch is designed to ping each sensor and then process the results once every cycle. If you want to process results after each ping, you would need a different sketch. I've created a message that talks more about this and gives examples of different ways of processing the results including sketches that process results after each ping. There's also more information about the 15 sensor sketch and other tips. Please see the following message for detailed information:
http://arduino.cc/forum/index.php/topic,106043.msg1009294.html#msg1009294Tim