#include <Wire.h>
#include <LiquidCrystal I2C.h>
#include <Keypad.h>
// Definicja tablicy słów
char words[][10] = {
"hello",
"world",
"arduino",
"game",
"binary"
};
// Liczba słów w tablicy
int numWords = sizeof(words) / sizeof(words[0]);
// Indeks wylosowanego słowa
int randomIndex;
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
// Definicja klawiatury numerycznej
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Zmienna poziomu
int Level = 1;
void setup() {
lcd.begin(16, 2); // Inicjalizacja wyświetlacza LCD
lcd.print("Wpisz kod binarny");
randomSeed(analogRead(A0)); // Inicjalizacja generatora liczb losowych
randomIndex = random(numWords); // Wylosowanie indeksu słowa
lcd.setCursor(0, 1);
lcd.print(words[randomIndex]);
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') {
String input = "";
while (key != '*') {
key = keypad.getKey();
if (key != NO_KEY) {
input += key;
}
}
input.trim(); // Usunięcie zbędnych spacji
if (input.equals(convertToBinary(words[randomIndex]))) {
lcd.clear();
lcd.print("Gratulacje!");
delay(2000);
Level++; // Zwiększenie poziomu
} else {
lcd.clear();
lcd.print("Błąd. Spróbuj ponownie.");
delay(2000);
}
randomIndex = random(numWords); // Wylosowanie nowego indeksu słowa
lcd.clear();
lcd.print("Wpisz kod binarny");
lcd.setCursor(0, 1);
lcd.print(words[randomIndex]);
}
}
}
// Funkcja konwertująca słowo na kod binarny
String convertToBinary(const char* word) {
String binaryCode = "";
for (int i = 0; i < strlen(word); i++) {
binaryCode += String(word[i], BIN);
binaryCode += " ";
}
return binaryCode;
}