Simple code for multiple sensor test using K64f
Dependencies: mbed MQ2 FXOS8700Q DHT TextLCD ESP8266
hcsr04.h@5:a981584597fb, 2021-03-25 (annotated)
- Committer:
- abhishek_gupta
- Date:
- Thu Mar 25 10:44:23 2021 +0000
- Revision:
- 5:a981584597fb
This code represents the use of multiple sensors in a simple way in order to create a small scale smart home system which runs on its own
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
abhishek_gupta | 5:a981584597fb | 1 | /* Copyright (c) 2013 Prabhu Desai |
abhishek_gupta | 5:a981584597fb | 2 | * pdtechworld@gmail.com |
abhishek_gupta | 5:a981584597fb | 3 | * |
abhishek_gupta | 5:a981584597fb | 4 | * |
abhishek_gupta | 5:a981584597fb | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
abhishek_gupta | 5:a981584597fb | 6 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
abhishek_gupta | 5:a981584597fb | 7 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
abhishek_gupta | 5:a981584597fb | 8 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
abhishek_gupta | 5:a981584597fb | 9 | * furnished to do so, subject to the following conditions: |
abhishek_gupta | 5:a981584597fb | 10 | * |
abhishek_gupta | 5:a981584597fb | 11 | * The above copyright notice and this permission notice shall be included in all copies or |
abhishek_gupta | 5:a981584597fb | 12 | * substantial portions of the Software. |
abhishek_gupta | 5:a981584597fb | 13 | * |
abhishek_gupta | 5:a981584597fb | 14 | * For more details on the sensor : |
abhishek_gupta | 5:a981584597fb | 15 | * http://www.elecfreaks.com/store/hcsr04-ultrasonic-sensor-distance-measuring-module-p-91.html?zenid=pgm8pgnvaodbe36dibq5s1soi3 |
abhishek_gupta | 5:a981584597fb | 16 | * |
abhishek_gupta | 5:a981584597fb | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
abhishek_gupta | 5:a981584597fb | 18 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
abhishek_gupta | 5:a981584597fb | 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
abhishek_gupta | 5:a981584597fb | 20 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
abhishek_gupta | 5:a981584597fb | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
abhishek_gupta | 5:a981584597fb | 22 | */ |
abhishek_gupta | 5:a981584597fb | 23 | |
abhishek_gupta | 5:a981584597fb | 24 | #ifndef MBED_HCSR04_H |
abhishek_gupta | 5:a981584597fb | 25 | #define MBED_HCSR04_H |
abhishek_gupta | 5:a981584597fb | 26 | |
abhishek_gupta | 5:a981584597fb | 27 | #include "mbed.h" |
abhishek_gupta | 5:a981584597fb | 28 | |
abhishek_gupta | 5:a981584597fb | 29 | /** HCSR04 Class(es) |
abhishek_gupta | 5:a981584597fb | 30 | */ |
abhishek_gupta | 5:a981584597fb | 31 | |
abhishek_gupta | 5:a981584597fb | 32 | class HCSR04 |
abhishek_gupta | 5:a981584597fb | 33 | { |
abhishek_gupta | 5:a981584597fb | 34 | public: |
abhishek_gupta | 5:a981584597fb | 35 | /** Create a HCSR04 object connected to the specified pin |
abhishek_gupta | 5:a981584597fb | 36 | * @param pin i/o pin to connect to |
abhishek_gupta | 5:a981584597fb | 37 | */ |
abhishek_gupta | 5:a981584597fb | 38 | HCSR04(PinName TrigPin,PinName EchoPin); |
abhishek_gupta | 5:a981584597fb | 39 | ~HCSR04(); |
abhishek_gupta | 5:a981584597fb | 40 | |
abhishek_gupta | 5:a981584597fb | 41 | /** Return the distance from obstacle in cm |
abhishek_gupta | 5:a981584597fb | 42 | * @param distance in cms and returns -1, in case of failure |
abhishek_gupta | 5:a981584597fb | 43 | */ |
abhishek_gupta | 5:a981584597fb | 44 | unsigned int get_dist_cm(void); |
abhishek_gupta | 5:a981584597fb | 45 | /** Return the pulse duration equal to sonic waves travelling to obstacle and back to receiver. |
abhishek_gupta | 5:a981584597fb | 46 | * @param pulse duration in microseconds. |
abhishek_gupta | 5:a981584597fb | 47 | */ |
abhishek_gupta | 5:a981584597fb | 48 | unsigned int get_pulse_us(void); |
abhishek_gupta | 5:a981584597fb | 49 | /** Generates the trigger pulse of 10us on the trigger PIN. |
abhishek_gupta | 5:a981584597fb | 50 | */ |
abhishek_gupta | 5:a981584597fb | 51 | void start(void ); |
abhishek_gupta | 5:a981584597fb | 52 | void isr_rise(void); |
abhishek_gupta | 5:a981584597fb | 53 | void isr_fall(void); |
abhishek_gupta | 5:a981584597fb | 54 | void fall (void (*fptr)(void)); |
abhishek_gupta | 5:a981584597fb | 55 | void rise (void (*fptr)(void)); |
abhishek_gupta | 5:a981584597fb | 56 | |
abhishek_gupta | 5:a981584597fb | 57 | |
abhishek_gupta | 5:a981584597fb | 58 | |
abhishek_gupta | 5:a981584597fb | 59 | private: |
abhishek_gupta | 5:a981584597fb | 60 | |
abhishek_gupta | 5:a981584597fb | 61 | Timer pulsetime; |
abhishek_gupta | 5:a981584597fb | 62 | DigitalOut trigger; |
abhishek_gupta | 5:a981584597fb | 63 | InterruptIn echo; |
abhishek_gupta | 5:a981584597fb | 64 | unsigned int pulsedur; |
abhishek_gupta | 5:a981584597fb | 65 | unsigned int distance; |
abhishek_gupta | 5:a981584597fb | 66 | }; |
abhishek_gupta | 5:a981584597fb | 67 | |
abhishek_gupta | 5:a981584597fb | 68 | #endif |