I want two Led's ( Led 1 and Led 3 ) in a 5*5 Led - Matrix to light up and when the corresponding buttons in the 5*5 push-button are pressed the Led's turns off and another two Led's ( Led 3 and Led 5 ) should light up and when the corresponding buttons are pressed the Led's turns off and another two led's ( Led 5 and Led 11 ) should light up and the process continues for next set of Led's ( Led 11 and Led 13 ) and the next set is ( Led 13 and Led 15 ) and the next set is ( Led 15 and Led 17 ) and ( Led 17 and Led 21 ) and (Led 21 and Led 23 ) and ( Led 23 and Led 25 ).
The code I have written is :
/* 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();
}
}
Expected Result: The user can press button 1 or button 3 in the first step if he presses button 1 the led-1 should go off and after pressing button 3 the led-3 should go off and another set of LEDs (Led-3 and Led-5) should glow.
NOTE: The user has the right to press button-3 first and then button-1 then also first led-3 should go off and then led-1 should go off and then the next set of LEDs can glow.
Actual Result: If I press button 1 both led-1 and led-3 goes off and also when I press Button-3 some random led's start blinking.