1. 아두이노 우노보드
2. 아두이노 IDE
3. 서보모터(SG90), 초음파센서(HC-SR04)
#include <Servo.h>
const int trigPin1 = 7;
const int echoPin1 = 8;
Servo myservo;
int pos = 0;
int motor =3;
long duration1, distance1;
void setup()
{
Serial.begin(9600);
myservo.attach(3);
pinMode(trigPin1,OUTPUT);
pinMode(echoPin1,INPUT);
}
void loop()
{
long duration1, distance1;
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = (duration1/2) / 29.1;
if (distance1 < 15 && distance1 > 0)
{
digitalWrite(8 , HIGH);
}
else if (distance1 >= 15)
{
for(pos = 0; pos <= 180; pos += 1)
{
myservo.write(pos);
delay(1000);
}
for(pos = 180; pos>=0; pos-=1)
{
myservo.write(pos);
delay(1000);
}
}
else if (distance1 > 30)
{
digitalWrite(7 , LOW);
}
Serial.println(pos);
myservo.detach();
delay(10000);
}
저희가 거리감지센서를 이용하여 서보모터를 돌린후 detach를 이용하여 서보모터의 작동을 멈춘후 다시 attach로 작동하기를 원합니다...
detach,attach 올바른 사용 방법좀 알려주시면 감사하겠습니다.
|