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.
SR04.h
00001 #ifndef SR04_H 00002 #define SR04_H 00003 DigitalOut pinTrigg(PC_10); 00004 DigitalIn pinEcho(PC_12); 00005 const char* distMsg = "Actual distance: %g \n\r"; 00006 float actualDistance = 0; 00007 float pulseIn(DigitalIn pin) 00008 { 00009 Timer durationTimer; 00010 float tempDuration = 0; 00011 while(!pin); //wait for high 00012 durationTimer.start(); 00013 while(pin); //wait for low 00014 tempDuration = durationTimer.read_us(); 00015 return tempDuration; 00016 } 00017 float getDistance(int sensorID) 00018 { 00019 if(sensorID == 0) 00020 { 00021 float duration = 0; 00022 pinTrigg = 0; 00023 wait_us(2); 00024 pinTrigg = 1; 00025 wait_us(10); 00026 pinTrigg = 0; 00027 duration = pulseIn(pinEcho); 00028 duration /= 58; //datasheet! 00029 return duration; 00030 } 00031 return 0; 00032 } 00033 #endif //SR04_H
Generated on Sat Jul 23 2022 13:11:59 by
 1.7.2
 1.7.2