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

时间:2019-10-27 06:53:16

标签: c matrix arduino arduino-ide arduino-esp8266

先生,我正在使用带有3 * 3按钮的3 * 3 led -matrix,我需要一个程序来点亮左上方的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();

  }
}

1 个答案:

答案 0 :(得分:0)

您需要检查用户是否仅按了预期的键。
为此,您需要跟踪期望使用哪个密钥并进行检查。

首先使用初始期望的键设置全局变量。

char expected = '1';

然后将您的switch语句置于条件中,仅在满足条件时执行。

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

然后,在每个开关盒中,将预期的密钥更新为下一个。

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