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