NewPing15SensorsTimer.pde 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // ---------------------------------------------------------------------------
  2. // Before attempting to use this sketch, please read the "Help with 15 Sensors Example Sketch":
  3. // https://bitbucket.org/teckel12/arduino-new-ping/wiki/Help%20with%2015%20Sensors%20Example%20Sketch
  4. //
  5. // This example code was used to successfully communicate with 15 ultrasonic sensors. You can adjust
  6. // the number of sensors in your project by changing SONAR_NUM and the number of NewPing objects in the
  7. // "sonar" array. You also need to change the pins for each sensor for the NewPing objects. Each sensor
  8. // is pinged at 33ms intervals. So, one cycle of all sensors takes 495ms (33 * 15 = 495ms). The results
  9. // are sent to the "oneSensorCycle" function which currently just displays the distance data. Your project
  10. // would normally process the sensor results in this function (for example, decide if a robot needs to
  11. // turn and call the turn function). Keep in mind this example is event-driven. Your complete sketch needs
  12. // to be written so there's no "delay" commands and the loop() cycles at faster than a 33ms rate. If other
  13. // processes take longer than 33ms, you'll need to increase PING_INTERVAL so it doesn't get behind.
  14. // ---------------------------------------------------------------------------
  15. #include <NewPing.h>
  16. #define SONAR_NUM 15 // Number of sensors.
  17. #define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
  18. #define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
  19. unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
  20. unsigned int cm[SONAR_NUM]; // Where the ping distances are stored.
  21. uint8_t currentSensor = 0; // Keeps track of which sensor is active.
  22. NewPing sonar[SONAR_NUM] = { // Sensor object array.
  23. NewPing(41, 42, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  24. NewPing(43, 44, MAX_DISTANCE),
  25. NewPing(45, 20, MAX_DISTANCE),
  26. NewPing(21, 22, MAX_DISTANCE),
  27. NewPing(23, 24, MAX_DISTANCE),
  28. NewPing(25, 26, MAX_DISTANCE),
  29. NewPing(27, 28, MAX_DISTANCE),
  30. NewPing(29, 30, MAX_DISTANCE),
  31. NewPing(31, 32, MAX_DISTANCE),
  32. NewPing(34, 33, MAX_DISTANCE),
  33. NewPing(35, 36, MAX_DISTANCE),
  34. NewPing(37, 38, MAX_DISTANCE),
  35. NewPing(39, 40, MAX_DISTANCE),
  36. NewPing(50, 51, MAX_DISTANCE),
  37. NewPing(52, 53, MAX_DISTANCE)
  38. };
  39. void setup() {
  40. Serial.begin(115200);
  41. pingTimer[0] = millis() + 75; // First ping starts at 75ms, gives time for the Arduino to chill before starting.
  42. for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
  43. pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
  44. }
  45. void loop() {
  46. for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
  47. if (millis() >= pingTimer[i]) { // Is it this sensor's time to ping?
  48. pingTimer[i] += PING_INTERVAL * SONAR_NUM; // Set next time this sensor will be pinged.
  49. if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
  50. sonar[currentSensor].timer_stop(); // Make sure previous timer is canceled before starting a new ping (insurance).
  51. currentSensor = i; // Sensor being accessed.
  52. cm[currentSensor] = 0; // Make distance zero in case there's no ping echo for this sensor.
  53. sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
  54. }
  55. }
  56. // Other code that *DOESN'T* analyze ping results can go here.
  57. }
  58. void echoCheck() { // If ping received, set the sensor distance to array.
  59. if (sonar[currentSensor].check_timer())
  60. cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
  61. }
  62. void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
  63. // The following code would be replaced with your code that does something with the ping results.
  64. for (uint8_t i = 0; i < SONAR_NUM; i++) {
  65. Serial.print(i);
  66. Serial.print("=");
  67. Serial.print(cm[i]);
  68. Serial.print("cm ");
  69. }
  70. Serial.println();
  71. }