HC-04 UltraSonic Basic Test (with Arduino)
참고사이트 (☜Click)
선연결 순서 :: Green LED, Red LED, Echo, Trig 순서대로 각각 D10,D11,D12,D13 이다. 나머지는 VCC,GND를 Arduino에 연결해줄것
▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
Red POS to Arduino pin 11
Green POS to Arduino pin 10
560 ohm resistor to both LED NEG and GRD power rail
More info at: http://goo.gl/kJ8Gl
Original code improvements to the Ping sketch sourced from Trollmaker.com
Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar
*/
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10
void setup()
{
Serial.begin (9600); //통신속도
pinMode(trigPin, OUTPUT); //아두이노 ▶ 초음파센서
pinMode(echoPin, INPUT); //초음파센서 ▶ 아두이노
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop()
{
long duration, distance; //거리, 지속시간
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW); //여기 이후 딜레이는 Forever
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1; //cm단위로 거리연산
if (distance < 4) //거리가 4cm이하라면
{
// This is where the LED On/Off happens
digitalWrite(led,HIGH); //적색 ON
// When the Red condition is met, the Green LED should turn off
digitalWrite(led2,LOW); //녹색 OFF
}
else //거리가 4cm이상이라면
{
digitalWrite(led,LOW); //적색 OFF
digitalWrite(led2,HIGH); //녹색 ON
}
if (distance >= 200 || distance <= 0) //거리가 범위를 벗어나면
{
Serial.println("Out of range");
}
else //범위안에 있다면 시리얼 모니터에 표시
{
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
BikeBraking System HEX code