#include <LiquidCrystal_I2C.h>
// Define the pins for the ultrasonic sensor
#define PIN_TRIG 12
#define PIN_ECHO 11
// Define the pins for the button and LEDs
const int buttonPin = 2;
const int merah = 10; // Red LED
const int kuning = 9; // Yellow LED
const int biru = 8; // Blue LED
const int btn = 7; // Button pin
// Variables to store the measured distance and echo time
float cm;
float temp;
// Initialize the LCD with I2C address 0x27 and size 20x4
LiquidCrystal_I2C lcd(0x27, 20, 4);
int displayState = 0; // Variable to track the current display state
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
// Start serial communication for debugging
Serial.begin(9600);
// Set the pin modes for the ultrasonic sensor
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
// Initialize the LCD and turn on the backlight
lcd.init();
lcd.backlight();
// Set the pin modes for the button and LEDs
pinMode(buttonPin, INPUT_PULLUP);
pinMode(merah, OUTPUT);
pinMode(kuning, OUTPUT);
pinMode(biru, OUTPUT);
pinMode(btn, INPUT_PULLUP); // Set button pin as input with pull-up resistor
}
void loop() {
// Read the state of the button and handle debouncing
int reading = digitalRead(btn);
if (reading == LOW && (millis() - lastDebounceTime) > debounceDelay) {
lastDebounceTime = millis();
displayState = (displayState + 1) % 3; // Cycle through display states (0, 1, 2)
}
// Trigger the ultrasonic sensor
digitalWrite(PIN_TRIG, LOW);
delayMicroseconds(2);
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10); // Ensure the TRIG pin is HIGH for 10 microseconds
digitalWrite(PIN_TRIG, LOW);
// Measure the duration of the echo signal
temp = float(pulseIn(PIN_ECHO, HIGH));
cm = (temp * 0.0343) / 2; // Convert the echo time to distance in cm
// Print the echo time and distance to the serial monitor
Serial.print("Echo = ");
Serial.print(temp);
Serial.print(", Distance = ");
Serial.print(cm);
Serial.println(" cm");
// Update the LCD based on the current display state
lcd.clear();
switch (displayState) {
case 0:
lcd.setCursor(0, 0);
lcd.print("Echo =");
lcd.print(temp);
lcd.setCursor(0, 1);
lcd.print("Distance=");
lcd.print(cm);
lcd.print(" cm");
break;
case 1:
lcd.setCursor(0, 0);
lcd.print("Distance=");
lcd.print(cm);
lcd.print(" cm");
lcd.setCursor(0, 1);
lcd.print("Button: ");
if (reading == LOW) {
lcd.print("ON");
} else {
lcd.print("OFF");
}
break;
case 2:
lcd.setCursor(0, 0);
lcd.print("Temp=");
lcd.print(temp);
lcd.print(" us");
lcd.setCursor(0, 1);
lcd.print("Press btn to toggle");
break;
}
// Light up LEDs based on the measured distance
if (cm <= 100) {
digitalWrite(merah, HIGH);
digitalWrite(kuning, LOW);
digitalWrite(biru, LOW);
} else if (cm <= 200) {
digitalWrite(merah, LOW);
digitalWrite(kuning, HIGH);
digitalWrite(biru, LOW);
} else {
digitalWrite(merah, HIGH);
digitalWrite(kuning, HIGH);
digitalWrite(biru, HIGH);
}
// Wait for 200 milliseconds before the next loop iteration
delay(200);
}