/*Smart Water Pump*/

#define TRIGGER_PIN 3
#define ECHO_PIN 2
#define RED_LED_PIN 9
#define YELLOW_LED_PIN 10
#define BLUE_LED_PIN 11
#define GREEN_LED_PIN 12

long duration;
int distance;

void kedip(bool logika1, bool logika2, int jeda){
  digitalWrite(YELLOW_LED_PIN,logika1);
  delay(jeda);
  digitalWrite(YELLOW_LED_PIN,logika2);
  delay(jeda);
}

void setup() {
  pinMode(TRIGGER_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(YELLOW_LED_PIN, OUTPUT);
  pinMode(BLUE_LED_PIN, OUTPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);
  digitalWrite(RED_LED_PIN, LOW);
  digitalWrite(YELLOW_LED_PIN, LOW);
  digitalWrite(BLUE_LED_PIN, LOW);
  digitalWrite(GREEN_LED_PIN, LOW);
  Serial.begin(9600);
}

void loop() {
  // Mengirimkan sinyal ultrasonik
  digitalWrite(TRIGGER_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER_PIN, LOW);

  // Menerima echo dan menghitung jarak
  duration = pulseIn(ECHO_PIN, HIGH);
  distance = duration * 0.034 / 2;

  // Menyala atau mematikan LED berdasarkan jarak yang terdeteksi
  if (distance > 250) {
    digitalWrite(RED_LED_PIN, LOW);
    Serial.println("tidak ada truck");
  } 
  else if (distance > 100 && distance < 120) {
    long startTime = millis();
    while (millis() - startTime < 5000) {
      if (distance > 250) {
        return; // Keluar dari loop jika jarak kembali lebih dari 250cm
      }
      duration = pulseIn(ECHO_PIN, HIGH);
      distance = duration * 0.034 / 2;
    }
    digitalWrite(RED_LED_PIN, HIGH);
    Serial.println("truck A");
  } 
  else if (distance > 120 && distance < 130) {
    long startTime = millis();
    while (millis() - startTime < 5000) {
      if (distance > 250) {
        return; // Keluar dari loop jika jarak kembali lebih dari 250cm
      }
      duration = pulseIn(ECHO_PIN, HIGH);
      distance = duration * 0.034 / 2;
    }
    digitalWrite(RED_LED_PIN, HIGH);
    Serial.println("truck B");
  }
}
Ada Truck
Pump
Set Position
Water is full