From be42b5aefcdc5c399ca66c40b6bb18d05d0722a8 Mon Sep 17 00:00:00 2001 From: Subodh-roy2 <122513473+Subodh-roy2@users.noreply.github.com> Date: Sat, 23 Dec 2023 13:02:59 +0530 Subject: [PATCH 1/2] Update repositories.txt --- repositories.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/repositories.txt b/repositories.txt index 9f3f49961..ef323a642 100644 --- a/repositories.txt +++ b/repositories.txt @@ -6506,3 +6506,4 @@ https://github.com/scheffield/sic45x-driver https://github.com/GabyGold67/SevenSegDisplaysRTOSLib https://github.com/randomouscrap98/arduboy_raycast https://github.com/Xinyuan-LilyGO/LilyGo-AMOLED-Series.git +https://github.com/Subodh-roy2/test_libr From 00f6d7cb8cdb5799f82dd19531615c96ad1c1475 Mon Sep 17 00:00:00 2001 From: Subodh-roy2 <122513473+Subodh-roy2@users.noreply.github.com> Date: Sat, 23 Dec 2023 13:09:28 +0530 Subject: [PATCH 2/2] Add files via upload --- keywords.txt | 18 +++++++++++ library.properties | 10 +++++++ src/Ultrasonic.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++ src/Ultrasonic.h | 46 ++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 keywords.txt create mode 100644 library.properties create mode 100644 src/Ultrasonic.cpp create mode 100644 src/Ultrasonic.h diff --git a/keywords.txt b/keywords.txt new file mode 100644 index 000000000..4aa7cee5a --- /dev/null +++ b/keywords.txt @@ -0,0 +1,18 @@ +####################################### +# Datatypes (KEYWORD1) +####################################### + +Ultrasonic KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### +read KEYWORD2 +distanceRead KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### +CM LITERAL1 Constants +INC LITERAL1 Constants + diff --git a/library.properties b/library.properties new file mode 100644 index 000000000..4e167031c --- /dev/null +++ b/library.properties @@ -0,0 +1,10 @@ +name=Ultrasonic +version=2.1.0 +author=Erick Simões +maintainer=Erick Simões +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 diff --git a/src/Ultrasonic.cpp b/src/Ultrasonic.cpp new file mode 100644 index 000000000..77008731a --- /dev/null +++ b/src/Ultrasonic.cpp @@ -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 +#else + #include +#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); +} diff --git a/src/Ultrasonic.h b/src/Ultrasonic.h new file mode 100644 index 000000000..0809d3c7d --- /dev/null +++ b/src/Ultrasonic.h @@ -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