Garn E / Mbed 2 deprecated HC-SR04

Dependencies:   HCSR04 mbed

Fork of HC-SR04 by TVZ Mechatronics Team

Files at this revision

API Documentation at this revision

Comitter:
tbjazic
Date:
Sat Dec 05 09:07:15 2015 +0000
Parent:
1:22043b67c31c
Child:
3:3297ea6e3ae1
Commit message:
Library HCSR04 created.

Changed in this revision

HCSR04.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HCSR04.lib	Sat Dec 05 09:07:15 2015 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/TVZ-Mechatronics-Team/code/HCSR04/#8871082486ac
--- a/main.cpp	Fri Dec 04 17:38:02 2015 +0000
+++ b/main.cpp	Sat Dec 05 09:07:15 2015 +0000
@@ -1,88 +1,10 @@
-/** Revision 1: Building the class  */
+/** Revision 2: Separating the class declaration, implementation and main() function into files.  */
 
 #include "mbed.h"
+#include "HCSR04.h"
 
 Serial pc(USBTX, USBRX);    // communication with terminal
 
-class HCSR04 {
-    
-    public:
-    
-    /** Receives two PinName variables.
-     * @param echoPin mbed pin to which the echo signal is connected to
-     * @param triggerPin mbed pin to which the trigger signal is connected to
-     */
-    HCSR04(PinName echoPin, PinName triggerPin);
-    
-    /** Calculates the distance in cm, with the calculation time of 25 ms.
-     * @returns distance of the measuring object in cm.
-     */
-    float getDistance_cm();
-    
-    private:
-    
-    InterruptIn echo;       // echo pin
-    DigitalOut trigger;     // trigger pin
-    Timer timer;            // echo pulsewidth measurement
-    float distance;         // store the distance in cm
-    
-    /** Start the timer. */
-    void startTimer();
-    
-    /** Stop the timer. */
-    void stopTimer();
-    
-    /** Initialization. */
-    void init();
-    
-    /** Start the measurement. */
-    void startMeasurement();
-};
-
-
-HCSR04::HCSR04(PinName echoPin, PinName triggerPin) : echo(echoPin), trigger(triggerPin) {
-    init();
-}
-
-void HCSR04::init() {
-    /** configure the rising edge to start the timer */
-    echo.rise(this, &HCSR04::startTimer);
-    
-    /** configure the falling edge to stop the timer */
-    echo.fall(this, &HCSR04::stopTimer);
-    
-    distance = -1; // initial distance
-}
-
-void HCSR04::startTimer() {
-    timer.start(); // start the timer
-}
-
-void HCSR04::stopTimer() {
-    timer.stop(); // stop the timer
-}
-
-void HCSR04::startMeasurement() {
-    /** Start the measurement by sending the 10us trigger pulse. */
-    trigger = 1;
-    wait_us(10);
-    trigger = 0;
-    
-    /** Wait for the sensor to finish measurement (generate rise and fall interrupts).
-     *  Minimum wait time is determined by maximum measurement distance of 400 cm.
-     *  t_min = 400 * 58 = 23200 us = 23.2 ms */
-    wait_ms(25); 
-    
-    /** calculate the distance in cm */
-    distance = timer.read() * 1e6 / 58;
-    timer.reset(); // reset the timer to 0 after storing the distance
-}
-
-float HCSR04::getDistance_cm() {
-    startMeasurement();
-    return distance;
-}
-
 int main() {
     HCSR04 sensor(p5, p7);     // instantiate the sensor object
     while(1) {