//##########################################################################################################################################################################################################################
//### Initialize
//##########################################################################################################################################################################################################################
//MODULE 5 - KEYPADS
//#############################################################################################################
// Constants for pin assignments
const int buttonPins[] = {37, 39, 41, 43};
const int ledPins[] = {36, 38, 40, 42};
// Symbols for the decoding columns
char columns[6][7] = {
{'A', 'B', 'C', 'D', 'E', 'F', 'G'},
{'H', 'I', 'J', 'K', 'L', 'M', 'N'},
{'O', 'P', 'Q', 'R', 'S', 'T', 'U'},
{'V', 'W', 'X', 'Y', 'Z', '1', '2'},
{'3', '4', '5', '6', '7', '8', '9'},
{'0', '!', '@', '#', '$', '%', '^'}
};
char selectedSymbols[4];
char sortedSymbols[4];
int buttonToSymbol[4];
int inputCount = 0;
bool gameWon = false;
int selectedColumn;
//##########################################################################################################################################################################################################################
//### SETUP
//##########################################################################################################################################################################################################################
void setup() {
Serial.begin(9600);
//MODULE 5 - KEYPADS
// Initialize button and LED pins
for (int i = 0; i < 4; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW);
}
// Seed random number generator
randomSeed(analogRead(0));
// Start a new game
startNewGame();
}
//##########################################################################################################################################################################################################################
//### FUNCTIONS
//##########################################################################################################################################################################################################################
//#############################################################################################################
//### MODULE 5 - KEYPADS
//#############################################################################################################
// Function to start a new game
void startNewGame() {
// Select a random column
selectedColumn = random(0, 6);
// Select 4 random symbols from the selected column and assign to buttons
for (int i = 0; i < 4; i++) {
int symbolIndex;
bool symbolAssigned;
// Ensure unique symbols are assigned to buttons
do {
symbolAssigned = false;
symbolIndex = random(0, 7);
for (int j = 0; j < i; j++) {
if (selectedSymbols[j] == columns[selectedColumn][symbolIndex]) {
symbolAssigned = true;
break;
}
}
} while (symbolAssigned);
selectedSymbols[i] = columns[selectedColumn][symbolIndex];
buttonToSymbol[i] = symbolIndex;
}
// Display assigned symbols for debugging
for (int i = 0; i < 4; i++) {
Serial.print("Button ");
Serial.print(buttonPins[i]);
Serial.print(": ");
Serial.println(selectedSymbols[i]);
}
inputCount = 0;
gameWon = false;
sortSymbols();
}
// Function to find the index of a character in the columns array
int getIndex(char c) {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
if (columns[i][j] == c) {
return i * 7 + j;
}
}
}
return -1; // Character not found
}
// Function to sort the second array based on the order in the columns array
void sortSymbolsArray(char arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (getIndex(arr[i]) > getIndex(arr[j])) {
// Swap elements
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
void sortSymbols() {
for (int i = 0; i < 4; i++) {
sortedSymbols[i] = selectedSymbols[i];
}
// Sort the second array
sortSymbolsArray(sortedSymbols, 4);
Serial.print("After sorting: ");
for (int i = 0; i < 4; i++) {
Serial.print(sortedSymbols[i]);
Serial.print(' ');
}
Serial.println();
}
// Function to check button presses
void checkButtons() {
for (int i = 0; i < 4; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
delay(20); // Debounce delay
if (digitalRead(buttonPins[i]) == LOW) { // Check again after delay
digitalWrite(ledPins[i], HIGH);
Serial.println(selectedSymbols[i]);
Serial.println(sortedSymbols[inputCount]);
// Check if the button pressed matches the expected symbol in order
if (selectedSymbols[i] != sortedSymbols[inputCount]) {
Serial.println("You lose! Try again.");
gameWon = true;
}
inputCount++;
// Check if all buttons have been pressed correctly
if (inputCount == 4) {
Serial.println("You win!");
gameWon = true;
}
}
while (digitalRead(buttonPins[i]) == LOW); // Wait for button release
}
}
}
void resetGame() {
inputCount = 0;
gameWon = false;
startNewGame();
for (int i = 0; i < 4; i++) {
digitalWrite(ledPins[i], LOW);
}
}
//##########################################################################################################################################################################################################################
//### Loop
//##########################################################################################################################################################################################################################
void loop() {
//MODULE 5 - KEYPADS
//#############################################################################################################
if (!gameWon) {
checkButtons();
} else {
resetGame();
}
}