Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
HCSR04.cpp
- Committer:
- ldelinic
- Date:
- 2015-12-09
- Revision:
- 0:0910b24c9da3
File content as of revision 0:0910b24c9da3:
#include "mbed.h"
#include "HCSR04.h"
HCSR04::HCSR04(PinName echoPin, PinName triggerPin): echo(echoPin), trigger(triggerPin){
    
    init();
  }
    
void HCSR04::init(){
    
    echo.rise(this, &HCSR04::startTimer);            //na rastucem bridu zapocni timer
    
    echo.fall(this, &HCSR04::stopTimer);             //na padajucem bridu zaustavi timer
    
    distance = -1;                                   //pocetna udaljenost
}
void HCSR04::startTimer(){
    
    timer.start();      //zapocni brojanje
    
}
    
void HCSR04::stopTimer(){
    
    timer.stop();
}
    
void HCSR04::startMeasurement(){
    
    trigger=1;
    wait_us(10);
    trigger=0;
    wait_ms(25);
    distance=timer.read()*1e6/58;
    timer.reset();
}
float HCSR04::getDistance_cm(){
    
    startMeasurement();
    return distance;    
    }