diff --git a/src/HCSR04.cpp b/src/HCSR04.cpp index f056a0f..6b07a7c 100644 --- a/src/HCSR04.cpp +++ b/src/HCSR04.cpp @@ -9,13 +9,15 @@ HCSR04Sensor::HCSR04Sensor() {} HCSR04Sensor::~HCSR04Sensor() { this->end(); } -void HCSR04Sensor::begin(uint8_t triggerPin, uint8_t* echoPins, uint8_t echoCount, uint32_t timeout, eUltraSonicUnlock_t unlock) { +void HCSR04Sensor::begin(uint8_t triggerPin, uint8_t* echoPins, uint8_t echoCount, uint32_t timeout, uint16_t triggerTime, uint16_t triggerWait, eUltraSonicUnlock_t unlock) { if (this->echoCount != echoCount) this->end(); this->triggerPin = triggerPin; pinMode(triggerPin, OUTPUT); this->timeout = timeout; + this->triggerTime = triggerTime; + this->triggerWait = triggerWait; this->echoCount = echoCount; if (this->lastMicroseconds == NULL) this->lastMicroseconds = new long[echoCount]; @@ -82,10 +84,13 @@ void HCSR04Sensor::measureMicroseconds(long* results) { digitalWrite(triggerPin, LOW); delayMicroseconds(4); - // Hold trigger for 10 microseconds, which is signal for sensor to measure distance. + // Hold trigger HIGH for 10 microseconds (default), which signals the sensor to measure distance. digitalWrite(triggerPin, HIGH); - delayMicroseconds(11); + delayMicroseconds(this->triggerTime); + + // Set trigger LOW again and wait to give the sensor time for sending the signal without interference digitalWrite(triggerPin, LOW); + delayMicroseconds(this->triggerWait); // Attach interrupts to echo pins for the starting point for (uint8_t i = 0; i < this->echoCount; i++) { diff --git a/src/HCSR04.h b/src/HCSR04.h index 8e5ff22..3cabbb5 100644 --- a/src/HCSR04.h +++ b/src/HCSR04.h @@ -26,7 +26,8 @@ class HCSR04Sensor { void begin(uint8_t triggerPin, uint8_t echoPin) { begin(triggerPin, new uint8_t[1]{ echoPin }, 1); } void begin(uint8_t triggerPin, uint8_t* echoPins, uint8_t echoCount) { begin(triggerPin, echoPins, echoCount, 100000, eUltraSonicUnlock_t::unlockSkip); } void begin(uint8_t triggerPin, uint8_t echoPin, uint32_t timeout, eUltraSonicUnlock_t unlock) { begin(triggerPin, new uint8_t[1]{ echoPin }, 1, timeout, unlock); } - void begin(uint8_t triggerPin, uint8_t* echoPins, uint8_t echoCount, uint32_t timeout, eUltraSonicUnlock_t unlock); + void begin(uint8_t triggerPin, uint8_t* echoPins, uint8_t echoCount, uint32_t timeout, eUltraSonicUnlock_t unlock) { begin(triggerPin, echoPins, echoCount, timeout, 10, 10, unlock); } + void begin(uint8_t triggerPin, uint8_t* echoPins, uint8_t echoCount, uint32_t timeout, uint16_t triggerTime, uint16_t triggerWait, eUltraSonicUnlock_t unlock); void end(); long* measureMicroseconds() { measureMicroseconds(lastMicroseconds); return lastMicroseconds; } @@ -75,6 +76,8 @@ class HCSR04Sensor { double* lastDistances; uint32_t timeout; + uint16_t triggerTime = 10; // HC-SR04 needs at least 10µs trigger. Others may need longer trigger pulses. + uint16_t triggerWait = 10; // HC-SR04 sends its signal about 200µs. We only wait a small amount to reduce interference, but to not miss anything on slower clock speeds. volatile uint8_t triggerPin; volatile unsigned long* volatile triggerTimes;