-
Notifications
You must be signed in to change notification settings - Fork 65
/
UltrasonicSimple.ino
59 lines (54 loc) · 1.82 KB
/
UltrasonicSimple.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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);
}