스크래치2.0 & 아두이노

> 피지컬컴퓨팅 > [SW] 코드아이

int Echo = 12;
int Trig = 13;
int Buzzer = 8;

void setup() {
  Serial.begin(57600);
  pinMode(Echo, INPUT);
  pinMode(Trig, OUTPUT);
}

void loop() {
  long duration;
 
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(5);
  digitalWrite(Trig, LOW);

  duration = pulseIn(Echo, HIGH) / 29 / 2;
 
  if (duration < 30) {
    tone(Buzzer, 196, 100);
    delay(duration * 25);
  }
  delay(10);
}