我正在开发一个记忆游戏。该代码是用 .C 编写的,将在 arduino 上运行。 在代码中,必须生成 8 到 14 之间的随机数。 轮数将决定随机化数字所需的数量。例如,在第 1 轮中:随机化 1 个数字并将其存储在变量 Correct_order[] = {number_random_1} 中。第 2 轮:随机化 2 个数字并将它们存储在变量 Correct_order[] = {number_random_1, number_random_2} 中。 在间隔之间,等待用户按下其中一个按钮并将其存储在变量 selected_order[] = [number_pin] 中。用户按下的次数也取决于他们所在的轮次。如果循环中的值 selected_order[index] == Correct_order[index] 为 true,则应该进入下一轮。否则,程序应该警告游戏已经结束,可能是在 Serial.println("message") 中。
我设法按轮进行随机化过程,但是,我无法对两个变量(数组), Correct_order 和 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
}
在评论
// do(rounds++;)while{x<rounds};
中,我尝试执行“Do While”来等待用户按下按钮并存储引脚号+常量(以正确的顺序等于引脚号),但它给了我一个问题,因为它有 5 个 if(){}
条件,就像每一轮的检查,即在第 3 轮中,它只存储一个值。
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);
输出:
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
嵌入式系统倾向于有效地存储数据,因此没有
int
数组,而是位字段。假设您正在处理 MCU 的 GPIO 端口而不是业余爱好者板上的某些连接器,那么这将直接对应于 PORTn
寄存器(在 AVR MCU 上)。
例如,假设 LED 位于 PORTA 引脚 1、2、3 上:
#define PORTA_PIN1 (1u << 0)
#define PORTA_PIN2 (1u << 1)
#define PORTA_PIN3 (1u << 2)
...
const uint8_t ledPins = {PORTA_PIN1, PORTA_PIN2, PORTA_PIN3};
...
// Activate all LEDs at the same time, no loops necessary:
PORTA = ledPins;
类似地,您不会使用
bool
数组、按钮数组等,而是使用位字段。我们正在谈论的是仍在生产中的最糟糕的古董 MCU 之一;我们没有海量的 RAM 和闪存。
所以:
uint8_t button_pressed = 0;
...
if(PORTX & PORTX_PIN1) // wherever the button happens to be located
button_pressed |= PORTX_PIN1;
else
button_pressed &= ~PORTX_PIN1;
(但也可以消除按钮弹跳并提供拉电阻。但那是另一个故事了。)
一般来说,嵌入式系统仅使用
stdint.h
中的类型,因为它们是固定大小且可移植的。像 int
等朴素的默认类型永远不会被使用。它不仅是 16 位,而且还是有符号的,这通常是我们在进行硬件相关编程时要避免的事情,因为有符号变量不能与按位算术很好地混合。