Skip to content

Commit

Permalink
added support for specifying the trigger length and waiting after tha…
Browse files Browse the repository at this point in the history
…t before expecting the echo
  • Loading branch information
d03n3rfr1tz3 committed Jun 7, 2021
1 parent 92f4628 commit 95e10a3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/HCSR04.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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++) {
Expand Down
5 changes: 4 additions & 1 deletion src/HCSR04.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 95e10a3

Please # to comment.