在 LED 矩阵和按钮中控制开和关

Controlled On and Off in a Led-Matrix And Push-Button

先生,我正在使用带有 3*3 按钮的 3*3 LED 矩阵,我需要一个程序来点亮左上角的 LED 并等待用户的响应。如果按下相应的按钮,则 LED 灯熄灭,紧邻的 LED 灯亮起,过程继续。此代码适用于此,问题是当用户按下第一个按钮时,第一个 LED 熄灭,第二个 LED 亮起,当按下第二个按钮时,第二个 LED 熄灭,第三个 LED 熄灭,但是当用户错误地触摸时在过程中间再次按下第二个按钮,随机 LED 开始发光,这不应该发生代码应该等到按下正确的按钮并且过程继续,任何人都可以帮助我处理代码。谢谢

#include <Keypad.h>

int led_rows[]={ 2 , 3 , 4 };
int led_cols[]={ 5 , 6 , 7 };

int led_matriz[3][3]= {     
    {0, 0, 0}, 
    {0, 0, 0},  
    {0, 0, 0}, };

const byte rows = 3; 
const byte cols = 3; 

char keys[rows][cols] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
};

byte rowPins[rows] = {10,9,8}; 
byte colPins[cols] = {13,12,11}; 

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );

void setup(){
  Serial.begin(9600);

  for(int i=0;i<3;i++){
  pinMode(led_cols[i], OUTPUT);
  digitalWrite (led_cols[i], HIGH);
  }

  for(int i=0;i<3;i++){
  pinMode(led_rows[i], OUTPUT);
  digitalWrite (led_rows[i], LOW);
  }
  Led1_On();
}

void Led1_On(){
  digitalWrite (led_rows[0], HIGH);
  digitalWrite (led_cols[0], LOW);
}

void Led1_Off(){
  digitalWrite (led_rows[0], LOW);
  digitalWrite (led_cols[0], HIGH);
}

void Led2_On(){
  digitalWrite (led_rows[0], HIGH);
  digitalWrite (led_cols[1], LOW);
}

void Led2_Off(){
  digitalWrite (led_rows[0], LOW);
  digitalWrite (led_cols[1], HIGH);
}

void Led3_On(){
  digitalWrite (led_rows[0], HIGH);
  digitalWrite (led_cols[2], LOW);
}

void Led3_Off(){
  digitalWrite (led_rows[0], LOW);
  digitalWrite (led_cols[2], HIGH);
}

void Led4_On(){
  digitalWrite (led_rows[1], HIGH);
  digitalWrite (led_cols[0], LOW);
}

void Led4_Off(){
  digitalWrite (led_rows[1], LOW);
  digitalWrite (led_cols[0], HIGH);
}

void Led5_On(){
  digitalWrite (led_rows[1], HIGH);
  digitalWrite (led_cols[1], LOW);
}

void Led5_Off(){
  digitalWrite (led_rows[1], LOW);
  digitalWrite (led_cols[1], HIGH);
}

void Led6_On(){
  digitalWrite (led_rows[1], HIGH);
  digitalWrite (led_cols[2], LOW);
}

void Led6_Off(){
  digitalWrite (led_rows[1], LOW);
  digitalWrite (led_cols[2], HIGH);
}

void Led7_On(){
  digitalWrite (led_rows[2], HIGH);
  digitalWrite (led_cols[0], LOW);
}

void Led7_Off(){
  digitalWrite (led_rows[2], LOW);
  digitalWrite (led_cols[0], HIGH);
}

void Led8_On(){
  digitalWrite (led_rows[2], HIGH);
  digitalWrite (led_cols[1], LOW);
}

void Led8_Off(){
  digitalWrite (led_rows[2], LOW);
  digitalWrite (led_cols[1], HIGH);
}

void Led9_On(){
  digitalWrite (led_rows[2], HIGH);
  digitalWrite (led_cols[2], LOW);
}

void Led9_Off(){
  digitalWrite (led_rows[2], LOW);
  digitalWrite (led_cols[2], HIGH);
}

void loop(){
  char key = keypad.getKey();
  //Led1_On();

  switch(key)
  {
    case '1' :
    Led1_Off();
    Led2_On();
    break;

    case '2' :
    Led2_Off();
    Led3_On();
    break;

    case '3':
    Led3_Off();
    Led4_On();
    break;

    case '4':
    Led4_Off();
    Led5_On();
    break;

    case '5' :
    Led5_Off();
    Led6_On();
    break;

    case '6' :
    Led6_Off();
    Led7_On();
    break;

    case '7' :
    Led7_Off();
    Led8_On();
    break;

    case '8' :
    Led8_Off();
    Led9_On();
    break;

    case '9' :
    Led9_Off();

  }
}

您需要检查用户是否只按下了预期的键。
为此,您需要跟踪预期的密钥并进行检查。

首先使用最初预期的密钥设置一个全局变量。

char expected = '1';

然后将你的switch语句放入一个条件语句中,只有满足条件才执行。

if (key==expected)
{
    switch(key)
    {

然后,在每个 switch case 中,将预期的键更新为下一个。

        case '1' :
            expected = '2';
            Led1_Off();
            Led2_On();
            break;