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.
JSN_SR04.cpp
00001 #include "mbed.h" 00002 #include "JSN_SR04.h" 00003 00004 JSN_SR04::JSN_SR04(PinName echoPin, PinName triggerPin) : echo(echoPin), trigger(triggerPin) { 00005 init(); 00006 } 00007 00008 void JSN_SR04::init() { 00009 distance = -1; // Default distance 00010 minDistance = 2; // Default min boundary 00011 maxDistance = 400; // Default max boundary 00012 newDataReady = timerStarted = false; // Indicators are false by default 00013 } 00014 00015 void JSN_SR04::startTimer() { 00016 // Check if the timer started 00017 if (!timerStarted) { 00018 // start the timer 00019 timer.start(); 00020 timerStarted = true; 00021 00022 // Call the stopTimer() method after a predefined timeout (> 25 ms signal duration indicates that there is no obstacle) 00023 echoTimeout.attach_us(this, &JSN_SR04::stopTimer, SENSOR_TIMEOUT); 00024 00025 // Stop measurement and 00026 echo.fall(this, &JSN_SR04::stopTimer); 00027 echo.rise(NULL); 00028 } 00029 } 00030 00031 void JSN_SR04::calculateDistance() { 00032 int timer_state; // Current timer state (ticks amount) 00033 00034 // Check if timer was even started 00035 if (timerStarted) { 00036 timer_state = timer.read(); 00037 00038 // If timeout occured (no obstacle) 00039 if (timer_state >= SENSOR_TIMEOUT) distance = -1; 00040 00041 // If there is no timeout (an obstacle detected) 00042 else { 00043 // Calculate distance (duration of the signal * constant used in datasheet) 00044 distance = timer.read() * 1e6 / 58; 00045 00046 // Set distance to the minimum if it's lower 00047 if (distance < minDistance) 00048 distance = minDistance; 00049 00050 // Set distance to the maximum if it's higher 00051 if (distance > maxDistance) 00052 distance = maxDistance; 00053 } 00054 00055 // Indicate that new data is ready 00056 newDataReady = true; 00057 } 00058 } 00059 00060 void JSN_SR04::stopTimer() { 00061 // Stop the timer 00062 timer.stop(); 00063 00064 // Call the calculation function 00065 calculateDistance(); 00066 00067 // Reset the timer 00068 timer.reset(); 00069 timerStarted = false; 00070 00071 // Switch off the timeout timer event 00072 echoTimeout.detach(); 00073 echo.fall(NULL); 00074 } 00075 00076 void JSN_SR04::turnOffTrigger() { 00077 // Switch off the trigger 00078 trigger = 0; 00079 } 00080 00081 void JSN_SR04::startMeasurement() { 00082 // Trigger the sensor (by a signal described in the datasheet) 00083 trigger = 1; 00084 triggerTimeout.attach_us(this, &JSN_SR04::turnOffTrigger, 100); 00085 00086 // Start to measure a response 00087 echo.rise(this, &JSN_SR04::startTimer); 00088 00089 // Outdate current response 00090 newDataReady = false; 00091 } 00092 00093 float JSN_SR04::getDistance_cm() { 00094 // Indicate that the data was read 00095 newDataReady = false; 00096 00097 // Return the data 00098 return distance; 00099 } 00100 00101 float JSN_SR04::getDistance_mm() { 00102 // Indicate that the data was read 00103 newDataReady = false; 00104 00105 // Return the data 00106 if (distance != -1) 00107 return distance * 10; 00108 00109 else return distance; 00110 } 00111 00112 bool JSN_SR04::isNewDataReady() { 00113 // Return the indicator 00114 return newDataReady; 00115 } 00116 00117 void JSN_SR04::setRanges(float minRange, float maxRange) { 00118 // Set new boundaries that are in range of a sensors reading ability (datasheet) 00119 if (minRange < maxRange) { 00120 if (minRange >= 2 && minRange < 400) // bug from revs. 4 and 5 corrected 00121 minDistance = minRange; 00122 if (maxRange <= 400) 00123 maxDistance = maxRange; 00124 } 00125 } 00126 00127 float JSN_SR04::getMinRange() { 00128 // Return a lower boundary 00129 return minDistance; 00130 } 00131 00132 float JSN_SR04::getMaxRange() { 00133 // Return an upper boundary 00134 return maxDistance; 00135 }
Generated on Thu Jul 14 2022 20:23:26 by
1.7.2