/*
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(4, 6);
int distance;
int distance2;
int led1 = 9;
int led2 = 11;
int led3 = 12;
void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
void loop() {
// Pass INC as a parameter to get the distance in inches
distance = ultrasonic.read(CM);
Serial.print("Distance in CM: ");
Serial.println(distance);
distance2 = ultrasonic.read(INC);
Serial.print("Distance in Inches: ");
Serial.println(distance2);
Serial.println();
delay(1000);
if(distance < 100)
{
digitalWrite(led1, HIGH);
delay(1000);
digitalWrite(led1, LOW);
delay(1000);
}
else if(distance >= 100 && distance <= 149)
{
digitalWrite(led2, HIGH);
delay(1000);
digitalWrite(led2, LOW);
delay(1000);
}
else if(distance >= 150)
{
digitalWrite(led3, HIGH);
delay(1000);
digitalWrite(led3, LOW);
delay(1000);
}
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
ultrasonic:VCC
ultrasonic:TRIG
ultrasonic:ECHO
ultrasonic:GND
led1:A
led1:C
r1:1
r1:2
led2:A
led2:C
r2:1
r2:2
led3:A
led3:C
r3:1
r3:2