Chris Powers / HCSR04
Committer:
Powers
Date:
Wed Jun 03 17:04:17 2020 +0000
Revision:
1:6f9e41e319ad
Parent:
0:a3bc6812cccb
Child:
2:daacb9d21d73
Version 0.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Powers 0:a3bc6812cccb 1 //-------------------------------------
Powers 0:a3bc6812cccb 2 // Short Example how to use this class:
Powers 0:a3bc6812cccb 3 //-------------------------------------
Powers 0:a3bc6812cccb 4 /*
Powers 0:a3bc6812cccb 5 #include "mbed.h"
Powers 0:a3bc6812cccb 6 #include "HCSR04.h"
Powers 0:a3bc6812cccb 7
Powers 0:a3bc6812cccb 8 Serial pc(USBTX, USBRX, 115200);
Powers 0:a3bc6812cccb 9 HCSR04 sensor(D9, D8);
Powers 0:a3bc6812cccb 10
Powers 0:a3bc6812cccb 11 int main(void)
Powers 0:a3bc6812cccb 12 {
Powers 0:a3bc6812cccb 13 pc.printf("\nHCSR04 - Interrupt\n");
Powers 0:a3bc6812cccb 14 sensor.startSensor(2);
Powers 0:a3bc6812cccb 15 while(1)
Powers 0:a3bc6812cccb 16 {
Powers 0:a3bc6812cccb 17 if(sensor.checkUpdate())
Powers 0:a3bc6812cccb 18 {
Powers 0:a3bc6812cccb 19 pc.printf("Distance: %.2f \n", sensor.getDistance());
Powers 0:a3bc6812cccb 20 }
Powers 0:a3bc6812cccb 21 }
Powers 0:a3bc6812cccb 22 return 0;
Powers 0:a3bc6812cccb 23 }
Powers 0:a3bc6812cccb 24 */
Powers 0:a3bc6812cccb 25 #ifndef HCSR04_H
Powers 0:a3bc6812cccb 26 #define HCSR04_H
Powers 0:a3bc6812cccb 27
Powers 0:a3bc6812cccb 28 #include "mbed.h"
Powers 0:a3bc6812cccb 29
Powers 0:a3bc6812cccb 30
Powers 0:a3bc6812cccb 31 class HCSR04
Powers 0:a3bc6812cccb 32 {
Powers 0:a3bc6812cccb 33 private:
Powers 0:a3bc6812cccb 34 // Trigger PIN - to write LOW and HIGH
Powers 0:a3bc6812cccb 35 DigitalOut trigger;
Powers 0:a3bc6812cccb 36 // ECHO PIN - to start Function after rising/falling flank
Powers 0:a3bc6812cccb 37 InterruptIn echo;
Powers 0:a3bc6812cccb 38 // Ticker object to periodicly start the measurement
Powers 0:a3bc6812cccb 39 Ticker execute;
Powers 0:a3bc6812cccb 40 // Timeout to write LOW to Trigger pin after 10 microseconds
Powers 0:a3bc6812cccb 41 Timeout go_low;
Powers 0:a3bc6812cccb 42 // Timer to measure the time between the sonic burst goes out and in again
Powers 0:a3bc6812cccb 43 Timer get_time;
Powers 0:a3bc6812cccb 44 // Variables for the update status and the the distance in microseconds
Powers 0:a3bc6812cccb 45 volatile float distance_us;
Powers 0:a3bc6812cccb 46 volatile bool update;
Powers 0:a3bc6812cccb 47
Powers 0:a3bc6812cccb 48 // Called by TICKER object to periodicly write HIGH to TRIGGER PIN
Powers 0:a3bc6812cccb 49 void sendHigh(void);
Powers 0:a3bc6812cccb 50 // Called by TIMEOUT Object to set TRIPPER PIN LOW again
Powers 0:a3bc6812cccb 51 void sendLow(void);
Powers 0:a3bc6812cccb 52 // Called by interrupt - rising flank on ECHO PIN
Powers 0:a3bc6812cccb 53 void startTimer(void);
Powers 0:a3bc6812cccb 54 // Called by interrupt - falling flank on ECHO PIN
Powers 0:a3bc6812cccb 55 void stopTimer(void);
Powers 0:a3bc6812cccb 56
Powers 0:a3bc6812cccb 57 public:
Powers 0:a3bc6812cccb 58 // Constructor
Powers 0:a3bc6812cccb 59 HCSR04(PinName _trigger, PinName _echo):trigger(_trigger), echo(_echo)
Powers 0:a3bc6812cccb 60 {
Powers 0:a3bc6812cccb 61 // Attach member functions to InterruptIn for changing state on ECHO PIN [HIGH,LOW]
Powers 0:a3bc6812cccb 62 echo.rise(callback(this, &HCSR04::startTimer));
Powers 0:a3bc6812cccb 63 echo.fall(callback(this, &HCSR04::stopTimer));
Powers 0:a3bc6812cccb 64
Powers 0:a3bc6812cccb 65 // Default Values
Powers 0:a3bc6812cccb 66 trigger.write(0);
Powers 0:a3bc6812cccb 67 update = false;
Powers 0:a3bc6812cccb 68
Powers 0:a3bc6812cccb 69 }
Powers 0:a3bc6812cccb 70
Powers 1:6f9e41e319ad 71 // Function to return the distance in cm, should be called if "checkUpdate()" returns true
Powers 0:a3bc6812cccb 72 float getDistance(void);
Powers 0:a3bc6812cccb 73 // Check for an updatet status for the measurement
Powers 0:a3bc6812cccb 74 bool checkUpdate(void);
Powers 0:a3bc6812cccb 75 // Function to start sending HIGH in periodic intervalls to TRIGGER PIN via a TICKER Object
Powers 0:a3bc6812cccb 76 void startSensor(int freq);
Powers 0:a3bc6812cccb 77 // Function to stop sending HIGH to TRIGGER PIN
Powers 0:a3bc6812cccb 78 void stopSensor(void);
Powers 0:a3bc6812cccb 79
Powers 0:a3bc6812cccb 80
Powers 0:a3bc6812cccb 81 };
Powers 0:a3bc6812cccb 82
Powers 0:a3bc6812cccb 83 #endif