hcsr04.h@1:c7753ec66a4b, 2012-03-18 (annotated)
- Committer:
- dglover77
- Date:
- Sun Mar 18 21:01:49 2012 +0000
- Revision:
- 1:c7753ec66a4b
- Parent:
- 0:c587e0d5adda
- Child:
- 2:d99d91a7e8f1
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:c7753ec66a4b | 8 | //New comment |
dglover77 | 1:c7753ec66a4b | 9 | |
dglover77 | 0:c587e0d5adda | 10 | |
dglover77 | 0:c587e0d5adda | 11 | #ifndef MBED_HCSR04_H |
dglover77 | 0:c587e0d5adda | 12 | #define MBED_HCSR04_H |
dglover77 | 0:c587e0d5adda | 13 | |
dglover77 | 0:c587e0d5adda | 14 | //required to use mbed functions |
dglover77 | 0:c587e0d5adda | 15 | #include "mbed.h" |
dglover77 | 0:c587e0d5adda | 16 | |
dglover77 | 0:c587e0d5adda | 17 | #define TRIGGER_DELAY 12 // length of trigger signal expected by HCSR04 sensor |
dglover77 | 0:c587e0d5adda | 18 | #define INCHES_DIVISOR 148 // |
dglover77 | 0:c587e0d5adda | 19 | #define CM_DIVISOR 58 |
dglover77 | 0:c587e0d5adda | 20 | |
dglover77 | 0:c587e0d5adda | 21 | class hcsr04 { |
dglover77 | 0:c587e0d5adda | 22 | |
dglover77 | 0:c587e0d5adda | 23 | private: |
dglover77 | 1:c7753ec66a4b | 24 | InterruptIn *_echo_int; // pin to receive echo signal |
dglover77 | 1:c7753ec66a4b | 25 | DigitalOut trigger_out; // pin to send the trigger signal |
dglover77 | 1:c7753ec66a4b | 26 | Timer timer; // timer to track length of pulse |
dglover77 | 1:c7753ec66a4b | 27 | // int timestart; // beginning time of echo pulse |
dglover77 | 1:c7753ec66a4b | 28 | float value; // to store the last pulse length |
dglover77 | 0:c587e0d5adda | 29 | |
dglover77 | 0:c587e0d5adda | 30 | public: |
dglover77 | 0:c587e0d5adda | 31 | bool measuring; // true while the echo signal is high (measurement in progress) |
dglover77 | 1:c7753ec66a4b | 32 | hcsr04(PinName trigger, PinName echo) : trigger_out(trigger) { // _pass the names to the pin configuration |
dglover77 | 0:c587e0d5adda | 33 | // _trigger_out = new DigitalOut( trigger ); |
dglover77 | 0:c587e0d5adda | 34 | _echo_int = new InterruptIn( echo ); |
dglover77 | 0:c587e0d5adda | 35 | _echo_int->rise(this, &hcsr04::timer_start); |
dglover77 | 0:c587e0d5adda | 36 | _echo_int->fall(this, &hcsr04::calc_measurement); |
dglover77 | 0:c587e0d5adda | 37 | measuring = false; |
dglover77 | 0:c587e0d5adda | 38 | } |
dglover77 | 0:c587e0d5adda | 39 | |
dglover77 | 0:c587e0d5adda | 40 | |
dglover77 | 0:c587e0d5adda | 41 | |
dglover77 | 0:c587e0d5adda | 42 | void calc_measurement() { |
dglover77 | 0:c587e0d5adda | 43 | value = timer.read_us(); |
dglover77 | 1:c7753ec66a4b | 44 | //value = timer.read_us() - timestart; |
dglover77 | 1:c7753ec66a4b | 45 | timer.stop(); |
dglover77 | 1:c7753ec66a4b | 46 | timer.reset(); |
dglover77 | 1:c7753ec66a4b | 47 | measuring = false; |
dglover77 | 0:c587e0d5adda | 48 | } |
dglover77 | 1:c7753ec66a4b | 49 | |
dglover77 | 0:c587e0d5adda | 50 | void timer_start() { |
dglover77 | 1:c7753ec66a4b | 51 | this->timer.start(); |
dglover77 | 1:c7753ec66a4b | 52 | measuring = true; |
dglover77 | 0:c587e0d5adda | 53 | } |
dglover77 | 1:c7753ec66a4b | 54 | |
dglover77 | 0:c587e0d5adda | 55 | void trigger(void) { |
dglover77 | 1:c7753ec66a4b | 56 | trigger_out.write(1); // start trigger signal |
dglover77 | 0:c587e0d5adda | 57 | wait_us(TRIGGER_DELAY); |
dglover77 | 1:c7753ec66a4b | 58 | trigger_out.write(0); // end trigger signal |
dglover77 | 0:c587e0d5adda | 59 | } |
dglover77 | 1:c7753ec66a4b | 60 | |
dglover77 | 0:c587e0d5adda | 61 | float inches() { |
dglover77 | 0:c587e0d5adda | 62 | return value / INCHES_DIVISOR; |
dglover77 | 0:c587e0d5adda | 63 | } |
dglover77 | 1:c7753ec66a4b | 64 | |
dglover77 | 0:c587e0d5adda | 65 | float cm() { |
dglover77 | 1:c7753ec66a4b | 66 | return value /CM_DIVISOR; |
dglover77 | 0:c587e0d5adda | 67 | } |
dglover77 | 1:c7753ec66a4b | 68 | |
dglover77 | 0:c587e0d5adda | 69 | }; |
dglover77 | 0:c587e0d5adda | 70 | |
dglover77 | 0:c587e0d5adda | 71 | #endif |