-
Notifications
You must be signed in to change notification settings - Fork 0
/
ayat-guessing-game.ino
380 lines (336 loc) · 8.58 KB
/
ayat-guessing-game.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <DFRobotDFPlayerMini.h>
#include "track.h" // Pastikan file ini ada dan berisi definisi nama track
DFRobotDFPlayerMini dfPlayer;
LiquidCrystal_I2C lcd(0x22, 20, 4);
// Konfigurasi Keypad
const byte ROWS = 3;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'Y', 'N', '0', '1'},
{'2', '3', '4', '5'},
{'6', '7', '8', '9'}
};
byte rowPins[ROWS] = {5, 4, 3};
byte colPins[COLS] = {9, 8, 7, 6};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Konfigurasi pin
const int ledPin = 50;
const int buzzerPin = 51;
// Variabel
int skor = 5;
int nomorFile;
unsigned long waktuMulaiBuzzer = 0;
const unsigned long durasiCuitan = 500;
bool audioSedangDiputar = false;
unsigned long waktuPesanMulai = 0;
const unsigned long durasiPesan = 2000;
bool pesanAktif = false;
bool sdCardStatus = false;
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(9600);
Serial1.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Periksa komponen
bool allComponentsOK = periksaKomponen();
if (allComponentsOK) {
sistemBerjalan();
tampilkanMenuUtama();
} else {
lcd.clear();
lcd.print("Pemeriksaan gagal");
lcd.setCursor(0, 1);
lcd.print("Periksa perangkat");
while (true); // Hentikan program
}
}
void loop() {
unsigned long currentMillis = millis();
// Handle buzzer timing
if (waktuMulaiBuzzer > 0 && currentMillis - waktuMulaiBuzzer >= durasiCuitan) {
digitalWrite(buzzerPin, LOW);
waktuMulaiBuzzer = 0;
}
// Check audio playback status
if (audioSedangDiputar && dfPlayer.available() && dfPlayer.readType() == DFPlayerPlayFinished) {
audioSedangDiputar = false;
tampilkanMenuUtama();
}
// Check keypad input
char key = keypad.getKey();
if (key) {
prosesInputKeypad(key);
}
// Handle message display duration
if (pesanAktif && currentMillis - waktuPesanMulai >= durasiPesan) {
pesanAktif = false;
tampilkanMenuUtama();
}
// Cek event dari DFPlayer
if (dfPlayer.available()) {
printDetail(dfPlayer.readType(), dfPlayer.read());
}
}
bool periksaKomponen() {
bool status = true;
// Periksa DFPlayer dan kartu SD
lcd.clear();
lcd.print("Memeriksa DFPlayer");
lcd.setCursor(0, 1);
lcd.print("dan kartu SD...");
if (!dfPlayer.begin(Serial1)) {
lcd.clear();
lcd.print("DFPlayer gagal!");
lcd.setCursor(0, 1);
lcd.print("Periksa koneksi");
status = false;
} else {
// Coba baca jumlah file di kartu SD
int jumlahFile = dfPlayer.readFileCounts();
if (jumlahFile == -1) {
lcd.clear();
lcd.print("Kartu SD gagal!");
lcd.setCursor(0, 1);
lcd.print("Periksa kartu SD");
status = false;
} else {
lcd.clear();
lcd.print("DFPlayer & SD OK");
lcd.setCursor(0, 1);
lcd.print("File: ");
lcd.print(jumlahFile);
sdCardStatus = true;
}
}
delay(2000);
// Periksa LCD (jika sampai di sini, LCD bekerja)
lcd.clear();
lcd.print("Memeriksa LCD...");
lcd.setCursor(0, 1);
lcd.print("LCD OK");
delay(1000);
// Periksa LED
lcd.clear();
lcd.print("Memeriksa LED...");
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
lcd.setCursor(0, 1);
lcd.print("LED OK");
delay(1000);
// Periksa Buzzer
lcd.clear();
lcd.print("Memeriksa Buzzer...");
tone(buzzerPin, 1000, 500);
lcd.setCursor(0, 1);
lcd.print("Buzzer OK");
delay(1000);
return status;
}
void sistemBerjalan() {
digitalWrite(ledPin, HIGH);
}
void tampilkanMenuUtama() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Selamat Datang!");
lcd.setCursor(0, 1);
lcd.print("Pilih Permainan:");
lcd.setCursor(0, 2);
lcd.print("1: Dengar Ayat");
lcd.setCursor(0, 3);
lcd.print("2: Tebak Ayat");
}
void prosesInputKeypad(char key) {
if (!audioSedangDiputar && !pesanAktif) {
if (key == '1') {
mintaKonfirmasi("Yakin mendengar?", saatYaMendengarkan, saatTidakMendengarkan);
} else if (key == '2') {
mintaKonfirmasi("Yakin menebak?", saatYaMenebak, saatTidakMenebak);
}
}
}
void mintaKonfirmasi(const char* pesan, void (*callbackYa)(), void (*callbackTidak)()) {
lcd.clear();
lcd.print(pesan);
lcd.setCursor(0, 1);
lcd.print("Y: Ya N: Tidak");
while (true) {
char key = keypad.getKey();
if (key == 'Y') {
callbackYa();
break;
}
if (key == 'N') {
callbackTidak();
break;
}
}
}
void modeMendengarkan() {
lcd.clear();
lcd.print("Masukkan nomor:");
char input[4] = "";
dapatkanInputKeypad(input, sizeof(input));
nomorFile = atoi(input);
mintaKonfirmasi("Putar file ini?", saatPutarFile, tampilkanMenuUtama);
}
void modeMenebak() {
nomorFile = random(1, 287);
playAudioFile(nomorFile);
audioSedangDiputar = true;
lcd.setCursor(15, 0);
lcd.print("Skor:");
lcd.print(skor);
mintaKonfirmasi("Tebak nomor?", saatTebakanJawaban, tampilkanMenuUtama);
}
void dapatkanInputKeypad(char* bufferInput, size_t ukuranBuffer) {
size_t indeks = 0;
while (indeks < ukuranBuffer - 1) {
char key = keypad.getKey();
if (key) {
if (key >= '0' && key <= '9') {
bufferInput[indeks++] = key;
lcd.setCursor(indeks, 1);
lcd.print(key);
}
}
}
bufferInput[indeks] = '\0';
}
void playAudioFile(int trackNumber) {
dfPlayer.play(trackNumber);
audioSedangDiputar = true;
lcd.clear();
lcd.print("Memutar file ");
lcd.setCursor(0, 1);
lcd.print(trackNames[trackNumber - 1]); // Tampilkan nama track
}
void saatYaMendengarkan() {
modeMendengarkan();
}
void saatTidakMendengarkan() {
tampilkanMenuUtama();
}
void saatYaMenebak() {
modeMenebak();
}
void saatTidakMenebak() {
tampilkanMenuUtama();
}
void saatPutarFile() {
playAudioFile(nomorFile);
}
void saatTebakanJawaban() {
lcd.clear();
lcd.print("Masukkan tebakan:");
char input[4] = "";
dapatkanInputKeypad(input, sizeof(input));
int tebakan = atoi(input);
if (tebakan == nomorFile) {
skor++;
dfPlayer.play(287); // Putar suara "jawaban benar"
tampilkanPesan(("Benar! Skor: " + String(skor)).c_str());
} else {
skor--;
dfPlayer.play(286); // Putar suara "jawaban salah"
jawabanSalah();
tampilkanPesan(("Salah! Skor: " + String(skor)).c_str());
}
if (skor == 0) {
tampilkanPesan("Permainan Selesai");
dfPlayer.play(288); // Putar suara "game over"
} else if (skor == 10) {
tampilkanPesan("Anda Menang!");
dfPlayer.play(289); // Putar suara "menang"
} else {
mintaKonfirmasi("Main lagi?", saatYaMendengarkan, tampilkanMenuUtama);
}
}
void jawabanSalah() {
digitalWrite(buzzerPin, HIGH);
waktuMulaiBuzzer = millis();
}
void tampilkanPesan(const char* pesan) {
lcd.clear();
lcd.print(pesan);
waktuPesanMulai = millis();
pesanAktif = true;
}
void prosesKartuSDEvent(uint8_t event) {
switch(event) {
case DFPlayerCardInserted:
sdCardStatus = true;
lcd.clear();
lcd.print("Kartu SD dimasukkan");
break;
case DFPlayerCardRemoved:
sdCardStatus = false;
lcd.clear();
lcd.print("Kartu SD dicabut");
break;
}
delay(2000);
tampilkanMenuUtama();
}
void printDetail(uint8_t type, int value) {
switch (type) {
case TimeOut:
Serial.println(F("Time Out!"));
break;
case WrongStack:
Serial.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
prosesKartuSDEvent(DFPlayerCardInserted);
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
prosesKartuSDEvent(DFPlayerCardRemoved);
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value) {
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}