Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. ir sensors
  2. const pinArray_t irReflectancePins[] = {25,27,24,26};
  3. int asipDistanceClass::getDistance(int sequenceId)
  4. {
  5. const long MAX_DISTANCE = 100;
  6. const long MAX_DURATION = (MAX_DISTANCE * 58);
  7.  
  8. // The sensor is triggered by a HIGH pulse of 2 or more microseconds.
  9. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  10. byte pin = pins[sequenceId];
  11. pinMode(pin, OUTPUT);
  12. digitalWrite(pin, LOW);
  13. delayMicroseconds(4);
  14. digitalWrite(pin, HIGH);
  15. delayMicroseconds(10);
  16. digitalWrite(pin, LOW);
  17.  
  18. pinMode(pin, INPUT);
  19.  
  20. // limit pulseIn duration to a max of 275cm (just under 16ms)
  21. // if pulse does not arrive in this time then ping sensor may not be connected
  22. // if you need to increase this then you must change the distanceSensorDataRequest message body size
  23. long duration = pulseIn(pin, HIGH, MAX_DURATION);
  24. // convert the time into a distance
  25. // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  26. // The ping travels out and back, so to find the distance of the
  27. // object we take half of the distance travelled.
  28. int cm = (duration / 29) / 2;
  29. return cm;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement