Ultrasound Ranging Sensor module

Dependents:   Lets_move_Seki2018_ver2 Lets_move_Seki2018_null UNKO_FINAL_PlamA_2 UNKO_FINAL_PlanA

Fork of HCSR04 by Prabhu Desai

Files at this revision

API Documentation at this revision

Comitter:
yuron
Date:
Wed Sep 05 15:06:24 2018 +0000
Parent:
7:71da0dbf4400
Commit message:
2018/09/05???B??????????????(??????????)

Changed in this revision

hcsr04.cpp Show annotated file Show diff for this revision Revisions of this file
hcsr04.h Show annotated file Show diff for this revision Revisions of this file
diff -r 71da0dbf4400 -r e150242bce6b hcsr04.cpp
--- a/hcsr04.cpp	Sat Mar 30 18:55:03 2013 +0000
+++ b/hcsr04.cpp	Wed Sep 05 15:06:24 2018 +0000
@@ -24,6 +24,7 @@
 HCSR04::HCSR04(PinName TrigPin,PinName EchoPin):
     trigger(TrigPin), echo(EchoPin)
 {
+    setTemp(15);
     pulsetime.stop();
     pulsetime.reset();
     echo.rise(this,&HCSR04::isr_rise);
@@ -35,22 +36,36 @@
 {
 }
 
-void HCSR04::isr_rise(void)
-{
-    pulsetime.start();
-}
 void HCSR04::start(void)
 {
+    pulsetime_us = pulsetime.read_us();
+    if(pulsetime_us > maxTime){
+        pulsetime.stop();
+        pulsetime.reset();
+        trigger=1;
+        trigger=0;
+    }
     trigger=1;
     wait_us(10);
     trigger=0;
 }
 
+void HCSR04::isr_rise(void)
+{
+    pulsetime.start();
+}
+
 void HCSR04::isr_fall(void)
 {
     pulsetime.stop();
-    pulsedur = pulsetime.read_us();
-    distance= (pulsedur*343)/20000;
+    pulsetime_us = pulsetime.read_us();
+    if(pulsetime_us > maxTime){
+        trigger=1;
+        trigger=0;
+        distance = 0;
+    }else{
+    distance = pulsetime_us*sonic/20000;
+    }
     pulsetime.reset();
 }
 
@@ -67,13 +82,13 @@
 {
     return distance;
 }
-unsigned int HCSR04::get_pulse_us()
-{
-    return pulsedur;
+
+void HCSR04::setTemp(int temp){
+    temperature = temp;
+    sonic = 331.5+0.61*temperature;
+    maxTime = 10*1000000/sonic;
 }
 
-
-
 /*******************************************************
    Here is a sample code usage
 ********************************************************* 
@@ -82,6 +97,7 @@
 int main()
 {
     unsigned char count=0;
+    setTep(20);
     while(1) {
         usensor.start();
         wait_ms(500); 
diff -r 71da0dbf4400 -r e150242bce6b hcsr04.h
--- a/hcsr04.h	Sat Mar 30 18:55:03 2013 +0000
+++ b/hcsr04.h	Wed Sep 05 15:06:24 2018 +0000
@@ -53,16 +53,19 @@
     void isr_fall(void);
     void fall (void (*fptr)(void));
     void rise (void (*fptr)(void));
-
-
+    
+    void setTemp(int temp);
 
 private:
 
     Timer pulsetime;
+    float pulsetime_us;
+    unsigned int distance;
+    int temperature;
+    double maxTime;
+    double sonic;
     DigitalOut  trigger;
     InterruptIn echo;
-    unsigned int pulsedur;
-    unsigned int distance;
 };
 
 #endif
\ No newline at end of file