Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of IEEE_14_Freescale by
hcsr04.h
- Committer:
- soonerbot
- Date:
- 2014-03-07
- Revision:
- 14:a30aa3b29a2e
- Child:
- 15:b10859606504
File content as of revision 14:a30aa3b29a2e:
// Primary Author: David Glover
// March, 2012
// ECE 510 Embedded Systems, Roy Kravitz
//
// HCSR04 ultrasonic sensor class using interrupts and allowing multiple instances.
// You can use the same trigger pin for multiple instances, the echo is received and
// measurement calculated based on the echo input alone (when the echo signal goes high
// the timer is started, when the echo signal goes low the timer value in microseconds
// is saved as the length, and the timer is stopped and reset. Length calculation is done
// when the length is requested (either inches or centimeters) using the appropriate
// function.
#ifndef MBED_HCSR04_H
#define MBED_HCSR04_H
//required to use mbed functions
#include "mbed.h"
#define TRIGGER_DELAY 12 // length of trigger signal expected by HCSR04 sensor
#define INCHES_DIVISOR 148 //
#define CM_DIVISOR 58
class hcsr04 {
private:
InterruptIn *_echo_int; // pin to receive echo signal
DigitalOut trigger_out; // pin to send the trigger signal
Timer timer; // timer to track length of pulse
float value; // to store the last pulse length
public:
bool measuring; // true while the echo signal is high (measurement in progress)
hcsr04(PinName trigger, PinName echo) : trigger_out(trigger) { // _pass the names to the pin configuration
//trigger_out = new DigitalOut( trigger );
_echo_int = new InterruptIn( echo );
_echo_int->rise(this, &hcsr04::timer_start);
_echo_int->fall(this, &hcsr04::calc_measurement);
measuring = false;
}
void calc_measurement() {
value = timer.read_us();
//value = timer.read_us() - timestart;
timer.stop();
timer.reset();
measuring = false;
}
void timer_start() {
this->timer.start();
measuring = true;
}
void trigger(void) {
trigger_out.write(1); // start trigger signal
wait_us(TRIGGER_DELAY);
trigger_out.write(0); // end trigger signal
}
float inches() { // return distance in inches.
return value / INCHES_DIVISOR;
}
float cm() { // return distance in centimeters.
return value / CM_DIVISOR;
}
/* void pulse(void) { // Rohan's code
trigger();
while(measuring == false);
// interrupts call timer_start() and calc_measurement()
}
*/
};
#endif
