0
I'm developing a memory game. The code is written in .C and will run on an arduino. Within the code, randomized numbers from 8 to 14 must be generated. The number of rounds will determine the number needed to randomize the number. For example, in round 1: randomize 1 number and store it in the variable correct_order[] = {number_random_1}. round 2: randomize 2 numbers and store them in the variable correct_order[] = {number_random_1, number_random_2}. Between the intervals, wait for the user to press one of the buttons and store it in the variable selected_order[] = [number_pin]. The number of times the user has to press will also depend on the number of which round they are in. If the values in a loop, selected_order[index] == correct_order[index], are true, it should go to the next round. Otherwise, the program should warn that the game is over, perhaps in a Serial.println("message").
I managed to do the randomization process by rounds, however, I can't do the checks on the two variables (array), correct_order and selected_order.
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd_1(32,16,2);
const int ledPins[] = {13, 12, 11, 10, 9, 8};
const int buttonsPins[] = {2, 3, 4, 5, 6, 7};
const int numLeds = 6;
void setup() {
lcd_1.init();
lcd_1.setCursor(0,0);
lcd_1.backlight();
lcd_1.display();
Serial.begin(9600);
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(buttonsPins[i], INPUT); // Enables the internal pull-up resistor on the button pins
}
}
int correct_order[6];
int selected_order[6];
bool button_pressed_0 = false; // Variable to track whether button 1 has been pressed
bool button_pressed_1 = false; // Variable to track whether button 2 has been pressed
bool button_pressed_2 = false;
bool button_pressed_3 = false;
bool button_pressed_4 = false;
bool button_pressed_5 = false;
void reset(bool* button[], int quantity) {
// sets the initial conditions only for the specified buttons
for (int i = 0; i < quantity; i++) {
*(button[i]) = false;
}
}
int rounds = 1;
void loop() {
Serial.print("\n START GAME \n");
for(int i = 0; i<rounds;i++){
correct_order[i] = random(8, 14);
lcd_1.setCursor(0, 0);
lcd_1.print("rounds: ");
lcd_1.setCursor(0, 1);
lcd_1.print(rounds);
delay(1000);
lcd_1.clear();
Serial.print("\n variable rounds: ");
Serial.print(rounds);
for (int led = 0; led < rounds; led++) {
Serial.print("\n variable led (indice): ");
Serial.print(led);
Serial.print("\n pin: ");
Serial.print(correct_order[led]);
digitalWrite(correct_order[led],HIGH);
delay(1000);
digitalWrite(correct_order[led],LOW);
delay(1000);
}
int x = 0;
// do(rounds++;)while{x<rounds};
}
Serial.println("\n ------- THE GAME IS OVER -----------");
// correct_order - selected_order
// pin (led) - pin (button) + cts(normalizacao)
// 13 - 2 + 11 = 13
// 12 - 3 + 9 = 12
// 11 - 4 + 7 = 11
// 10 - 5 + 5 = 10
// 9 - 6 + 3 = 9
// 8 - 7 + 2 = 8
rounds = 1;
bool* button[] = {&button_pressed_0, &button_pressed_1, &button_pressed_2, &button_pressed_3, &button_pressed_4, &button_pressed_5};
reset(button, 6); // This will reset only the first 6 buttons
}
In the comment // do(rounds++;)while{x<rounds}; , I tried to do a Do While to wait for the user to press the buttons and store the pin number + constant (to be equal to the pin number in the correct order), but it's giving me a problem, because it has 5 if(){} conditions, and it's like a check for each round, i.e. in round 3, it only stores one value.
do {
if(digitalRead(buttonsPins[0]) == HIGH && !button_pressed_0) {
Serial.println("\n pressed");
Serial.print("index: ");
Serial.print(x);
Serial.print("\n Pin: ");
Serial.print(buttonsPins[0]);
selected_order[x] = buttonsPins[0] + 11;
button_pressed_0= true;
x++;
}
if(digitalRead(botoesPinos[1]) == HIGH && !button_pressed_1) {
Serial.println("\n pressed");
Serial.print("index: ");
Serial.print(x);
Serial.print("\n Pin: ");
Serial.print(buttonsPins[1]);
selected_order[x] = buttonsPins[1] + 9;
button_pressed_1= true;
x++;
}
if(digitalRead(botoesPinos[2]) == HIGH && !button_pressed_2) {
Serial.println("\n pressed");
Serial.print("index: ");
Serial.print(x);
Serial.print("\n Pin: ");
Serial.print(buttonsPins[2]);
selected_order[x] = buttonsPins[2] + 7;
button_pressed_2= true;
x++;
}
if(digitalRead(botoesPinos[3]) == HIGH && !button_pressed_3) {
Serial.println("\n pressed");
Serial.print("index: ");
Serial.print(x);
Serial.print("\n Pin: ");
Serial.print(buttonsPins[3]);
selected_order[x] = buttonsPins[3] + 5;
button_pressed_3= true;
x++;
}
if(digitalRead(buttonsPins[4]) == HIGH && !button_pressed_4) {
Serial.println("\n pressed");
Serial.print("index: ");
Serial.print(x);
Serial.print("\n Pin: ");
Serial.print(buttonsPins[4]);
selected_order[x] = buttonsPins[4] + 3;
button_pressed_4= true;
x++;
}
if(digitalRead(buttonsPins[5]) == HIGH && !button_pressed_5) {
Serial.println("\n pressed");
Serial.print("index: ");
Serial.print(x);
Serial.print("\n Pin: ");
Serial.print(buttonsPins[5]);
selected_order[x] = buttonsPins[5] + 1;
button_pressed_5= true;
x++;
}
} while (x < rounds);
OUTPUT
START GAME
variavel rodadas: 1
variavel led (index): 0
pine: 9
pressionado
index: 0
pine: 6
variavel rodadas: 2
variavel led (index): 0
pine: 9
variavel led (index): 1
pine: 9
variavel rodadas: 3
variavel led (index): 0
pine: 9
variavel led (index): 1
pine: 9
link to tinkcard: Login | Tinkercad