Original Code Repo: https://os.mbed.com/users/priyank12p/code/Electronically-Connected-Intelligent-She/ Modified for Fall 2021 students.

Dependencies:   mbed mbed-http ESP8266

Files at this revision

API Documentation at this revision

Comitter:
prabhuvd
Date:
Sat Mar 30 17:27:39 2013 +0000
Child:
1:093521f56089
Commit message:
Version 1.0 , the rise and fall methods needs to be attached to pin;

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hcsr04.cpp	Sat Mar 30 17:27:39 2013 +0000
@@ -0,0 +1,67 @@
+/* Copyright (c) 2013 Prabhu Desai
+ * pdtechworld@gmail.com
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+
+#include "hcsr04.h"
+
+
+DistMeasure::DistMeasure(PinName TrigPin,PinName EchoPin,unsigned int maxtime):
+    trigger(TrigPin), echo(EchoPin), timeout(maxtime)
+{
+    pulsetime.stop();
+    pulsetime.reset();
+   // this.rise(&DistMeasure::isr_rise);
+   // this.fall(&DistMeasure::isr_fall);    
+}
+
+
+DistMeasure::~DistMeasure()
+{
+}
+
+ 
+void DistMeasure::isr_rise(void)
+{
+    pulsetime.start();
+}
+
+void DistMeasure::isr_fall(void)
+{
+    pulsetime.stop();
+    pulsedur = pulsetime.read_us();
+    distance= (pulsedur*343)/20000;
+    pulsetime.reset();
+}
+void DistMeasure::rise (void (*fptr)(void))
+{
+    echo.rise(fptr);
+}
+void DistMeasure::fall (void (*fptr)(void))
+{
+    echo.fall(fptr);
+}
+
+unsigned int DistMeasure::get_distance_cm()
+{
+    if(distance > 65530) {
+        return -1;
+    } else {
+        return distance;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hcsr04.h	Sat Mar 30 17:27:39 2013 +0000
@@ -0,0 +1,63 @@
+/* Copyright (c) 2013 Prabhu Desai
+ * pdtechworld@gmail.com
+ *
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * For more details on the sensor :
+ * http://www.elecfreaks.com/store/hcsr04-ultrasonic-sensor-distance-measuring-module-p-91.html?zenid=pgm8pgnvaodbe36dibq5s1soi3
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef MBED_HCSR04_H
+#define MBED_HCSR04_H
+
+#include "mbed.h"
+
+/** Distance Measure Class(es)
+ */
+
+class DistMeasure
+{
+public:
+    /** Create a DistMeasure object connected to the specified pin
+    * @param pin i/o pin to connect to
+    */
+    DistMeasure(PinName TrigPin,PinName EchoPin,unsigned int maxtime);
+    ~DistMeasure();
+
+    /** Return the distance from obstacle in cm
+    * @param distance in cms and returns -1, in case of failure
+    */
+ 
+    unsigned int get_distance_cm(void);
+    void isr_rise(void);
+    void isr_fall(void);
+    void fall (void (*fptr)(void));
+    void rise (void (*fptr)(void));
+    
+
+
+private:
+
+    Timer pulsetime;
+    DigitalOut  trigger;
+    InterruptIn echo;
+    unsigned int timeout;
+    unsigned int pulsedur;
+    unsigned int distance;    
+};
+
+#endif
\ No newline at end of file