Coursework

Committer:
sesa514652
Date:
Fri Feb 04 16:44:14 2022 +0000
Revision:
45:f1db729741f7
Parent:
0:1f799c7cce2b
Submission

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sesa514652 0:1f799c7cce2b 1 /* File: HCSR04.h
sesa514652 0:1f799c7cce2b 2 * Author: Antonio Buonanno
sesa514652 0:1f799c7cce2b 3 *Board: STM NUCLEO F401RE,
sesa514652 0:1f799c7cce2b 4 *Hardware: Ultrasonic Range HC-SR04,
sesa514652 0:1f799c7cce2b 5 *
sesa514652 0:1f799c7cce2b 6 *This work derived from Arduino library,
sesa514652 0:1f799c7cce2b 7 *
sesa514652 0:1f799c7cce2b 8 * Desc: driver for HCSR04 Ultrasonic Range Finder. The returned range
sesa514652 0:1f799c7cce2b 9 * is in units of meters.
sesa514652 0:1f799c7cce2b 10 *
sesa514652 0:1f799c7cce2b 11 *
sesa514652 0:1f799c7cce2b 12 *
sesa514652 0:1f799c7cce2b 13 */
sesa514652 0:1f799c7cce2b 14
sesa514652 0:1f799c7cce2b 15 /* EXAMPLE
sesa514652 0:1f799c7cce2b 16 #include "mbed.h"
sesa514652 0:1f799c7cce2b 17 #include "hcsr04.h"
sesa514652 0:1f799c7cce2b 18
sesa514652 0:1f799c7cce2b 19 //D12 TRIGGER D11 ECHO
sesa514652 0:1f799c7cce2b 20 HCSR04 sensor(D12, D11);
sesa514652 0:1f799c7cce2b 21 int main() {
sesa514652 0:1f799c7cce2b 22 while(1) {
sesa514652 0:1f799c7cce2b 23
sesa514652 0:1f799c7cce2b 24 long distance = sensor.distance();
sesa514652 0:1f799c7cce2b 25 printf("distanza %d \n",distance);
sesa514652 0:1f799c7cce2b 26 wait(1.0); // 1 sec
sesa514652 0:1f799c7cce2b 27
sesa514652 0:1f799c7cce2b 28 }
sesa514652 0:1f799c7cce2b 29 }
sesa514652 0:1f799c7cce2b 30 */
sesa514652 0:1f799c7cce2b 31 #ifndef hcsr04_H
sesa514652 0:1f799c7cce2b 32 #define hcsr04_H
sesa514652 0:1f799c7cce2b 33 #include "mbed.h"
sesa514652 0:1f799c7cce2b 34
sesa514652 0:1f799c7cce2b 35
sesa514652 0:1f799c7cce2b 36
sesa514652 0:1f799c7cce2b 37 class HCSR04 {
sesa514652 0:1f799c7cce2b 38 public:
sesa514652 0:1f799c7cce2b 39 HCSR04(PinName t, PinName e);
sesa514652 0:1f799c7cce2b 40 long echo_duration();
sesa514652 0:1f799c7cce2b 41 long distance();
sesa514652 0:1f799c7cce2b 42
sesa514652 0:1f799c7cce2b 43 private:
sesa514652 0:1f799c7cce2b 44 DigitalOut trig;
sesa514652 0:1f799c7cce2b 45 DigitalIn echo;
sesa514652 0:1f799c7cce2b 46 Timer timer;
sesa514652 0:1f799c7cce2b 47 long duration,distance_cm;
sesa514652 0:1f799c7cce2b 48 };
sesa514652 0:1f799c7cce2b 49
sesa514652 0:1f799c7cce2b 50 #endif