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