Easyuino  1.2.0
DistanceMeter.h
1 /*
2 MIT License
3 
4 Copyright (c) 2017 André Pires
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 /*
25 DistanceMeter.h
26 */
27 #ifndef _EASYUINO_DISTANCE_METER_h
28 #define _EASYUINO_DISTANCE_METER_h
29 
30 #include "Utilities.h"
31 #include "Device.h"
32 
34 #define ECHO_TIMEOUT_MICROS 30000UL
35 
36 #define DELAY_AFTER_TRIGGER_LOW_MICROS 2
37 
38 #define ECHO_PULSE_DURATION_MICROS 10
39 
41 #define DEFAULT_AIR_TEMPERATURE_CELSIUS 20.0f
42 
43 #define DEFAULT_SOUND_SPEED_CM_PER_SEC 34340.0f
44 
45 namespace Easyuino {
46 
59  class DistanceMeter : public Device {
60 
61  protected:
63  uint8_t _triggerPin;
65  uint8_t _echoPin;
67  volatile bool _isEchoing;
69  volatile float _distance;
70 
71  public:
76  DistanceMeter(IN uint8_t triggerPin, IN uint8_t echoPin);
77 
81  DistanceMeter(IN uint8_t triggerEchoPin);
82 
85 
86  bool begin();
87 
88  void end();
89 
93  virtual float getDistanceCentimeters();
94 
98  float getDistanceInches();
99 
105  virtual void updateDistance();
106 
107  protected:
112  float executeUpdateDistanceBlock(IN float soundSpeed);
113 
114  };
115 
116 };
117 
118 
119 #endif
float getDistanceInches()
Gets the last value that the API measured using the US.
Used to represent the at least supported Ultrasonic Module models enum UltrasonicModule : uint8_t { H...
Definition: DistanceMeter.h:59
~DistanceMeter()
Destructor.
uint8_t _triggerPin
Arduino pin used to trigger the echo wave emission.
Definition: DistanceMeter.h:63
DistanceMeter(IN uint8_t triggerPin, IN uint8_t echoPin)
Constructor.
bool begin()
Used to put the device ready to receive requests.
virtual void updateDistance()
Updates the distance of the Ultrasonic Module to the objects in a blocking way.
volatile bool _isEchoing
Used to know if it is in a middle of a measurement.
Definition: DistanceMeter.h:67
Definition: Button.h:38
float executeUpdateDistanceBlock(IN float soundSpeed)
Execute a block distance measurement and calculates the distance based on the sound speed...
volatile float _distance
It contains a cached value of the last distance measured.
Definition: DistanceMeter.h:69
General class that provides the common API behaviour for all the devices/sensors. ...
Definition: Device.h:42
virtual float getDistanceCentimeters()
Gets the last value that the API measured using the Ultrasonic Module.
uint8_t _echoPin
Arduino pin that is set to LOW by when reflected echo wave arrives.
Definition: DistanceMeter.h:65
void end()
Used to stop the device API.