lab 3

Dependencies:   mbed mbed-rtos tsi_sensor SLCD

Committer:
wstapleton
Date:
Tue Feb 18 21:16:23 2020 +0000
Revision:
0:78b84e1ce9df
Lab2 hint 2/18/2020

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wstapleton 0:78b84e1ce9df 1 #ifndef HCSR04_H
wstapleton 0:78b84e1ce9df 2 #define HCSR04_H
wstapleton 0:78b84e1ce9df 3
wstapleton 0:78b84e1ce9df 4 // The following lines MUST be added into the main() routine
wstapleton 0:78b84e1ce9df 5 /*
wstapleton 0:78b84e1ce9df 6 HCSR04_timer.start();
wstapleton 0:78b84e1ce9df 7 HCSR04_echo.rise(&echo_start);
wstapleton 0:78b84e1ce9df 8 HCSR04_echo.fall(&echo_end);
wstapleton 0:78b84e1ce9df 9 HCSR04_start.attach(&pulse_trigger, 100E-3);
wstapleton 0:78b84e1ce9df 10 */
wstapleton 0:78b84e1ce9df 11
wstapleton 0:78b84e1ce9df 12 DigitalOut HCSR04_trigger(PTC13);
wstapleton 0:78b84e1ce9df 13 InterruptIn HCSR04_echo(PTC16);
wstapleton 0:78b84e1ce9df 14 Ticker HCSR04_start;
wstapleton 0:78b84e1ce9df 15 Timer HCSR04_timer;
wstapleton 0:78b84e1ce9df 16 float HCSR04_time, HCSR04_inches, HCSR04_cm;
wstapleton 0:78b84e1ce9df 17
wstapleton 0:78b84e1ce9df 18 void HCSR04_pulse_trigger()
wstapleton 0:78b84e1ce9df 19 {
wstapleton 0:78b84e1ce9df 20 HCSR04_trigger.write(1);
wstapleton 0:78b84e1ce9df 21 wait(10E-6);
wstapleton 0:78b84e1ce9df 22 HCSR04_trigger.write(0);
wstapleton 0:78b84e1ce9df 23 }
wstapleton 0:78b84e1ce9df 24
wstapleton 0:78b84e1ce9df 25 void HCSR04_echo_start()
wstapleton 0:78b84e1ce9df 26 {
wstapleton 0:78b84e1ce9df 27 HCSR04_timer.reset();
wstapleton 0:78b84e1ce9df 28 }
wstapleton 0:78b84e1ce9df 29
wstapleton 0:78b84e1ce9df 30 void HCSR04_echo_end()
wstapleton 0:78b84e1ce9df 31 {
wstapleton 0:78b84e1ce9df 32 HCSR04_time=HCSR04_timer.read();
wstapleton 0:78b84e1ce9df 33 if(HCSR04_time < 30E-3) // delay longer than 30 ms indicates no echo
wstapleton 0:78b84e1ce9df 34 {
wstapleton 0:78b84e1ce9df 35 HCSR04_inches=HCSR04_time*13503.9*0.5; // speed of sound = 13503.9 inches/second and distance to object is 1/2 path length;
wstapleton 0:78b84e1ce9df 36 HCSR04_cm=HCSR04_time*34300.0*0.5; // speed of sound = 34300 cm/second and distance to object is 1/2 path length;
wstapleton 0:78b84e1ce9df 37 }
wstapleton 0:78b84e1ce9df 38 else
wstapleton 0:78b84e1ce9df 39 {
wstapleton 0:78b84e1ce9df 40 HCSR04_inches=-1.0;
wstapleton 0:78b84e1ce9df 41 HCSR04_cm=-1.0;
wstapleton 0:78b84e1ce9df 42 }
wstapleton 0:78b84e1ce9df 43 }
wstapleton 0:78b84e1ce9df 44
wstapleton 0:78b84e1ce9df 45
wstapleton 0:78b84e1ce9df 46 #endif //HCSR04_H