HCSR04 - Mehmet Akif Arvas

Committer:
marvas
Date:
Tue Jul 06 10:17:17 2021 +0000
Revision:
1:f746a7bb4e54
Parent:
0:86b2086be101
HCSR04 - Mehmet Akif Arvas

Who changed what in which revision?

UserRevisionLine numberNew contents of line
antoniolinux 0:86b2086be101 1 #include "hcsr04.h"
antoniolinux 0:86b2086be101 2 #include "mbed.h"
antoniolinux 0:86b2086be101 3 /*
antoniolinux 0:86b2086be101 4 *HCSR04.cpp
antoniolinux 0:86b2086be101 5 */
antoniolinux 0:86b2086be101 6 HCSR04::HCSR04(PinName t, PinName e) : trig(t), echo(e) {}
marvas 1:f746a7bb4e54 7 int HCSR04::echo_sure() {
antoniolinux 0:86b2086be101 8
antoniolinux 0:86b2086be101 9 timer.reset(); //reset timer
antoniolinux 0:86b2086be101 10 trig=0; // trigger low
antoniolinux 0:86b2086be101 11 wait_us(2); // wait
antoniolinux 0:86b2086be101 12 trig=1; // trigger high
antoniolinux 0:86b2086be101 13 wait_us(10);
antoniolinux 0:86b2086be101 14 trig=0; // trigger low
antoniolinux 0:86b2086be101 15 while(!echo); // start pulseIN
antoniolinux 0:86b2086be101 16 timer.start();
antoniolinux 0:86b2086be101 17 while(echo);
antoniolinux 0:86b2086be101 18 timer.stop();
antoniolinux 0:86b2086be101 19 return timer.read_us();
antoniolinux 0:86b2086be101 20
antoniolinux 0:86b2086be101 21 }
antoniolinux 0:86b2086be101 22
antoniolinux 0:86b2086be101 23 //return distance in cm
marvas 1:f746a7bb4e54 24 int HCSR04::uzaklik(){
marvas 1:f746a7bb4e54 25 sure = echo_sure();
marvas 1:f746a7bb4e54 26 mesafe_cm = (sure/2)/29.1 ;
marvas 1:f746a7bb4e54 27 return mesafe_cm;
antoniolinux 0:86b2086be101 28
antoniolinux 0:86b2086be101 29 }