Added timeout functionality

Fork of HCSR04 by Antoniolinux B.

Committer:
EmbeddedSam
Date:
Mon Oct 24 12:59:41 2016 +0000
Revision:
1:e01896ab28d7
Parent:
0:86b2086be101
added timeout functionality

Who changed what in which revision?

UserRevisionLine numberNew contents of line
antoniolinux 0:86b2086be101 1 #include "hcsr04.h"
antoniolinux 0:86b2086be101 2 #include "mbed.h"
antoniolinux 0:86b2086be101 3 /*
antoniolinux 0:86b2086be101 4 *HCSR04.cpp
antoniolinux 0:86b2086be101 5 */
antoniolinux 0:86b2086be101 6 HCSR04::HCSR04(PinName t, PinName e) : trig(t), echo(e) {}
antoniolinux 0:86b2086be101 7 long HCSR04::echo_duration() {
antoniolinux 0:86b2086be101 8
antoniolinux 0:86b2086be101 9 timer.reset(); //reset timer
EmbeddedSam 1:e01896ab28d7 10 timeout_timer.reset();
EmbeddedSam 1:e01896ab28d7 11
EmbeddedSam 1:e01896ab28d7 12 //Initiate Trigger
antoniolinux 0:86b2086be101 13 trig=0; // trigger low
antoniolinux 0:86b2086be101 14 wait_us(2); // wait
antoniolinux 0:86b2086be101 15 trig=1; // trigger high
antoniolinux 0:86b2086be101 16 wait_us(10);
antoniolinux 0:86b2086be101 17 trig=0; // trigger low
EmbeddedSam 1:e01896ab28d7 18
EmbeddedSam 1:e01896ab28d7 19 timeout_timer.start();
EmbeddedSam 1:e01896ab28d7 20
EmbeddedSam 1:e01896ab28d7 21 while(!echo){
EmbeddedSam 1:e01896ab28d7 22 //Wait for 5V/3.3V pulse to start
EmbeddedSam 1:e01896ab28d7 23 if(timeout_timer.read_ms()>=10){ //100 ms timeout
EmbeddedSam 1:e01896ab28d7 24 timedOut = true;
EmbeddedSam 1:e01896ab28d7 25 break;
EmbeddedSam 1:e01896ab28d7 26 }
EmbeddedSam 1:e01896ab28d7 27 }
EmbeddedSam 1:e01896ab28d7 28
EmbeddedSam 1:e01896ab28d7 29 //Pulse Started
EmbeddedSam 1:e01896ab28d7 30 if(timedOut == true){
EmbeddedSam 1:e01896ab28d7 31 //Error, pulse didn't start for some reason
EmbeddedSam 1:e01896ab28d7 32 timedOut = false;
EmbeddedSam 1:e01896ab28d7 33 return 999999; //error pulse not started in time
EmbeddedSam 1:e01896ab28d7 34 }
EmbeddedSam 1:e01896ab28d7 35 else
EmbeddedSam 1:e01896ab28d7 36 {
EmbeddedSam 1:e01896ab28d7 37 timeout_timer.reset();
EmbeddedSam 1:e01896ab28d7 38 timer.start();
EmbeddedSam 1:e01896ab28d7 39 timeout_timer.start();
EmbeddedSam 1:e01896ab28d7 40 while(echo){
EmbeddedSam 1:e01896ab28d7 41 if(timeout_timer.read_ms()>=10){
EmbeddedSam 1:e01896ab28d7 42 timedOut = true;
EmbeddedSam 1:e01896ab28d7 43 break;
EmbeddedSam 1:e01896ab28d7 44 }
EmbeddedSam 1:e01896ab28d7 45 }
EmbeddedSam 1:e01896ab28d7 46 if(timedOut == true){
EmbeddedSam 1:e01896ab28d7 47 timedOut = false;
EmbeddedSam 1:e01896ab28d7 48 return 999999;
EmbeddedSam 1:e01896ab28d7 49 }
EmbeddedSam 1:e01896ab28d7 50 else{
EmbeddedSam 1:e01896ab28d7 51 timer.stop();
EmbeddedSam 1:e01896ab28d7 52 return timer.read_us();
EmbeddedSam 1:e01896ab28d7 53 }
EmbeddedSam 1:e01896ab28d7 54 }
EmbeddedSam 1:e01896ab28d7 55
EmbeddedSam 1:e01896ab28d7 56
EmbeddedSam 1:e01896ab28d7 57
EmbeddedSam 1:e01896ab28d7 58
antoniolinux 0:86b2086be101 59
antoniolinux 0:86b2086be101 60 }
antoniolinux 0:86b2086be101 61
antoniolinux 0:86b2086be101 62 //return distance in cm
antoniolinux 0:86b2086be101 63 long HCSR04::distance(){
antoniolinux 0:86b2086be101 64 duration = echo_duration();
antoniolinux 0:86b2086be101 65 distance_cm = (duration/2)/29.1 ;
antoniolinux 0:86b2086be101 66 return distance_cm;
antoniolinux 0:86b2086be101 67
antoniolinux 0:86b2086be101 68 }