Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
HCSR04.h@2:daacb9d21d73, 2020-06-03 (annotated)
- Committer:
- Powers
- Date:
- Wed Jun 03 17:05:41 2020 +0000
- Revision:
- 2:daacb9d21d73
- Parent:
- 1:6f9e41e319ad
Version 0.3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Powers | 0:a3bc6812cccb | 1 | //------------------------------------- |
Powers | 0:a3bc6812cccb | 2 | // Short Example how to use this class: |
Powers | 2:daacb9d21d73 | 3 | // [returns Distance in cm(float)] |
Powers | 0:a3bc6812cccb | 4 | //------------------------------------- |
Powers | 0:a3bc6812cccb | 5 | /* |
Powers | 0:a3bc6812cccb | 6 | #include "mbed.h" |
Powers | 0:a3bc6812cccb | 7 | #include "HCSR04.h" |
Powers | 0:a3bc6812cccb | 8 | |
Powers | 0:a3bc6812cccb | 9 | Serial pc(USBTX, USBRX, 115200); |
Powers | 0:a3bc6812cccb | 10 | HCSR04 sensor(D9, D8); |
Powers | 0:a3bc6812cccb | 11 | |
Powers | 0:a3bc6812cccb | 12 | int main(void) |
Powers | 0:a3bc6812cccb | 13 | { |
Powers | 0:a3bc6812cccb | 14 | pc.printf("\nHCSR04 - Interrupt\n"); |
Powers | 0:a3bc6812cccb | 15 | sensor.startSensor(2); |
Powers | 0:a3bc6812cccb | 16 | while(1) |
Powers | 0:a3bc6812cccb | 17 | { |
Powers | 0:a3bc6812cccb | 18 | if(sensor.checkUpdate()) |
Powers | 0:a3bc6812cccb | 19 | { |
Powers | 2:daacb9d21d73 | 20 | pc.printf("Distance: %.2fcm \n", sensor.getDistance()); |
Powers | 0:a3bc6812cccb | 21 | } |
Powers | 0:a3bc6812cccb | 22 | } |
Powers | 0:a3bc6812cccb | 23 | return 0; |
Powers | 0:a3bc6812cccb | 24 | } |
Powers | 0:a3bc6812cccb | 25 | */ |
Powers | 0:a3bc6812cccb | 26 | #ifndef HCSR04_H |
Powers | 0:a3bc6812cccb | 27 | #define HCSR04_H |
Powers | 0:a3bc6812cccb | 28 | |
Powers | 0:a3bc6812cccb | 29 | #include "mbed.h" |
Powers | 0:a3bc6812cccb | 30 | |
Powers | 0:a3bc6812cccb | 31 | |
Powers | 0:a3bc6812cccb | 32 | class HCSR04 |
Powers | 0:a3bc6812cccb | 33 | { |
Powers | 0:a3bc6812cccb | 34 | private: |
Powers | 0:a3bc6812cccb | 35 | // Trigger PIN - to write LOW and HIGH |
Powers | 0:a3bc6812cccb | 36 | DigitalOut trigger; |
Powers | 0:a3bc6812cccb | 37 | // ECHO PIN - to start Function after rising/falling flank |
Powers | 0:a3bc6812cccb | 38 | InterruptIn echo; |
Powers | 0:a3bc6812cccb | 39 | // Ticker object to periodicly start the measurement |
Powers | 0:a3bc6812cccb | 40 | Ticker execute; |
Powers | 0:a3bc6812cccb | 41 | // Timeout to write LOW to Trigger pin after 10 microseconds |
Powers | 0:a3bc6812cccb | 42 | Timeout go_low; |
Powers | 0:a3bc6812cccb | 43 | // Timer to measure the time between the sonic burst goes out and in again |
Powers | 0:a3bc6812cccb | 44 | Timer get_time; |
Powers | 0:a3bc6812cccb | 45 | // Variables for the update status and the the distance in microseconds |
Powers | 0:a3bc6812cccb | 46 | volatile float distance_us; |
Powers | 0:a3bc6812cccb | 47 | volatile bool update; |
Powers | 0:a3bc6812cccb | 48 | |
Powers | 0:a3bc6812cccb | 49 | // Called by TICKER object to periodicly write HIGH to TRIGGER PIN |
Powers | 0:a3bc6812cccb | 50 | void sendHigh(void); |
Powers | 0:a3bc6812cccb | 51 | // Called by TIMEOUT Object to set TRIPPER PIN LOW again |
Powers | 0:a3bc6812cccb | 52 | void sendLow(void); |
Powers | 0:a3bc6812cccb | 53 | // Called by interrupt - rising flank on ECHO PIN |
Powers | 0:a3bc6812cccb | 54 | void startTimer(void); |
Powers | 0:a3bc6812cccb | 55 | // Called by interrupt - falling flank on ECHO PIN |
Powers | 0:a3bc6812cccb | 56 | void stopTimer(void); |
Powers | 0:a3bc6812cccb | 57 | |
Powers | 0:a3bc6812cccb | 58 | public: |
Powers | 0:a3bc6812cccb | 59 | // Constructor |
Powers | 0:a3bc6812cccb | 60 | HCSR04(PinName _trigger, PinName _echo):trigger(_trigger), echo(_echo) |
Powers | 0:a3bc6812cccb | 61 | { |
Powers | 0:a3bc6812cccb | 62 | // Attach member functions to InterruptIn for changing state on ECHO PIN [HIGH,LOW] |
Powers | 0:a3bc6812cccb | 63 | echo.rise(callback(this, &HCSR04::startTimer)); |
Powers | 0:a3bc6812cccb | 64 | echo.fall(callback(this, &HCSR04::stopTimer)); |
Powers | 0:a3bc6812cccb | 65 | |
Powers | 0:a3bc6812cccb | 66 | // Default Values |
Powers | 0:a3bc6812cccb | 67 | trigger.write(0); |
Powers | 0:a3bc6812cccb | 68 | update = false; |
Powers | 0:a3bc6812cccb | 69 | |
Powers | 0:a3bc6812cccb | 70 | } |
Powers | 0:a3bc6812cccb | 71 | |
Powers | 1:6f9e41e319ad | 72 | // Function to return the distance in cm, should be called if "checkUpdate()" returns true |
Powers | 0:a3bc6812cccb | 73 | float getDistance(void); |
Powers | 0:a3bc6812cccb | 74 | // Check for an updatet status for the measurement |
Powers | 0:a3bc6812cccb | 75 | bool checkUpdate(void); |
Powers | 0:a3bc6812cccb | 76 | // Function to start sending HIGH in periodic intervalls to TRIGGER PIN via a TICKER Object |
Powers | 0:a3bc6812cccb | 77 | void startSensor(int freq); |
Powers | 0:a3bc6812cccb | 78 | // Function to stop sending HIGH to TRIGGER PIN |
Powers | 0:a3bc6812cccb | 79 | void stopSensor(void); |
Powers | 0:a3bc6812cccb | 80 | |
Powers | 0:a3bc6812cccb | 81 | |
Powers | 0:a3bc6812cccb | 82 | }; |
Powers | 0:a3bc6812cccb | 83 | |
Powers | 0:a3bc6812cccb | 84 | #endif |