初始化提交
This commit is contained in:
46
arduino-cli/libraries/Ultrasonic/CODE_OF_CONDUCT.md
Normal file
46
arduino-cli/libraries/Ultrasonic/CODE_OF_CONDUCT.md
Normal file
@@ -0,0 +1,46 @@
|
||||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to creating a positive environment include:
|
||||
|
||||
* Using welcoming and inclusive language
|
||||
* Being respectful of differing viewpoints and experiences
|
||||
* Gracefully accepting constructive criticism
|
||||
* Focusing on what is best for the community
|
||||
* Showing empathy towards other community members
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
* The use of sexualized language or imagery and unwelcome sexual attention or advances
|
||||
* Trolling, insulting/derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or electronic address, without explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a professional setting
|
||||
|
||||
## Our Responsibilities
|
||||
|
||||
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
|
||||
|
||||
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at erick.simoes@live.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
|
||||
|
||||
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
|
||||
|
||||
[homepage]: http://contributor-covenant.org
|
||||
[version]: http://contributor-covenant.org/version/1/4/
|
||||
42
arduino-cli/libraries/Ultrasonic/CONTRIBUTING.md
Normal file
42
arduino-cli/libraries/Ultrasonic/CONTRIBUTING.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# Contributing to Ultrasonic
|
||||
|
||||
Tank you very much for taking the time to contribute! :+1:
|
||||
|
||||
Here you will find guidelines for contributing to Ultrasonic. Feel free to propose changes to this document in a pull request.
|
||||
|
||||
## Code Conduct
|
||||
|
||||
This project and everyone participating in it is governed by the [Ultrasonic Code of Conduct](CODE_OF_CONDUCT.md). By participating, you are expected to uphold this code. Please report unacceptable behavior to [erick.simoes@live.com](mailto:erick.simoes@live.com).
|
||||
|
||||
## I have a question
|
||||
|
||||
Soon we will have a Wiki, until there, you can get your questions through (in order of preference):
|
||||
- GitHub issues
|
||||
- posting a message on [Makerama](https://www.facebook.com/groups/makerama/?source_id=449693438550360)'s group on Facebook
|
||||
- sending a message on [Makerama](https://www.facebook.com/Makerama.br/)'s page on Facebook
|
||||
- send a email to [erick.simoes@live.com](mailto:erick.simoes@live.com)
|
||||
> Note: Prefer to ask your question through some public channel, so that others can learn too
|
||||
|
||||
## Ways to help this project:
|
||||
1. Using
|
||||
- Use this library in your projects (open source or privates),
|
||||
> Note: You will make the day of the creator of this lib very happy to tell him that you are using in your project :+1:
|
||||
2. Testing
|
||||
- Use the features and try in various devices.
|
||||
3. Suggesting a feature
|
||||
- Do you need some feature? Suggest it as a issue.
|
||||
4. Notifying a bug
|
||||
- Did you notice any strange behavior? Reporter the bug as a issue!
|
||||
5. Implementing
|
||||
- You can do enhancement, fix a bug, solve an issue or expand the documentation.
|
||||
|
||||
## Premises:
|
||||
This project was elaborated following some premises:
|
||||
#### Minimalism:
|
||||
- The minimum memory consumption;
|
||||
- The minimum of features;
|
||||
- Code minimum.
|
||||
#### Oriented to the developer experience
|
||||
- As simple as possible to use.
|
||||
|
||||
Thanks! :heart:
|
||||
21
arduino-cli/libraries/Ultrasonic/LICENSE
Normal file
21
arduino-cli/libraries/Ultrasonic/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Erick Simões
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
98
arduino-cli/libraries/Ultrasonic/README.md
Normal file
98
arduino-cli/libraries/Ultrasonic/README.md
Normal file
@@ -0,0 +1,98 @@
|
||||
[](https://github.com/ellerbrock/open-source-badges/)
|
||||
[](https://opensource.org/licenses/mit-license.php)
|
||||
|
||||
Ultrasonic
|
||||
===========
|
||||
|
||||
_Minimalist library for ultrasound module to Arduino_
|
||||
|
||||
### Compatible with **HC-SR04**, **Ping)))** and **Seeed SEN136B5B** (_from Seeed Studio_)
|
||||
|
||||
Work with **ultrasonic modules** is fairly simple, but can be even more practical if you abstract the control of some features. This library aims to resource efficiency and to simplify access to data.
|
||||
|
||||
Where necessary use the ultrasonic module **HC-SR04** (one of the most common on the market), **Ping)))** and/or **Seeed SEN136B5B** (_from Seeed Studio_), there are hundreds of libraries that purport to provide the most diverse roles for the user, however, the vast majority of the time, we just need to find out the distance and is that's what does this library.
|
||||
|
||||
This library is minimalist, reduces code execution, validation and unnecessary use of global variables, prioritizing smaller data types.
|
||||
|
||||
Wiring:
|
||||
---------------
|
||||
It is very easy to connect an ultrasound module to the Arduino. For example, if you are using **HC-SR04**, connect the **trigger** and **echo** pin module on pin **12** and **13** of the Arduino, respectively. As in the picture:
|
||||

|
||||
|
||||
If you are using a module with three pins (like **Ping)))** or **Seeed SEN136B5B**), you can conect the **sig** pin module on pin **13** of the Arduino.
|
||||
|
||||
> You can use the [Fritzing](http://fritzing.org/home/)(_.fzz_) files inside [extras](https://github.com/ErickSimoes/Ultrasonic/tree/master/extras) to draw your prototypes.
|
||||
|
||||
How to use:
|
||||
---------------
|
||||
The idea is to provide a simpler environment possible. To do this, simply follow the steps:
|
||||
|
||||
1. **Installing**
|
||||
|
||||
First you need to import the library so that the IDE recognizes it. The simplest way is importing through the IDE itself:
|
||||
- Click in ```Sketch > Include Library > Manage Libraries...```;
|
||||
- In the search field type: ```ultrasonic```;
|
||||
- In the list, look for ```Ultrasonic by Erick Simões```;
|
||||
- Click on ```Install```.
|
||||
|
||||
> Alternatively, you can download the library [here](https://github.com/ErickSimoes/Ultrasonic/archive/master.zip) and import the ```.zip``` file into the IDE (see how to import a library [here](https://www.arduino.cc/en/Guide/Libraries#toc4)).
|
||||
2. **Importing on code**
|
||||
|
||||
To import the library to your code, just write at the beginning of the code ```#include <Ultrasonic.h>``` or, in the Arduino IDE, click in ```Sketch > Include Library > Ultrasonic``` (_will have the same result_).
|
||||
3. **Starting** (the most exciting part)
|
||||
|
||||
Now is simply create a variable of type Ultrasonic passing as parameters two values representing, respectively, the Trig (emitter) and Echo (receiver) pins. Like this:
|
||||
```c++
|
||||
Ultrasonic ultrasonic(12, 13);
|
||||
```
|
||||
If you are using a module with three pins (like **Ping)))** or **Seeed SEN136B5B**), pass as a parameter only the signal pin. Like this:
|
||||
```c++
|
||||
Ultrasonic ultrasonic(13);
|
||||
```
|
||||
4. **Discovering the distance**
|
||||
|
||||
Having initialized a variable, you can run hers from the method that returns the distance read by module Ultrasonic: ```read()```:
|
||||
```c++
|
||||
ultrasonic.read()
|
||||
```
|
||||
5. **Only this?**
|
||||
|
||||
Yes. That's it. By default, the value returned from the function ```read()``` is the distance in centimeters.
|
||||
|
||||
6. **Seriously?**
|
||||
|
||||
You can still do a little more determining the unit of measurement that will be returned (centimeters (CM) or inches (INC)).
|
||||
```c++
|
||||
ultrasonic.read() // distance in CM
|
||||
ultrasonic.read(CM) // distance in CM
|
||||
ultrasonic.read(INC) // distance in INC
|
||||
```
|
||||
You can also use more than one ultrasound module:
|
||||
```c++
|
||||
ultrasonic ultrasound1(12, 13);
|
||||
ultrasonic ultrasound2(10, 11);
|
||||
ultrasonic ultrasound3(5);
|
||||
```
|
||||
|
||||
7. **Timeouts**
|
||||
|
||||
If there is no object in range, the library will lock-up as it waits for the return pulse.
|
||||
You can change how long to wait by setting a timeout (in microseconds) in the constructor:
|
||||
```c++
|
||||
Ultrasonic ultrasonic(12, 13, 40000UL);
|
||||
```
|
||||
Or during runtime:
|
||||
```c++
|
||||
ultrasonic.setTimeout(40000UL);
|
||||
```
|
||||
Using a 40ms timeout should give you a maximum range of approximately 6.8m. You may need to adjust this parameter.
|
||||
|
||||
#### See the examples [here](https://github.com/ErickSimoes/Ultrasonic/tree/master/examples).
|
||||
|
||||
License
|
||||
----
|
||||
Ultrasonic by [Erick Simões](http://ericksimoes.com.br/ "Erick Simões") is licensed under a MIT License.
|
||||
Based on the work of Carl John Nobile available [here](http://wiki.tetrasys-design.net/HCSR04Ultrasonic).
|
||||
Feel free to contact the author on Twitter: [@AloErickSimoes](https://twitter.com/AloErickSimoes)
|
||||
|
||||
See [LICENSE](https://github.com/ErickSimoes/Ultrasonic/blob/master/LICENSE) for details.
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Miltiple Ultrasonic Sensors
|
||||
* Prints the distance read by many ultrasonic sensors in
|
||||
* centimeters and inches. They are supported to four pins
|
||||
* ultrasound sensors (liek HC-SC04) and three pins
|
||||
* (like PING))) and Seeed Studio sensors).
|
||||
*
|
||||
* The circuit:
|
||||
* * In this circuit there is an ultrasonic module HC-SC04,
|
||||
* PING))) and a Seeed Studio (4 pins, 3 pins, 3 pins,
|
||||
* respectively), attached to digital pins as follows:
|
||||
* --------------------- --------------------- -------------------
|
||||
* | HC-SC04 | Arduino | | PING))) | Arduino | | Seeed | Arduino |
|
||||
* --------------------- --------------------- -------------------
|
||||
* | Vcc | 5V | | Vcc | 5V | | Vcc | 5V |
|
||||
* | Trig | 12 | AND | SIG | 10 | AND | SIG | 8 |
|
||||
* | Echo | 13 | | Gnd | GND | | Gnd | GND |
|
||||
* | Gnd | GND | --------------------- -------------------
|
||||
* ---------------------
|
||||
* Note: You do not obligatorily need to use the pins defined above
|
||||
*
|
||||
* By default, the distance returned by the read()
|
||||
* method is in centimeters. To get the distance in inches,
|
||||
* pass INC as a parameter.
|
||||
* Example: ultrasonic.read(INC)
|
||||
*
|
||||
* created 3 Mar 2017
|
||||
* by Erick Simões (github: @ErickSimoes | twitter: @AloErickSimoes)
|
||||
* modified 11 Jun 2018
|
||||
* by Erick Simões (github: @ErickSimoes | twitter: @AloErickSimoes)
|
||||
*
|
||||
* This example code is released into the MIT License.
|
||||
*/
|
||||
|
||||
#include <Ultrasonic.h>
|
||||
|
||||
Ultrasonic ultrasonic1(12, 13); // An ultrasonic sensor HC-04
|
||||
Ultrasonic ultrasonic2(10); // An ultrasonic sensor PING)))
|
||||
Ultrasonic ultrasonic3(8); // An Seeed Studio ultrasonic sensor
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.print("Sensor 01: ");
|
||||
Serial.print(ultrasonic1.read()); // Prints the distance on the default unit (centimeters)
|
||||
Serial.println("cm");
|
||||
|
||||
Serial.print("Sensor 02: ");
|
||||
Serial.print(ultrasonic2.read(CM)); // Prints the distance making the unit explicit
|
||||
Serial.println("cm");
|
||||
|
||||
Serial.print("Sensor 03: ");
|
||||
Serial.print(ultrasonic3.read(INC)); // Prints the distance in inches
|
||||
Serial.println("inc");
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Timeout
|
||||
* Prints the distance read by an ultrasonic sensor in
|
||||
* centimeters and change the default timeout. They are
|
||||
* supported to four pins ultrasound sensors (liek HC-SC04)
|
||||
* and three pins (like PING))) and Seeed Studio sensors).
|
||||
*
|
||||
* The circuit:
|
||||
* * Module HR-SC04 (four pins) or PING))) (and other with
|
||||
* three pins), attached to digital pins as follows:
|
||||
* --------------------- --------------------
|
||||
* | HC-SC04 | Arduino | | 3 pins | Arduino |
|
||||
* --------------------- --------------------
|
||||
* | Vcc | 5V | | Vcc | 5V |
|
||||
* | Trig | 12 | OR | SIG | 13 |
|
||||
* | Echo | 13 | | Gnd | GND |
|
||||
* | Gnd | GND | --------------------
|
||||
* ---------------------
|
||||
* Note: You do not obligatorily need to use the pins defined above
|
||||
*
|
||||
* By default, the distance returned by the read()
|
||||
* method is in centimeters. To get the distance in inches,
|
||||
* pass INC as a parameter.
|
||||
* Example: ultrasonic.read(INC)
|
||||
*
|
||||
* created 11 Jun 2018
|
||||
* by Erick Simões (github: @ErickSimoes | twitter: @AloErickSimoes)
|
||||
*
|
||||
* This example code is released into the MIT License.
|
||||
*/
|
||||
|
||||
#include <Ultrasonic.h>
|
||||
|
||||
/**
|
||||
* Is possible set the timeout in the constructor, like:
|
||||
* Ultrasonic ultrasonic(12, 13, 20000UL);
|
||||
*/
|
||||
Ultrasonic ultrasonic(12, 13);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
ultrasonic.setTimeout(40000UL);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.print("Distance in CM: ");
|
||||
Serial.println(ultrasonic.read());
|
||||
delay(1000);
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Ultrasonic Simple
|
||||
* Prints the distance read by an ultrasonic sensor in
|
||||
* centimeters. They are supported to four pins ultrasound
|
||||
* sensors (liek HC-SC04) and three pins (like PING)))
|
||||
* and Seeed Studio sensors).
|
||||
*
|
||||
* The circuit:
|
||||
* * Module HR-SC04 (four pins) or PING))) (and other with
|
||||
* three pins), attached to digital pins as follows:
|
||||
* --------------------- --------------------
|
||||
* | HC-SC04 | Arduino | | 3 pins | Arduino |
|
||||
* --------------------- --------------------
|
||||
* | Vcc | 5V | | Vcc | 5V |
|
||||
* | Trig | 12 | OR | SIG | 13 |
|
||||
* | Echo | 13 | | Gnd | GND |
|
||||
* | Gnd | GND | --------------------
|
||||
* ---------------------
|
||||
* Note: You do not obligatorily need to use the pins defined above
|
||||
*
|
||||
* By default, the distance returned by the read()
|
||||
* method is in centimeters. To get the distance in inches,
|
||||
* pass INC as a parameter.
|
||||
* Example: ultrasonic.read(INC)
|
||||
*
|
||||
* created 3 Apr 2014
|
||||
* by Erick Simões (github: @ErickSimoes | twitter: @AloErickSimoes)
|
||||
* modified 23 Jan 2017
|
||||
* by Erick Simões (github: @ErickSimoes | twitter: @AloErickSimoes)
|
||||
* modified 03 Mar 2017
|
||||
* by Erick Simões (github: @ErickSimoes | twitter: @AloErickSimoes)
|
||||
* modified 11 Jun 2018
|
||||
* by Erick Simões (github: @ErickSimoes | twitter: @AloErickSimoes)
|
||||
*
|
||||
* This example code is released into the MIT License.
|
||||
*/
|
||||
|
||||
#include <Ultrasonic.h>
|
||||
|
||||
/*
|
||||
* Pass as a parameter the trigger and echo pin, respectively,
|
||||
* or only the signal pin (for sensors 3 pins), like:
|
||||
* Ultrasonic ultrasonic(13);
|
||||
*/
|
||||
Ultrasonic ultrasonic(12, 13);
|
||||
int distance;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Pass INC as a parameter to get the distance in inches
|
||||
distance = ultrasonic.read();
|
||||
|
||||
Serial.print("Distance in CM: ");
|
||||
Serial.println(distance);
|
||||
delay(1000);
|
||||
}
|
||||
BIN
arduino-cli/libraries/Ultrasonic/extras/Ultrasonic.fzz
Normal file
BIN
arduino-cli/libraries/Ultrasonic/extras/Ultrasonic.fzz
Normal file
Binary file not shown.
24
arduino-cli/libraries/Ultrasonic/keywords.txt
Normal file
24
arduino-cli/libraries/Ultrasonic/keywords.txt
Normal file
@@ -0,0 +1,24 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map Ultrasonic
|
||||
# by Erick Simões
|
||||
# github: @ErickSimoes
|
||||
# twitter: @AloErickSimoes
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
Ultrasonic KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
read KEYWORD2
|
||||
distanceRead KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
CM LITERAL1 Constants
|
||||
INC LITERAL1 Constants
|
||||
10
arduino-cli/libraries/Ultrasonic/library.properties
Normal file
10
arduino-cli/libraries/Ultrasonic/library.properties
Normal file
@@ -0,0 +1,10 @@
|
||||
name=Ultrasonic
|
||||
version=3.0.0
|
||||
author=Erick Simões <erick.simoes@live.com>
|
||||
maintainer=Erick Simões <erick.simoes@live.com>
|
||||
sentence=Minimalist library for ultrasound module to Arduino
|
||||
paragraph=Work with ultrasound module in a simple and light way. Compatible with the modules HC-SR04, Ping))) and Seeed Studio sensor. This library aims to resource efficiency and to simplify access to data.
|
||||
category=Sensors
|
||||
url=https://github.com/ErickSimoes/Ultrasonic
|
||||
architectures=*
|
||||
includes=Ultrasonic.h
|
||||
75
arduino-cli/libraries/Ultrasonic/src/Ultrasonic.cpp
Normal file
75
arduino-cli/libraries/Ultrasonic/src/Ultrasonic.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Ultrasonic.cpp
|
||||
*
|
||||
* Library for Ultrasonic Ranging Module in a minimalist way
|
||||
*
|
||||
* created 3 Apr 2014
|
||||
* by Erick Simões (github: @ErickSimoes | twitter: @AloErickSimoes)
|
||||
* modified 23 Jan 2017
|
||||
* by Erick Simões (github: @ErickSimoes | twitter: @AloErickSimoes)
|
||||
* modified 04 Mar 2017
|
||||
* by Erick Simões (github: @ErickSimoes | twitter: @AloErickSimoes)
|
||||
* modified 15 May 2017
|
||||
* by Eliot Lim (github: @eliotlim)
|
||||
* modified 10 Jun 2018
|
||||
* by Erick Simões (github: @ErickSimoes | twitter: @AloErickSimoes)
|
||||
* modified 14 Jun 2018
|
||||
* by Otacilio Maia (github: @OtacilioN | linkedIn: in/otacilio)
|
||||
*
|
||||
* Released into the MIT License.
|
||||
*/
|
||||
|
||||
#if ARDUINO >= 100
|
||||
#include <Arduino.h>
|
||||
#else
|
||||
#include <WProgram.h>
|
||||
#endif
|
||||
|
||||
#include "Ultrasonic.h"
|
||||
|
||||
Ultrasonic::Ultrasonic(uint8_t trigPin, uint8_t echoPin, unsigned long timeOut) {
|
||||
trig = trigPin;
|
||||
echo = echoPin;
|
||||
threePins = trig == echo ? true : false;
|
||||
pinMode(trig, OUTPUT);
|
||||
pinMode(echo, INPUT);
|
||||
timeout = timeOut;
|
||||
}
|
||||
|
||||
unsigned int Ultrasonic::timing() {
|
||||
if (threePins)
|
||||
pinMode(trig, OUTPUT);
|
||||
|
||||
digitalWrite(trig, LOW);
|
||||
delayMicroseconds(2);
|
||||
digitalWrite(trig, HIGH);
|
||||
delayMicroseconds(10);
|
||||
digitalWrite(trig, LOW);
|
||||
|
||||
if (threePins)
|
||||
pinMode(trig, INPUT);
|
||||
|
||||
previousMicros = micros();
|
||||
while(!digitalRead(echo) && (micros() - previousMicros) <= timeout); // wait for the echo pin HIGH or timeout
|
||||
previousMicros = micros();
|
||||
while(digitalRead(echo) && (micros() - previousMicros) <= timeout); // wait for the echo pin LOW or timeout
|
||||
|
||||
return micros() - previousMicros; // duration
|
||||
}
|
||||
|
||||
/*
|
||||
* If the unit of measure is not passed as a parameter,
|
||||
* sby default, it will return the distance in centimeters.
|
||||
* To change the default, replace CM by INC.
|
||||
*/
|
||||
unsigned int Ultrasonic::read(uint8_t und) {
|
||||
return timing() / und / 2; //distance by divisor
|
||||
}
|
||||
|
||||
/*
|
||||
* This method is too verbal, so, it's deprecated.
|
||||
* Use read() instead.
|
||||
*/
|
||||
unsigned int Ultrasonic::distanceRead(uint8_t und) {
|
||||
return read(und);
|
||||
}
|
||||
46
arduino-cli/libraries/Ultrasonic/src/Ultrasonic.h
Normal file
46
arduino-cli/libraries/Ultrasonic/src/Ultrasonic.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Ultrasonic.h
|
||||
*
|
||||
* Library for Ultrasonic Ranging Module in a minimalist way
|
||||
*
|
||||
* created 3 Apr 2014
|
||||
* by Erick Simões (github: @ErickSimoes | twitter: @AloErickSimoes)
|
||||
* modified 23 Jan 2017
|
||||
* by Erick Simões (github: @ErickSimoes | twitter: @AloErickSimoes)
|
||||
* modified 04 Mar 2017
|
||||
* by Erick Simões (github: @ErickSimoes | twitter: @AloErickSimoes)
|
||||
* modified 15 May 2017
|
||||
* by Eliot Lim (github: @eliotlim)
|
||||
* modified 10 Jun 2018
|
||||
* by Erick Simões (github: @ErickSimoes | twitter: @AloErickSimoes)
|
||||
*
|
||||
* Released into the MIT License.
|
||||
*/
|
||||
|
||||
#ifndef Ultrasonic_h
|
||||
#define Ultrasonic_h
|
||||
|
||||
/*
|
||||
* Values of divisors
|
||||
*/
|
||||
#define CM 28
|
||||
#define INC 71
|
||||
|
||||
class Ultrasonic {
|
||||
public:
|
||||
Ultrasonic(uint8_t sigPin) : Ultrasonic(sigPin, sigPin) {};
|
||||
Ultrasonic(uint8_t trigPin, uint8_t echoPin, unsigned long timeOut = 20000UL);
|
||||
unsigned int read(uint8_t und = CM);
|
||||
unsigned int distanceRead(uint8_t und = CM) __attribute__ ((deprecated ("This method is deprecated, use read() instead.")));
|
||||
void setTimeout(unsigned long timeOut) {timeout = timeOut;}
|
||||
|
||||
private:
|
||||
uint8_t trig;
|
||||
uint8_t echo;
|
||||
boolean threePins = false;
|
||||
unsigned long previousMicros;
|
||||
unsigned long timeout;
|
||||
unsigned int timing();
|
||||
};
|
||||
|
||||
#endif // Ultrasonic_h
|
||||
Reference in New Issue
Block a user