microseconds delay to use HC_SR04 distance sensor November 24, 2016 10:17PM |
Registered: 8 years ago Posts: 12 |
double HC_SR04::getDistanceCM(){ sendTriggerPulse(_trigPin); waitForEcho(_echoPin, HIGH, 100); long startTime = micros(); waitForEcho(_echoPin, LOW, 100); long endTime = micros(); long duration = endTime - startTime; double distance = duration / 29.0 / 2.0; if (distance < _minCM || distance > _maxCM){ return -1; } return distance; } void HC_SR04::sendTriggerPulse(int pin){ digitalWrite(pin, HIGH); delayMicroseconds(10); digitalWrite(pin, LOW); } void HC_SR04::waitForEcho(int pin, int value, long timeout){ long giveupTime = millis() + timeout; while (digitalRead(pin) != value && millis() < giveupTime) {} }
// // #include "lib_tmr.bas" // #include "lib_intr.bas" // #include "lib_io.bas" //send 10us trigger pulse //wait for echo pin to go high //time for echo pin to go low (ms/= distance //10cm to 250cm // //the trigger is connected to pin 15 (RB6) constant trigPin {PORTB(PORT),8} //the echo is connected to pin 16 (RB7) constant echoPin {PORTB(PORT),7} constant ANSELA 0xBF886000 constant ANSELACLR 0xBF886004 constant ANSELASET 0xBF886008 constant ANSELAINV 0xBF88600C constant TRISA 0xBF886010 constant TRISACLR 0xBF886014 constant TRISASET 0xBF886018 constant TRISAINV 0xBF88601C constant PORTA 0xBF886020 constant LATA 0xBF886030 constant PORTACLR 0xBF886024 constant PORTASET 0xBF886028 constant PORTAINV 0xBF88602C constant CNPUASET 0xBF886058 function init() io_pinRole(*trigPin(),OUT,WOFF) //set output io_pinRole(*echoPin(),IN,WPD) //set input with weak pull down endf function waitforecho(*echotime,value,timeout) print "Waiting for echo:" print value print "rn" while (io_pinGet(*echoPin()) <> value) && (echotime < timeout) echotime = echotime + 1 wait(1) wend print "Echo received:" print echotime print " msrn" endf function settrigger() print "Sending Triggerrn" tmr_init(*TIMER4(),2,100) //10us pulse print "Timer initialisedrn" io_pinSet(*trigPin(),1) print "Trig pin highrn" while tmr_get(*TIMER4()) < 100 //print "*" wend //print "rn" io_pinSet(*trigPin(),0) //put the pin low tmr_clr(*TIMER4()) //reset the timer print "Trig pin lowrn" endf function getdistance() dim echotime dim distance# echotime = 0 io_pinSet(*trigPin(),0) //put the pin low wait(10) settrigger() waitforecho(*echotime,1,100) waitforecho(*echotime,0,100) distance# = echotime / 29.0 / 2.0 print "rn" print "Distance is: " print distance# print "cmrn" endf function settriglow() io_pinSet(*trigPin(),0) //put the pin low endf function settrighigh() io_pinSet(*trigPin(),1) //put the pin low endf function readecho() print(io_pinGet(*echoPin())) endf function main2() tmr_init(*TIMER23(),7,312500) //1 second repeating ir_set(*TIMER23(),3,"getdistance") //priority3 endf
Re: microseconds delay to use HC_SR04 distance sensor November 25, 2016 09:12AM |
Admin Registered: 11 years ago Posts: 351 |
Re: microseconds delay to use HC_SR04 distance sensor November 27, 2016 10:22PM |
Registered: 8 years ago Posts: 12 |
Re: microseconds delay to use HC_SR04 distance sensor November 27, 2016 10:24PM |
Registered: 8 years ago Posts: 12 |