#include <Servo.h>

#define ECHO_PIN 6
#define TRIG_PIN 5
#define pinBuzzer 7

Servo myservo;

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(pinBuzzer, OUTPUT);

Serial.begin(115200);
myservo.attach(10);
}

float readDistanceCM() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(ECHO_PIN, HIGH);
  int duration = pulseIn(ECHO_PIN, HIGH);
  return duration * 0.034 / 2;
}

void loop() {
  float distance = readDistanceCM();
  Serial.print("JARAK SENSOR: ");
  Serial.println(readDistanceCM());

if(distance <= 200){
  myservo.write(90);
  delay(15);
  tone(pinBuzzer, 1000);
  delay(200);
  tone(pinBuzzer, 500);
  delay(200);
}
else(
  myservo.write(0));
  noTone(pinBuzzer);

}