1 sensor using RTOS Timer, another 2 over RTOS Semaphore
Dependents: Autonomous_quadcopter
Fork of HCSR04 by
hcsr04.cpp
- Committer:
- edy05
- Date:
- 2017-10-26
- Revision:
- 1:53657de3246f
- Parent:
- 0:86b2086be101
- Child:
- 2:8801a3da8693
File content as of revision 1:53657de3246f:
#include "hcsr04.h" #include "mbed.h" #include "rtos.h" /* *HCSR04.cpp */ HCSR04::HCSR04(PinName t, PinName e) : trig(t), echo(e) { thread = new Thread(threadWorker, this); } void HCSR04::threadWorker(void const *p) { HCSR04* self = (HCSR04*)p; while(1){ self->trig=0; // trigger low wait_us(2); // wait self->trig=1; // trigger high wait_us(10); self->trig=0; // trigger low while(!self->echo); // start pulseIN self->timer.reset(); //reset timer self->timer.start(); while(self->echo); self->timer.stop(); self->distan = self->timer.read_us() / 58; Thread::wait(50); } } uint16_t HCSR04::getDistan(){ return distan; } //return distance in cm uint16_t HCSR04::getDistance(){ distance_cm = distan/58; return distance_cm; }