NewPingTimerMedian.pde 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // ---------------------------------------------------------------------------
  2. // Calculate a ping median using the ping_timer() method.
  3. // ---------------------------------------------------------------------------
  4. #include <NewPing.h>
  5. #define ITERATIONS 5 // Number of iterations.
  6. #define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on ping sensor.
  7. #define ECHO_PIN 11 // Arduino pin tied to echo pin on ping sensor.
  8. #define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
  9. #define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
  10. unsigned long pingTimer[ITERATIONS]; // Holds the times when the next ping should happen for each iteration.
  11. unsigned int cm[ITERATIONS]; // Where the ping distances are stored.
  12. uint8_t currentIteration = 0; // Keeps track of iteration step.
  13. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
  14. void setup() {
  15. Serial.begin(115200);
  16. pingTimer[0] = millis() + 75; // First ping starts at 75ms, gives time for the Arduino to chill before starting.
  17. for (uint8_t i = 1; i < ITERATIONS; i++) // Set the starting time for each iteration.
  18. pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
  19. }
  20. void loop() {
  21. for (uint8_t i = 0; i < ITERATIONS; i++) { // Loop through all the iterations.
  22. if (millis() >= pingTimer[i]) { // Is it this iteration's time to ping?
  23. pingTimer[i] += PING_INTERVAL * ITERATIONS; // Set next time this sensor will be pinged.
  24. if (i == 0 && currentIteration == ITERATIONS - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
  25. sonar.timer_stop(); // Make sure previous timer is canceled before starting a new ping (insurance).
  26. currentIteration = i; // Sensor being accessed.
  27. cm[currentIteration] = 0; // Make distance zero in case there's no ping echo for this iteration.
  28. sonar.ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
  29. }
  30. }
  31. // Other code that *DOESN'T* analyze ping results can go here.
  32. }
  33. void echoCheck() { // If ping received, set the sensor distance to array.
  34. if (sonar.check_timer())
  35. cm[currentIteration] = sonar.ping_result / US_ROUNDTRIP_CM;
  36. }
  37. void oneSensorCycle() { // All iterations complete, calculate the median.
  38. unsigned int uS[ITERATIONS];
  39. uint8_t j, it = ITERATIONS;
  40. uS[0] = NO_ECHO;
  41. for (uint8_t i = 0; i < it; i++) { // Loop through iteration results.
  42. if (cm[i] != NO_ECHO) { // Ping in range, include as part of median.
  43. if (i > 0) { // Don't start sort till second ping.
  44. for (j = i; j > 0 && uS[j - 1] < cm[i]; j--) // Insertion sort loop.
  45. uS[j] = uS[j - 1]; // Shift ping array to correct position for sort insertion.
  46. } else j = 0; // First ping is sort starting point.
  47. uS[j] = cm[i]; // Add last ping to array in sorted position.
  48. } else it--; // Ping out of range, skip and don't include as part of median.
  49. }
  50. Serial.print(uS[it >> 1]);
  51. Serial.println("cm");
  52. }