#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int redLedPin = 6;
const int yellowLedPins[] = { 8, 9, 10, 11, 12 };
bool buttonPressed1 = false;
bool buttonPressed2 = false;
bool buttonPressed3 = false;
bool buttonPressed4 = false;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const char* kota[] = { "Depok", "Klaten", "Bantul", "Sleman", "Solo" };
int indexkota = 0;
unsigned long previousMillis = 0;
const long interval = 2000;
const char* tulisan = "Terimakasih telah menggunakan layanan kami";
int indextulisan = 0;
bool runningText = false;
unsigned long previousMillisMessage = 0;
const long intervalMessage = 300;
void setup() {
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
pinMode(redLedPin, OUTPUT);
for (int i = 0; i < 5; i++) {
pinMode(yellowLedPins[i], OUTPUT);
}
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Kota Berikutnya:");
lcd.setCursor(0, 1);
lcd.print(kota[indexkota]);
}
void loop() {
unsigned long currentMillis = millis();
if (digitalRead(buttonPin1) == LOW && !buttonPressed1) {
Serial.println("Button 1 ditekan.");
buttonPressed1 = true;
runningText = true;
indextulisan = 0;
}
if (runningText) {
if (currentMillis - previousMillisMessage >= intervalMessage) {
previousMillisMessage = currentMillis;
lcd.clear();
lcd.setCursor(0, 0);
for (int i = 0; i < 16; i++) {
if (indextulisan + i < strlen(tulisan)) {
lcd.print(tulisan[indextulisan + i]);
}
}
indextulisan++;
if (indextulisan >= strlen(tulisan)) {
indextulisan = 0;
runningText = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Kota Berikutnya:");
lcd.setCursor(0, 1);
lcd.print(kota[indexkota]);
}
}
} else {
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
indexkota = (indexkota + 1) % 5;
lcd.setCursor(0, 0);
lcd.print("Kota Berikutnya:");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(kota[indexkota]);
}
}
if (digitalRead(buttonPin2) == LOW && !buttonPressed2) {
Serial.println("Button 2 ditekan.");
buttonPressed2 = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("STOP");
lcd.setCursor(0, 1);
lcd.print(" ");
for (int i = 0; i < 5; i++) {
digitalWrite(redLedPin, HIGH);
delay(500);
digitalWrite(redLedPin, LOW);
delay(500);
}
}
if (digitalRead(buttonPin3) == LOW && !buttonPressed3) {
Serial.println("Button 3 ditekan.");
buttonPressed3 = true;
for (int j = 0; j < 5; j++) {
for (int i = 4; i >= 0; i--) {
digitalWrite(yellowLedPins[i], HIGH);
delay(200);
digitalWrite(yellowLedPins[i], LOW);
}
}
}
if (digitalRead(buttonPin4) == LOW && !buttonPressed4) {
Serial.println("Button 4 ditekan.");
buttonPressed4 = true;
for (int j = 0; j < 5; j++) {
for (int i = 0; i < 5; i++) {
digitalWrite(yellowLedPins[i], HIGH);
delay(200);
digitalWrite(yellowLedPins[i], LOW);
}
}
}
if (digitalRead(buttonPin1) == HIGH) {
buttonPressed1 = false;
}
if (digitalRead(buttonPin2) == HIGH) {
buttonPressed2 = false;
}
if (digitalRead(buttonPin3) == HIGH) {
buttonPressed3 = false;
}
if (digitalRead(buttonPin4) == HIGH) {
buttonPressed4 = false;
}
}