3 #include "SonicRanger.h" 5 #ifdef ARDUINO_AVR_DIGISPARK 8 uint32_t pulseInLong(uint8_t pin, uint8_t state, uint32_t timeout)
13 uint8_t bit = digitalPinToBitMask(pin);
14 uint8_t port = digitalPinToPort(pin);
15 uint8_t stateMask = (state ? bit : 0);
17 uint32_t startMicros = micros();
20 while ((*portInputRegister(port) & bit) == stateMask) {
21 if (micros() - startMicros > timeout)
26 while ((*portInputRegister(port) & bit) != stateMask) {
27 if (micros() - startMicros > timeout)
31 uint32_t start = micros();
33 while ((*portInputRegister(port) & bit) == stateMask) {
34 if (micros() - startMicros > timeout)
37 return micros() - start;
39 #endif // ARDUINO_AVR_DIGISPARK 44 _maxCm(DefaultMaxRangeCm),
45 _timeoutMs(DefaultTimeoutMs)
51 pinMode(_trigPin, OUTPUT);
52 pinMode(_echoPin, INPUT);
57 digitalWrite(_trigPin, LOW);
59 digitalWrite(_trigPin, HIGH);
60 delayMicroseconds(10);
61 uint32_t b4 = Millis();
62 digitalWrite(_trigPin, LOW);
63 uint32_t time = pulseInLong(_echoPin, HIGH, _timeoutMs*1000UL);
64 uint32_t after = Millis();
66 if (MillisSince(b4, after) >= _timeoutMs) {
SonicRanger(const uint8_t trigPin, const uint8_t echoPin)