Committer:
dglover77
Date:
Fri Mar 16 03:36:59 2012 +0000
Revision:
0:c587e0d5adda
Child:
1:c7753ec66a4b

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dglover77 0:c587e0d5adda 1 // HCSR04 ultrasonic sensor class using interrupts and allowing multiple instances.
dglover77 0:c587e0d5adda 2 // You can use the same trigger pin for multiple instances, the echo is recieved and
dglover77 0:c587e0d5adda 3 // measurement calculated based on the echo input alone (when the echo signal goes high
dglover77 0:c587e0d5adda 4 // the timer is started, when the echo signal is low the timer value in microseconds
dglover77 0:c587e0d5adda 5 // is saved as the length, and the timer is stopped and reset. Length calculation is done
dglover77 0:c587e0d5adda 6 // when the length is requested (either inches or centimeters) using the appropriate
dglover77 0:c587e0d5adda 7 // function.
dglover77 0:c587e0d5adda 8 //
dglover77 0:c587e0d5adda 9
dglover77 0:c587e0d5adda 10 #ifndef MBED_HCSR04_H
dglover77 0:c587e0d5adda 11 #define MBED_HCSR04_H
dglover77 0:c587e0d5adda 12
dglover77 0:c587e0d5adda 13 //required to use mbed functions
dglover77 0:c587e0d5adda 14 #include "mbed.h"
dglover77 0:c587e0d5adda 15
dglover77 0:c587e0d5adda 16 #define TRIGGER_DELAY 12 // length of trigger signal expected by HCSR04 sensor
dglover77 0:c587e0d5adda 17 #define INCHES_DIVISOR 148 //
dglover77 0:c587e0d5adda 18 #define CM_DIVISOR 58
dglover77 0:c587e0d5adda 19
dglover77 0:c587e0d5adda 20 class hcsr04 {
dglover77 0:c587e0d5adda 21
dglover77 0:c587e0d5adda 22 private:
dglover77 0:c587e0d5adda 23 InterruptIn *_echo_int; // pin to receive echo signal
dglover77 0:c587e0d5adda 24 DigitalOut trigger_out; // pin to send the trigger signal
dglover77 0:c587e0d5adda 25 Timer timer; // timer to track length of pulse
dglover77 0:c587e0d5adda 26 // int timestart; // beginning time of echo pulse
dglover77 0:c587e0d5adda 27 float value; // to store the last pulse length
dglover77 0:c587e0d5adda 28
dglover77 0:c587e0d5adda 29 public:
dglover77 0:c587e0d5adda 30 bool measuring; // true while the echo signal is high (measurement in progress)
dglover77 0:c587e0d5adda 31 hcsr04(PinName trigger, PinName echo) : trigger_out(trigger) { // _pass the names to the pin configuration
dglover77 0:c587e0d5adda 32 // _trigger_out = new DigitalOut( trigger );
dglover77 0:c587e0d5adda 33 _echo_int = new InterruptIn( echo );
dglover77 0:c587e0d5adda 34 _echo_int->rise(this, &hcsr04::timer_start);
dglover77 0:c587e0d5adda 35 _echo_int->fall(this, &hcsr04::calc_measurement);
dglover77 0:c587e0d5adda 36 measuring = false;
dglover77 0:c587e0d5adda 37 }
dglover77 0:c587e0d5adda 38
dglover77 0:c587e0d5adda 39
dglover77 0:c587e0d5adda 40
dglover77 0:c587e0d5adda 41 void calc_measurement() {
dglover77 0:c587e0d5adda 42 value = timer.read_us();
dglover77 0:c587e0d5adda 43 //value = timer.read_us() - timestart;
dglover77 0:c587e0d5adda 44 timer.stop();
dglover77 0:c587e0d5adda 45 timer.reset();
dglover77 0:c587e0d5adda 46 measuring = false;
dglover77 0:c587e0d5adda 47 }
dglover77 0:c587e0d5adda 48
dglover77 0:c587e0d5adda 49 void timer_start() {
dglover77 0:c587e0d5adda 50 this->timer.start();
dglover77 0:c587e0d5adda 51 measuring = true;
dglover77 0:c587e0d5adda 52 }
dglover77 0:c587e0d5adda 53
dglover77 0:c587e0d5adda 54 void trigger(void) {
dglover77 0:c587e0d5adda 55 trigger_out.write(1); // start trigger signal
dglover77 0:c587e0d5adda 56 wait_us(TRIGGER_DELAY);
dglover77 0:c587e0d5adda 57 trigger_out.write(0); // end trigger signal
dglover77 0:c587e0d5adda 58 }
dglover77 0:c587e0d5adda 59
dglover77 0:c587e0d5adda 60 float inches() {
dglover77 0:c587e0d5adda 61 return value / INCHES_DIVISOR;
dglover77 0:c587e0d5adda 62 }
dglover77 0:c587e0d5adda 63
dglover77 0:c587e0d5adda 64 float cm() {
dglover77 0:c587e0d5adda 65 return value /CM_DIVISOR;
dglover77 0:c587e0d5adda 66 }
dglover77 0:c587e0d5adda 67
dglover77 0:c587e0d5adda 68 };
dglover77 0:c587e0d5adda 69
dglover77 0:c587e0d5adda 70 #endif