gpt4 book ai didi

通过 LED 矩阵和按钮控制开和关

转载 作者:行者123 更新时间:2023-11-30 16:11:33 30 4
gpt4 key购买 nike

先生,我正在使用带有 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();

}
}

最佳答案

您需要检查用户是否只按下了预期的键。
为此,您需要跟踪并检查需要哪个 key 。

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

char expected = '1';

然后将 switch 语句放入条件语句中,仅在满足条件时执行。

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

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

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

关于通过 LED 矩阵和按钮控制开和关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58577178/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com