#include <Servo.h>

// Program Lampu Lalu Lintas
byte led1 = 13; // Merah bawah
byte led2 = 12; // Kuning bawah
byte led3 = 11; // Hijau bawah
byte led4 = 10; // Merah kiri
byte led5 = 9;  // Kuning Kiri
byte led6 = 8;  // Hijau Kiri
byte led7 = 7;  // Merah kanan
byte led8 = 6;  // Kuning kanan
byte led9 = 1;  // Hijau Kanan

// Program Palang Kereta
#define ECHO_PIN 3
#define TRIG_PIN 2
#define pinBuzzer 5
Servo palang;
int potpin = 0;
int val;
bool isGateOpen = false;

void setup() {
  // Setup Program Lampu Lalu Lintas
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);
  pinMode(led9, OUTPUT);

  // Setup Program Palang Kereta
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  Serial.begin(115200);
  pinMode(pinBuzzer, OUTPUT);
  palang.attach(4);
}

void loop() {
  // Program Palang Kereta
  runProgram1Logic();

  // Program Lampu Lalu Lintas
  runProgram2Logic();
}

void runProgram1Logic() {
  float distance = readDistanceCM();
  Serial.print("Jarak Kereta Api:");
  Serial.println(distance);

  // Kondisi logika apakah di bawah 200
  if (distance <= 200) {
    if (!isGateOpen) {
      palang.write(90);
      isGateOpen = true;
      tone(pinBuzzer, 500);
      delay(1000);
      tone(pinBuzzer, 800);
    }
  } else {
    if (isGateOpen) {
      palang.write(0);
      isGateOpen = false;
      noTone(pinBuzzer);
    }
  }

  val = analogRead(potpin);
  val = map(val, 0, 1023, 0, 90);
  palang.write(val);
  delay(15);
}

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

void runProgram2Logic() {
  // Program Lampu Lalu Lintas
  digitalWrite(led3, HIGH); // Hijau bawah
  digitalWrite(led4, HIGH); // Merah Kiri
  digitalWrite(led7, HIGH); // Merah Kanan
  delay(5000);
  digitalWrite(led3, LOW);  // Hijau Atas
  digitalWrite(led7, LOW);  // Merah Kanan

  digitalWrite(led2, HIGH); // Kuning bawah
  digitalWrite(led8, HIGH); // Kuning Kanan
  delay(800);
  digitalWrite(led2, LOW);  // Kuning Atas
  digitalWrite(led8, LOW);  // Kuning Kanan

  // KONDISI 2
  digitalWrite(led9, HIGH); // Hijau Kanan
  digitalWrite(led4, HIGH); // Merah Kiri
  digitalWrite(led1, HIGH); // Merah Atas
  delay(5000);
  digitalWrite(led9, LOW);  // Hijau Kanan
  digitalWrite(led4, LOW);  // Merah Kiri

  digitalWrite(led8, HIGH); // Kuning Kanan
  digitalWrite(led5, HIGH); // Kuning Kiri
  delay(800);
  digitalWrite(led8, LOW);  // Kuning Kanan
  digitalWrite(led5, LOW);  // Kuning Kiri

  // KONDISI 3
  digitalWrite(led6, HIGH); // Hijau Kiri
  digitalWrite(led7, HIGH); // Merah Kanan
  digitalWrite(led1, HIGH); // Merah bawah
  delay(5000);
  digitalWrite(led6, LOW);  // Hijau Kiri
  digitalWrite(led1, LOW);  // Merah Atas

  digitalWrite(led2, HIGH); // Kuning bawah
  digitalWrite(led5, HIGH); // Kuning Kiri
  digitalWrite(led8, HIGH); // Kuning Kanan
  delay(800);
  digitalWrite(led2, LOW);  // Kuning bawah
  digitalWrite(led5, LOW);  // Kuning Kiri
}