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.
Dependencies: mbed mbedConnectorInterface mbedEndpointNetwork TrashSensors
Fork of TempAndDistTest by
Revision 8:c69fe28366d8, committed 2015-05-06
- Comitter:
- coyotebush
- Date:
- Wed May 06 04:02:08 2015 +0000
- Parent:
- 7:939fdc8df95b
- Child:
- 9:16b63cd4aba8
- Commit message:
- Distance sensing improvements
Changed in this revision
--- a/hcsr04.cpp Wed May 06 02:53:36 2015 +0000
+++ b/hcsr04.cpp Wed May 06 04:02:08 2015 +0000
@@ -38,11 +38,11 @@
void HCSR04::isr_rise(void)
{
pulsetime.start();
+ pulsetime.reset();
}
void HCSR04::start(void)
{
pulsedur = 0;
- distance = 0;
trigger=1;
wait_us(10);
@@ -53,8 +53,6 @@
{
pulsetime.stop();
pulsedur = pulsetime.read_us();
- distance= (pulsedur*343)/20000;
- pulsetime.reset();
}
void HCSR04::rise (void (*fptr)(void))
@@ -66,9 +64,11 @@
echo.fall(fptr);
}
-unsigned int HCSR04::get_dist_cm()
+double HCSR04::get_dist_cm()
{
- return distance;
+ if (pulsedur == 0)
+ return -1;
+ return pulsedur * kMicrosecondsToCentimeters;
}
unsigned int HCSR04::get_pulse_us()
{
--- a/hcsr04.h Wed May 06 02:53:36 2015 +0000
+++ b/hcsr04.h Wed May 06 04:02:08 2015 +0000
@@ -29,9 +29,13 @@
/** HCSR04 Class(es)
*/
+// TODO(CJF): steal clever logic from https://code.google.com/p/arduino-new-ping/ ?
+
class HCSR04
{
public:
+ static const double kMicrosecondsToCentimeters = 343.0 / 20000;
+
/** Create a HCSR04 object connected to the specified pin
* @param pin i/o pin to connect to
*/
@@ -41,7 +45,7 @@
/** Return the distance from obstacle in cm
* @param distance in cms and returns -1, in case of failure
*/
- unsigned int get_dist_cm(void);
+ double get_dist_cm(void);
/** Return the pulse duration equal to sonic waves travelling to obstacle and back to receiver.
* @param pulse duration in microseconds.
*/
@@ -62,7 +66,6 @@
DigitalOut trigger;
InterruptIn echo;
unsigned int pulsedur;
- unsigned int distance;
};
#endif
\ No newline at end of file
--- a/main.cpp Wed May 06 02:53:36 2015 +0000
+++ b/main.cpp Wed May 06 04:02:08 2015 +0000
@@ -46,9 +46,6 @@
}
int main() {
- double temperature;
- unsigned int distance;
-
logger.log("\r\n\r\nSmart Trash Can booting\r\n");
Connector::Endpoint::plumbNetwork();
Connector::Endpoint::start();
--- a/resource/DistanceResource.cpp Wed May 06 02:53:36 2015 +0000
+++ b/resource/DistanceResource.cpp Wed May 06 04:02:08 2015 +0000
@@ -2,8 +2,11 @@
string DistanceResource::get() {
char buf[10];
+ double dist;
sensor->start();
- wait_ms(500);
- snprintf(buf, 10, "%d", sensor->get_dist_cm());
+ while ((dist = sensor->get_dist_cm()) < 0)
+ ;
+ //wait_ms(500);
+ snprintf(buf, 10, "%0.2f", dist);
return string(buf);
}
\ No newline at end of file
