Diff: hcsr04.h
- Revision:
- 1:c7753ec66a4b
- Parent:
- 0:c587e0d5adda
- Child:
- 2:d99d91a7e8f1
diff -r c587e0d5adda -r c7753ec66a4b hcsr04.h --- a/hcsr04.h Fri Mar 16 03:36:59 2012 +0000 +++ b/hcsr04.h Sun Mar 18 21:01:49 2012 +0000 @@ -5,7 +5,8 @@ // 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. -// +//New comment + #ifndef MBED_HCSR04_H #define MBED_HCSR04_H @@ -20,15 +21,15 @@ 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 - // int timestart; // beginning time of echo pulse - float value; // to store the last pulse length + 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 + // int timestart; // beginning time of echo 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 + 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); @@ -40,31 +41,31 @@ void calc_measurement() { value = timer.read_us(); - //value = timer.read_us() - timestart; - timer.stop(); - timer.reset(); - measuring = false; + //value = timer.read_us() - timestart; + timer.stop(); + timer.reset(); + measuring = false; } - + void timer_start() { - this->timer.start(); - measuring = true; + this->timer.start(); + measuring = true; } - + void trigger(void) { - trigger_out.write(1); // start trigger signal + trigger_out.write(1); // start trigger signal wait_us(TRIGGER_DELAY); - trigger_out.write(0); // end trigger signal + trigger_out.write(0); // end trigger signal } - + float inches() { return value / INCHES_DIVISOR; } - + float cm() { - return value /CM_DIVISOR; + return value /CM_DIVISOR; } - + }; #endif \ No newline at end of file