#include <LiquidCrystal.h>
// Inisialisasi pin untuk LCD: RS, EN, D4, D5, D6, D7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int buzzerPin = 8; // Pin untuk buzzer
// Tabel sandi morse sederhana
String morseCode[] = {
".-", //
"-...", //
"-.-.", //
"-..", //
".", //
"..-.", //
"--.", //
"....", //
"..", //
".---", //
"-.-", //
".-..", //
"--", //
"-.", //
"---", //
".--.", //
"--.-", //
".-.", //
"...", //
"-", //
"..-", //
"...-", //
".--", //
"-..-", //
"-.--", //
"--..", //
};
// Kalimat yang akan dikonversi ke Morse
String text = "AKU SUKA BERMAIN BOLA";
// Durasi dalam milidetik untuk titik dan garis
const int dotDuration = 1000; // 1 detik untuk titik
const int dashDuration = 3000; // 3 detik untuk garis
const int symbolPause = 1000; // Jeda antar simbol
const int letterPause = 3000; // Jeda antar huruf
const int wordPause = 5000; // Jeda antar kata (5 detik)
void setup() {
// Inisialisasi LCD 16x2
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Morse Code:");
pinMode(buzzerPin, OUTPUT); // Setel pin buzzer sebagai OUTPUT
delay(2000); // Jeda sebelum menampilkan morse
lcd.clear();
}
void loop() {
// Loop untuk setiap karakter di dalam string
for (int i = 0; i < text.length(); i++) {
char c = text[i];
// Jika karakter adalah spasi, tambahkan jeda antar kata (1 detik)
if (c == ' ') {
lcd.clear();
delay(wordPause); // Jeda antar kata 1 detik
continue;
}
// Cek apakah karakter valid (A-Z)
if (c >= 'A' && c <= 'Z') {
int index = c - 'A';
String morse = morseCode[index];
lcd.setCursor(0, 0);
lcd.print(c); // Tampilkan huruf di LCD
lcd.setCursor(0, 1);
lcd.print(morse); // Tampilkan kode morse di LCD
// Kirim morse
sendMorse(morse);
delay(letterPause); // Jeda antar huruf
}
}
delay(300); // Jeda sebelum mengulang teks
lcd.clear(); // Hapus layar
}
void sendMorse(String morse) {
for (int i = 0; i < morse.length(); i++) {
if (morse[i] == '.') {
tone(buzzerPin, 1000); // Nyalakan buzzer pada 1000 Hz
delay(dotDuration); // Jeda untuk titik
noTone(buzzerPin); // Matikan buzzer
} else if (morse[i] == '-') {
tone(buzzerPin, 1000); // Nyalakan buzzer pada 1000 Hz
delay(dashDuration); // Jeda untuk garis
noTone(buzzerPin); // Matikan buzzer
}
delay(symbolPause); // Jeda antar simbol
}
}