NewPingEventTimer.pde 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // ---------------------------------------------------------------------------
  2. // This example shows how to use NewPing's ping_timer method which uses the Timer2 interrupt to get the
  3. // ping time. The advantage of using this method over the standard ping method is that it permits a more
  4. // event-driven sketch which allows you to appear to do two things at once. An example would be to ping
  5. // an ultrasonic sensor for a possible collision while at the same time navigating. This allows a
  6. // properly developed sketch to multitask. Be aware that because the ping_timer method uses Timer2,
  7. // other features or libraries that also use Timer2 would be effected. For example, the PWM function on
  8. // pins 3 & 11 on Arduino Uno (pins 9 and 11 on Arduino Mega) and the Tone library. Note, only the PWM
  9. // functionality of the pins is lost (as they use Timer2 to do PWM), the pins are still available to use.
  10. // NOTE: For Teensy/Leonardo (ATmega32U4) the library uses Timer4 instead of Timer2.
  11. // ---------------------------------------------------------------------------
  12. #include <NewPing.h>
  13. #define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on ping sensor.
  14. #define ECHO_PIN 11 // Arduino pin tied to echo pin on ping sensor.
  15. #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
  16. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
  17. unsigned int pingSpeed = 50; // How frequently are we going to send out a ping (in milliseconds). 50ms would be 20 times a second.
  18. unsigned long pingTimer; // Holds the next ping time.
  19. void setup() {
  20. Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  21. pingTimer = millis(); // Start now.
  22. }
  23. void loop() {
  24. // Notice how there's no delays in this sketch to allow you to do other processing in-line while doing distance pings.
  25. if (millis() >= pingTimer) { // pingSpeed milliseconds since last ping, do another ping.
  26. pingTimer += pingSpeed; // Set the next ping time.
  27. sonar.ping_timer(echoCheck); // Send out the ping, calls "echoCheck" function every 24uS where you can check the ping status.
  28. }
  29. // Do other stuff here, really. Think of it as multi-tasking.
  30. }
  31. void echoCheck() { // Timer2 interrupt calls this function every 24uS where you can check the ping status.
  32. // Don't do anything here!
  33. if (sonar.check_timer()) { // This is how you check to see if the ping was received.
  34. // Here's where you can add code.
  35. Serial.print("Ping: ");
  36. Serial.print(sonar.ping_result / US_ROUNDTRIP_CM); // Ping returned, uS result in ping_result, convert to cm with US_ROUNDTRIP_CM.
  37. Serial.println("cm");
  38. }
  39. // Don't do anything here!
  40. }