NewPing3Sensors.pde 1.0 KB

1234567891011121314151617181920212223242526272829
  1. // ---------------------------------------------------------------------------
  2. // Example NewPing library sketch that pings 3 sensors 20 times a second.
  3. // ---------------------------------------------------------------------------
  4. #include <NewPing.h>
  5. #define SONAR_NUM 3 // Number of sensors.
  6. #define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
  7. NewPing sonar[SONAR_NUM] = { // Sensor object array.
  8. NewPing(4, 5, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  9. NewPing(6, 7, MAX_DISTANCE),
  10. NewPing(8, 9, MAX_DISTANCE)
  11. };
  12. void setup() {
  13. Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  14. }
  15. void loop() {
  16. for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
  17. delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  18. Serial.print(i);
  19. Serial.print("=");
  20. Serial.print(sonar[i].ping_cm());
  21. Serial.print("cm ");
  22. }
  23. Serial.println();
  24. }