[실습세미나] 미로찾기 3팀

작성자
김재영
작성일
2022-05-14 17:45
조회
292



Servo servo1;
Servo servo2;// create servo object to control a servo

int Xval, Yval, Sel, sv1, sv2;

int trigPin=9;
int echoPin=10;
int buz=11;
float angle1=90;
float angle2=90;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(8,INPUT);

  //Digital input needs Pull-up resistor setting.
  digitalWrite(8,HIGH);

  //Servo setup with pin3
  servo1.attach(3);
  servo2.attach(5);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(11, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  Xval = analogRead(A0);
  Yval = analogRead(A1);
  Sel = digitalRead(8);

  sv1 = map(Xval,0,1023,60,110);
  sv2 = map(Yval,0,1023,70,110);
  
  if(sv1<77 && angle1<110){ angle1+=2; Serial.println(angle1); Serial.print("angle1 up"); } if(sv1>100 && angle1>60){
    angle1-=2;
    Serial.println(angle1);
    Serial.print("angle1 down");
    }
  if(sv2<77 && angle2<110){ angle2+=2; Serial.println(angle2); Serial.print("angle2 up"); } if(sv2>100 && angle2>60){
    angle2-=2;
    Serial.println(angle2);
    Serial.print("angle2 down");
    }
  servo1.write(angle1);
  servo2.write(angle2);// tell servo to go to position in variable 'pos'

  delay(15);

  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW);
  int distance1 = pulseIn(echoPin, HIGH)*17/1000;
  //Serial.println(distance);
 delay(10);

  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW);
  int distance2 = pulseIn(echoPin, HIGH)*17/1000;
  //Serial.println(distance);
  delay(10);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW);
  int distance3 = pulseIn(echoPin, HIGH)*17/1000;
  Serial.println(distance1);
if (distance1<5 && distance2<5 )
{
  tone(buz,2093,800);
      delay(1000);
}
}
전체 0