#include <LiquidCrystal.h>

// Initialize the LCD with the appropriate pins
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

// Define pins for the ultrasonic sensor
const int trigPin = 5;
const int echoPin = 6;

// Define variables for distance calculation
float duration;
float distance;

// Define pins for the LEDs
const int greenLedPin = 2;
const int yellowLedPin = 3;
const int redLedPin = 4;

// Define pin for the buzzer
const int buzzerPin = 13;

// Function to show the startup message on the LCD
void showStartupMessage() {
  lcd.setCursor(4, 0);
    lcd.print("Merhaba!");
      delay(1000);

        lcd.setCursor(0, 1);
          String message = "MesafeOlcer v1.0";
            for (byte i = 0; i < message.length(); i++) {
                lcd.print(message[i]);
                    delay(100);
                      }
                        delay(500);
                        }

                        void setup() {
                          // Initialize the LCD
                            lcd.begin(16, 2);
                              showStartupMessage();

                                // Set up the ultrasonic sensor pins
                                  pinMode(trigPin, OUTPUT);
                                    pinMode(echoPin, INPUT);

                                      // Set up the LED and buzzer pins
                                        pinMode(greenLedPin, OUTPUT);
                                          pinMode(yellowLedPin, OUTPUT);
                                            pinMode(redLedPin, OUTPUT);
                                              pinMode(buzzerPin, OUTPUT);

                                                // Start serial communication
                                                  Serial.begin(9600);
                                                  }

                                                  void loop() {
                                                    // Clear the trigPin
                                                      digitalWrite(trigPin, LOW);
                                                        delayMicroseconds(2);

                                                          // Trigger the ultrasonic sensor
                                                            digitalWrite(trigPin, HIGH);
                                                              delayMicroseconds(10); // Adjusted to the standard 10us
                                                                digitalWrite(trigPin, LOW);

                                                                  // Measure the duration of the echo
                                                                    duration = pulseIn(echoPin, HIGH);

                                                                      // Calculate the distance in cm
                                                                        distance = duration * 0.034 / 2;

                                                                          // Print the distance to the Serial Monitor
                                                                            Serial.print("Mesafe: ");
                                                                              Serial.print(distance);
                                                                                Serial.println(" cm");

                                                                                  // Update the LCD and LEDs based on the distance
                                                                                    if (distance < 20) {
                                                                                        digitalWrite(greenLedPin, LOW);
                                                                                            digitalWrite(yellowLedPin, LOW);
                                                                                                digitalWrite(redLedPin, HIGH);
                                                                                                    tone(buzzerPin, 1000); // High frequency for close distance
                                                                                                        lcd.clear();
                                                                                                            lcd.setCursor(0, 0);
                                                                                                                lcd.print("DUR");
                                                                                                                  } else if (distance >= 20 && distance < 50) {
                                                                                                                      digitalWrite(greenLedPin, LOW);
                                                                                                                          digitalWrite(yellowLedPin, HIGH);
                                                                                                                              digitalWrite(redLedPin, LOW);
                                                                                                                                  tone(buzzerPin, 500); // Medium frequency for medium distance
                                                                                                                                      lcd.clear();
                                                                                                                                          lcd.setCursor(0, 0);
                                                                                                                                              lcd.print("KAPALI");
                                                                                                                                                } else if (distance >= 50) {
                                                                                                                                                    digitalWrite(greenLedPin, HIGH);
                                                                                                                                                        digitalWrite(yellowLedPin, LOW);
                                                                                                                                                            digitalWrite(redLedPin, LOW);
                                                                                                                                                                tone(buzzerPin, 300); // Low frequency for far distance
                                                                                                                                                                    lcd.clear();
                                                                                                                                                                        lcd.setCursor(0, 0);
                                                                                                                                                                            lcd.print("SERBEST");
                                                                                                                                                                              }

                                                                                                                                                                                // Print the distance on the LCD
                                                                                                                                                                                  lcd.setCursor(0, 1);
                                                                                                                                                                                    lcd.print(distance);
                                                                                                                                                                                      lcd.print(" cm");

                                                                                                                                                                                        // Stop the buzzer after a short delay
                                                                                                                                                                                          delay(500);
                                                                                                                                                                                            noTone(buzzerPin);

                                                                                                                                                                                              // Small delay before the next measurement
                                                                                                                                                                                                delay(100);
                                                                                                                                                                                                }