Table of Contents
Switch Matrix in embedded c
A keyboard matrix is a circuit used in most electronic computer keyboards in which the key switches are connected by a grid of wires. Without a matrix circuit, each switch would require its own input pin to the controller. Keyboard switch matrices are arranged in rows and columns. if a key is pressed, a column wire makes contact with a row wire and completes a circuit. This closed circuit, triggers the algorithm in the controller and output is given.
CODE FOR BASIC BUTTON MATRIX AND TO CONTROL LEDs.
//4*3 Switch Matrix
//all 12 outputs are given with a LED turned ON for 0.8 second
//LDR* are the switches
//LDR1-LDR3 is the row
//LDR4-LDR7 is the column
//LED* are the outputs
//The output can be changed based of the needs
int LDR1 = A0;
int LDR2 = A1;
int LDR3 = A2;
int LDR4 = A3;
int LDR5 = A4;
int LDR6 = A5;
int LDR7 = A6;
int LED1 = 13;
int LED2 = 12;
int LED3 = 11;
int LED4 = 10;
int LED5 = 9;
int LED6 = 8;
int LED7 = 7;
int LED8 = 6;
int LED9 = 5;
int LED10 = 4;
int LED11 = 3;
int LED12 = 2;
void setup()
{
pinMode(LDR1, INPUT);
pinMode(LDR2, INPUT);
pinMode(LDR3, INPUT);
pinMode(LDR4, INPUT);
pinMode(LDR5, INPUT);
pinMode(LDR6, INPUT);
pinMode(LDR7, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT);
pinMode(LED9, OUTPUT);
pinMode(LED10, OUTPUT);
pinMode(LED11, OUTPUT);
pinMode(LED12, OUTPUT);
}
void loop()
{
if ((analogRead(LDR1) < 150) && (analogRead(LDR4) < 150))
{
digitalWrite(LED1, HIGH);
delay(800);
digitalWrite(LED1, LOW);
}
else if ((analogRead(LDR1) < 150) && (analogRead(LDR5) < 150))
{
digitalWrite(LED2, HIGH);
delay(800);
digitalWrite(LED2, LOW);
}
else if ((analogRead(LDR1) < 150) && (analogRead(LDR6) < 150))
{
digitalWrite(LED3, HIGH);
delay(800);
digitalWrite(LED3, LOW);
}
else if ((analogRead(LDR1) < 150) && (analogRead(LDR7) < 150))
{
digitalWrite(LED4, HIGH);
delay(800);
digitalWrite(LED4, LOW);
}
else if ((analogRead(LDR2) < 150) && (analogRead(LDR4) < 150))
{
digitalWrite(LED5, HIGH);
delay(800);
digitalWrite(LED5, LOW);
}
else if ((analogRead(LDR2) < 150) && (analogRead(LDR5) < 150))
{
digitalWrite(LED6, HIGH);
delay(800);
digitalWrite(LED6, LOW);
}
else if ((analogRead(LDR2) < 150) && (analogRead(LDR6) < 150))
{
digitalWrite(LED7, HIGH);
delay(800);
digitalWrite(LED7, LOW);
}
else if ((analogRead(LDR2) < 150) && (analogRead(LDR7) < 150))
{
digitalWrite(LED8, HIGH);
delay(800);
digitalWrite(LED8, LOW);
}
else if ((analogRead(LDR3) < 150) && (analogRead(LDR4) < 150))
{
digitalWrite(LED9, HIGH);
delay(800);
digitalWrite(LED9, LOW);
}
else if ((analogRead(LDR3) < 150) && (analogRead(LDR5) < 150))
{
digitalWrite(LED10, HIGH);
delay(800);
digitalWrite(LED10, LOW);
}
else if ((analogRead(LDR3) < 150) && (analogRead(LDR6) < 150))
{
digitalWrite(LED11, HIGH);
delay(800);
digitalWrite(LED11, LOW);
}
else if ((analogRead(LDR3) < 150) && (analogRead(LDR7) < 150))
{
digitalWrite(LED12, HIGH);
delay(800);
digitalWrite(LED12, LOW);
}}
Summary :
In this article we saw keyboard switches matrix in embedded c so about this section you have any query then free to ask me
Name of intern who share this task : Pratham bhomkar