Page 152 - 최강 아두이노 스마트 자동차 로봇 키트
P. 152
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per
centimeter.
// The ping travels out and back, so to find the
distance of the
// object we take half of the distance travelled.
// 시간에 대한 값을 센티미터로 변환
return microseconds / 29 / 2;
}
// global instance ultrasonic sensor.
//NewPing sonar_distance(TRIGGER_PIN, ECHO_PIN,
MAX_DISTANCE);
// global instance servo motor.
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
//
// get value current distance from HC-SR04 ( ultrasonic )
//
int getCurrDistance()
{
/*
int uS = sonar_distance.ping();
return uS / US_ROUNDTRIP_CM;
*/
long duration, cm;
digitalWrite(ECHO_PIN, LOW);
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10); //
digitalWrite(TRIGGER_PIN, LOW);
//
duration = pulseIn(ECHO_PIN, HIGH); // Echo pin: HIGH-
>Low 간격을 측정
cm = microsecondsToCentimeters(duration); // 거리(cm)로
변환
151