I want the Led's in a 5 * 5 Led-Matrix to light up in the shape of a square. If the corresponding buttons in the 5*5 Push-button matrix are pressed, the led turns off. The buttons are pressed in the clockwise direction starting from the first button on the top left side. I am using Arduino-Uno as programmer and Arduino-Mega2560 as target and the 5*5 Led-Matrix and 5 *5 push-Buttons matrix are connected to Arduino-Mega2560. I have the code for 5*5 Led-Matrix code which lights up in the shape of a square as separate code and 5*5 Push-Button Matrix as separate code, where to input the codes of 5*5 push-button matrix to control the 5*5 Led-Matrix, as discussed above. Thanks
The code for 5*5 Led-Matrix to light up in the shape of a square
#define MAXLEDS 5
int states[MAXLEDS][MAXLEDS] = {
{ 1, 1, 1 ,1 ,1 },
{ 1, 0, 0 ,0 ,1 },
{ 1, 0, 0, 0 ,1 },
{ 1, 0, 0, 0 ,1 },
{ 1, 1, 1, 1 ,1 },
};
int Led_Row_Pins[] = { 2 , 3 , 4 , 5 , 6 } ; // Anode pins are shorted in row_wise_manner
int Led_Column_Pins[] = {7 , 8 , 9 , 10 , 11 } ; // Column Pins are shorted in column_wise_manner
int Loop_Count = 5 ;
int i = 0 ;
int j = 0 ;
int state = 1 ;
void setup() {
for( i = 0 ; i < Loop_Count ; i++ ){ // Anode Pins are connected in row_wise manner and are made LOW so that they dont conduct
pinMode(Led_Row_Pins[i],OUTPUT);
digitalWrite(Led_Row_Pins[i],LOW);
pinMode(Led_Column_Pins[i],OUTPUT); // Cathode Pins are connected in column_wise manner and are made HIGH so that they dont conduct
digitalWrite(Led_Column_Pins[i],HIGH);
}
}
void switch_leds(int row) {
int i;
/* switch off all rows */
for(i = 0; i < MAXLEDS; i++) {
digitalWrite(Led_Row_Pins[i], 0);
}
/* switch columns according to current row */
for(i = 0; i < MAXLEDS; i++) {
digitalWrite(Led_Column_Pins[i], !states[row][i]);
}
/* switch on current row */
digitalWrite(Led_Row_Pins[row], 1);
}
void loop() {
static int row = 0;
/* switch on LEDs in a single row */
switch_leds(row);
/* next row */
row++; row %= MAXLEDS;
/* The processing delay between calls to loop() is added to this delay. */
delay(5);
}
The code for the 5*5 push-button matrix is,
#include <Keypad.h>
const byte ROWS = 5;
const byte COLS = 5;
char hexaKeys[ROWS][COLS] = {
{'0', '1', '2' , '3', '4'},
{'5', '6', '7' , '8', '9'},
{'A', 'B', 'C' , 'D', 'E'},
{'F', 'G', 'H', 'I', 'J'},
{'K', 'L', 'M', 'N','O'},
};
byte rowPins[ROWS] = { 40, 39 , 38 , 37 ,36 };
byte colPins[COLS] = { 35 , 34 , 33 , 32 , 31 };
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup(){
Serial.begin(9600);
}
void loop(){
char customKey = customKeypad.getKey();
if(customKey)
Serial.println(customKey);
}