我希望5 * 5 Led-矩阵中的两个Led(Led 1和Led 3)点亮,并且当按下5 * 5按钮中的相应按钮时,LED的指示灯熄灭,另外两个Led(Led 3和LED 5)应该亮起 当按下相应的按钮时,LED指示灯熄灭,另外两个LED指示灯(LED 5和LED 11)应点亮,并且该过程继续进行下一组LED指示灯(LED 11和LED 13),下一组LED指示灯(LED 13和LED 13)。 Led 15)和下一组是(Led 15和Led 17)和(Led 17和Led 21)和(Led 21和Led 23)和(Led 23和Led 25)。
我写的代码是:
/* If both "first_key_pressed" and "second_key_pressed", we will make the next two LEDs glow and set these two values to zero again
Initially, LEDs 1 and 3 are glowing, so it is declared that the Led 1 is currently first_in_position and Led 3 is currently second_in_position.
Once 1 and 3 are pressed, we will change this two values to first_in_position = '3' and second_in_position = '5'
The method set_next_expected_leds does this job.
*/
#include <Keypad.h>
const byte rows = 5;
const byte cols = 5;
char keys[rows][cols] = {
{'1','2','3','4','5'},
{'6','7','8','9','A'},
{'B','C','D','E','F'},
{'G','H','I','J','K'},
{'L','M','N','O','P'}
};
byte rowPins[rows] = {40, 39 , 38 , 37 , 36 };
byte colPins[cols] = { 35 , 34 ,33 , 32 , 31 };
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );
int Led_Rows[] = { 2 , 3 , 4 ,5 ,6 } ;
int Led_Columns[] = { 7 , 8 , 9 , 10 , 11 };
int Loop_Count = 5 ;
int first_key_pressed = 0;
int second_key_pressed = 0;
char first_in_position = '1';
char second_in_position = '3';
int Wait_Time = 1000 ;
int i = 0 ;
int j = 0 ;
int x = 0 ;
int y = 0 ;
int c = 0 ;
void setup() {
Serial.begin(9600);
for(int i=0;i<5;i++){
pinMode(Led_Rows[i], OUTPUT);
digitalWrite(Led_Rows[i],LOW);
pinMode(Led_Columns[i], OUTPUT);
digitalWrite(Led_Columns[i],HIGH);
}
Led_Glow(0,0);
Led_Glow(0,2);
}
void All_On(){
for( i = 0 ; i < Loop_Count ; i++ ){
digitalWrite(Led_Rows[i],HIGH);
while( j < Loop_Count ){
digitalWrite(Led_Columns[j],LOW);
j++ ;
}
}
j = 0 ;
}
void All_Off(){
for( i = 0 ; i < Loop_Count ; i++ ){
digitalWrite(Led_Rows[i],LOW);
digitalWrite(Led_Columns[i],HIGH);
}
}
void Led_Glow( int a , int b ){
digitalWrite(Led_Rows[a],HIGH);
digitalWrite(Led_Columns[b],LOW);
}
void Led_Dim( int a ,int b ){
digitalWrite(Led_Rows[a],LOW);
digitalWrite(Led_Columns[b],HIGH);
}
void check_for_position(int c) {
if (first_in_position == c) {
first_key_pressed = 1;
} else if (second_in_position == c) {
second_key_pressed = 1;
}
}
void set_next_expected_leds() {
if (first_in_position == '1') {
first_in_position = '3';
second_in_position = '5';
Led_Glow(0,2);
Led_Glow(0,4);
} else if (first_in_position == '3') {
first_in_position = '5';
second_in_position = 'B';
Led_Glow(0,4);
Led_Glow(2,0);
} else if (first_in_position == '5') {
first_in_position = 'B';
second_in_position = 'D';
Led_Glow(2,0);
Led_Glow(2,2);
} else if (first_in_position == 'B') {
first_in_position = 'D';
second_in_position = 'F';
Led_Glow(2,2);
Led_Glow(2,4);
} else if (first_in_position == 'D') {
first_in_position = 'F';
second_in_position = 'L';
Led_Glow(2,4);
Led_Glow(4,0);
} else if (first_in_position == 'F') {
first_in_position = 'L';
second_in_position = 'N';
Led_Glow(4,0);
Led_Glow(4,2);
} else if (first_in_position == 'L') {
first_in_position = 'N';
second_in_position = 'P';
Led_Glow(4,2);
Led_Glow(4,4);
} else if (first_in_position == 'N') {
} else if (first_in_position == 'P') {
}
first_key_pressed = 0;
second_key_pressed = 0;
}
void loop() {
char key = keypad.getKey();
switch (key) {
case '1':
check_for_position('1');
Led_Dim(0, 0);
break;
case '3':
check_for_position('3');
Led_Dim(0, 2);
break;
case '5':
check_for_position('5');
Led_Dim(0, 4);
break;
case 'B':
check_for_position('B');
Led_Dim(2, 0);
break;
case 'D':
check_for_position('D');
Led_Dim(2, 2);
break;
case 'F':
check_for_position('F');
Led_Dim(2, 4);
break;
case 'L':
check_for_position('L');
Led_Dim(4, 0);
break;
case 'N':
check_for_position('N');
Led_Dim(4, 2);
break;
case 'P':
check_for_position('P');
Led_Dim(4, 4);
}
if(first_key_pressed == 1 && second_key_pressed == 1) {
set_next_expected_leds();
}
}
预期结果:如果用户按下按钮1,则可以在第一步中按下按钮1或按钮3,LED-1应该熄灭,而在按下按钮3之后,LED-3应该熄灭,并且另一组LED(LED -3和Led-5)应该发光。
注意:用户有权先按下按钮3,然后再按下按钮1,然后再先按下led-3,然后再按下led-1,然后下一组LED才能发光。
实际结果:如果我按下按钮1,则led-1和led-3都会熄灭,并且当我按下Button-3时,一些随机的led也会开始闪烁。