Smartage application

Dependencies:   BufferedSerial SX1276GenericLib USBDeviceHT mbed Crypto X_NUCLEO_IKS01A2

Fork of STM32L0_LoRa by Helmut Tschemernjak

Committer:
marcozecchini
Date:
Mon Sep 17 22:16:48 2018 +0000
Revision:
34:8393ded26b4f
Parent:
31:6bf5a868695f
Bug fixed 4

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 29:04e1489f8fe2 1 #include "hcsr04.h"
marcozecchini 29:04e1489f8fe2 2 #include "mbed.h"
marcozecchini 29:04e1489f8fe2 3 /*
marcozecchini 29:04e1489f8fe2 4 *HCSR04.cpp
marcozecchini 29:04e1489f8fe2 5 */
marcozecchini 29:04e1489f8fe2 6 HCSR04::HCSR04(PinName t, PinName e) : trig(t), echo(e) {}
marcozecchini 29:04e1489f8fe2 7 long HCSR04::echo_duration() {
marcozecchini 29:04e1489f8fe2 8
marcozecchini 29:04e1489f8fe2 9 timer.reset(); //reset timer
marcozecchini 29:04e1489f8fe2 10 trig=0; // trigger low
marcozecchini 29:04e1489f8fe2 11 wait_us(2); // wait
marcozecchini 29:04e1489f8fe2 12 trig=1; // trigger high
marcozecchini 29:04e1489f8fe2 13 wait_us(10);
marcozecchini 29:04e1489f8fe2 14 trig=0; // trigger low
marcozecchini 29:04e1489f8fe2 15 while(!echo); // start pulseIN
marcozecchini 29:04e1489f8fe2 16 timer.start();
marcozecchini 29:04e1489f8fe2 17 while(echo);
marcozecchini 29:04e1489f8fe2 18 timer.stop();
marcozecchini 29:04e1489f8fe2 19 return timer.read_us();
marcozecchini 29:04e1489f8fe2 20
marcozecchini 29:04e1489f8fe2 21 }
marcozecchini 31:6bf5a868695f 22
marcozecchini 31:6bf5a868695f 23 //return distance in cm
marcozecchini 31:6bf5a868695f 24 long HCSR04::distance(){
marcozecchini 31:6bf5a868695f 25 duration = echo_duration();
marcozecchini 31:6bf5a868695f 26 distance_cm = (duration/2)/29.1 ;
marcozecchini 31:6bf5a868695f 27 return distance_cm;
marcozecchini 31:6bf5a868695f 28
marcozecchini 31:6bf5a868695f 29 }
marcozecchini 29:04e1489f8fe2 30