#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C LCD, ganti jika berbeda
const int buttonPin = 2; // Tombol push-button
const int potPin = A0; // Potensiometer
String text1 = "Pesan Pertama!";
String text2 = "Pesan Kedua!";
String currentText = ""; // Pesan yang aktif
int scrollDelay = 300; // Kecepatan scroll default (300 ms)
bool buttonState = false; // Menyimpan status tombol terakhir
void setup() {
lcd.begin(16, 2);
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Tekan Tombol!"); // Pesan awal
pinMode(buttonPin, INPUT_PULLUP); // Tombol dengan pull-up internal
delay(1000);
}
void loop() {
// Membaca nilai potensiometer untuk mengatur kecepatan scroll
int potValue = analogRead(potPin); // Membaca nilai dari A0 (0-1023)
scrollDelay = map(potValue, 0, 1023, 100, 500); // Memetakan nilai potensiometer ke range (100 ms hingga 500 ms)
// Membaca status tombol
if (digitalRead(buttonPin) == LOW) { // Jika tombol ditekan
if (!buttonState) { // Tombol baru saja ditekan
if (currentText == text1) {
currentText = text2; // Beralih ke Pesan Kedua
} else {
currentText = text1; // Beralih ke Pesan Pertama
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(currentText); // Menampilkan teks saat ini
}
buttonState = true; // Set status tombol
} else {
buttonState = false; // Reset status tombol jika dilepas
}
// Scroll teks jika lebih dari 16 karakter
if (currentText.length() > 0) {
int len = currentText.length();
for (int i = 0; i <= len; i++) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(currentText.substring(i, i + 16)); // Menampilkan 16 karakter dari posisi saat ini
delay(scrollDelay); // Delay untuk efek scroll
}
}
}