Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. #include <NewPing.h>
  2.  
  3. #define TRIGGER_PIN 5 // Arduino pin tied to trigger pin on the ultrasonic sensor.
  4. #define ECHO_PIN 4 // Arduino pin tied to echo pin on the ultrasonic sensor.
  5. #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
  6.  
  7. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
  8.  
  9. int personCounter;
  10. int currentState = 0;
  11. int previousState = 0;
  12. int maxDistanceRange = 100;
  13. int minDistanceRange = 10;
  14. int currentPingedDistance;
  15. //int humanPing = sonar.ping_median(iterations);
  16. int pingCounter;
  17. int pingsOnObject;
  18.  
  19. //int objectInRange = currentPingedDistance <= maxDistanceRange && currentPingedDistance > minDistanceRange;
  20.  
  21. void setup() {
  22. Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  23. }
  24.  
  25. void loop() {
  26.  
  27. if (sonar.ping_cm() <= maxDistanceRange && sonar.ping_cm() > minDistanceRange) {
  28.  
  29. pingsOnObject = 1;
  30.  
  31. while (sonar.ping_cm() <= maxDistanceRange && sonar.ping_cm() > minDistanceRange)
  32. {
  33. delay(50);
  34. sonar.ping();
  35. Serial.print(sonar.ping_cm());
  36. pingsOnObject++;
  37. Serial.print("Pings");
  38. Serial.print(" - ");
  39. Serial.println(pingsOnObject);
  40. if (pingsOnObject >= 5)
  41. {
  42. break;
  43. }
  44. else {
  45. continue;
  46. }
  47. }
  48.  
  49.  
  50. if (pingsOnObject >= 5) {
  51.  
  52. pingCounter = 1;
  53. currentState = 1;
  54.  
  55. while (sonar.ping_cm() <= maxDistanceRange && sonar.ping_cm() > minDistanceRange) {
  56. // delay(500);
  57. pingCounter ++;
  58. }
  59. Serial.print("Pings On Person");
  60. Serial.print(" - ");
  61. Serial.println(pingCounter + 5);
  62. }
  63. else {
  64.  
  65. currentState = 0;
  66. }
  67.  
  68. if (currentState != previousState) {
  69. if (currentState == 1) {
  70. personCounter = personCounter + 1;
  71.  
  72. Serial.print("Persons");
  73. Serial.print(" - ");
  74. Serial.println(personCounter);
  75. }
  76. }
  77.  
  78. } else {
  79.  
  80. pingsOnObject = 0;
  81. }
  82.  
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement