Hello, I am currently working on a project using the Arduino Leonardo. I have a 4x4 matrix keypad connected to pins 2,3,4,5,6,7 and 8. The shift Register 74HC595N connected to Pins 11,12, and 13 and is connected to an external 5V supply. The 8 outputs of the shift register are currently connected LCD light bulbs going to GND. The purpose of my project will be to as I push button 1 through 8 on the matrix keypad the LED on the corresponding shift register pin.
The issue I am having is the lights are not always turning on when they should and takes SEVERAL button pushes to actually turn them on. The following code is the code I am using.
#include <Keypad.h>
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] =
{
{‘1’,‘2’,‘3’},
{‘4’,‘5’,‘6’},
{‘7’,‘8’,‘9’},
{‘#’,‘0’,‘*’}
};
byte rowPins[ROWS] = { 2, 3, 4, 5 }; // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins(2,3,4,5).
byte colPins[COLS] = { 6, 7, 8 }; // Connect keypad COL0, COL1 and COL2 to these Arduino pins(6,7,8.
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );// Create the Keypad
int dataPin = 11; //Define which pins will be used for the Shift Register control
int latchPin = 13;
int clockPin = 12;
int Room1 = 0;
int Room2 = 0;
int Room3 = 0;
int Room4 = 0;
int Room5 = 0;
int Room6 = 0;
int Room7 = 0;
int Room8 = 0;
int Rooms = B00000000;
int DigitalState;
void setup()
{
pinMode(dataPin, OUTPUT); //Configure each IO Pin
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
Serial.begin(9600);
Serial.println(“reset”);
}
void loop()
{
char key = kpd.getKey();
if(key) // Check for a valid key.
{
switch (key)
{
case ‘1’:
DigitalState = digitalRead(Room1); // read value currently for Room 1
if (DigitalState == HIGH)// check if value is high
{
digitalWrite(Room1, LOW);// If HIGH write room light to LOW
bitWrite(Rooms, 0, 0);// Writes 0 to the 0bit of the Rooms word
digitalWrite(latchPin, LOW);// So the LED’s dont change as bits get shifted
shiftOut(dataPin, clockPin, MSBFIRST, Rooms);// Send the Rooms word to the pins of the Shift Register
digitalWrite(latchPin, HIGH); // Turn the Latch Pin HIGH so the LED’s will light up
}
else
{
digitalWrite(Room1, HIGH);// If LOW write room light to HIGH
bitWrite(Rooms, 0, 1);// Writes 0 to the 0bit of the Rooms word
digitalWrite(latchPin, LOW);// So the LED’s dont change as bits get shifted
shiftOut(dataPin, clockPin, MSBFIRST, Rooms);// Send the Rooms word to the pins of the Shift Register
digitalWrite(latchPin, HIGH); // Turn the Latch Pin HIGH so the LED’s will light up
}
break;
case ‘2’:
DigitalState = digitalRead(Room2); // read value currently for Room 2
if (DigitalState == HIGH)// check if value is high
{
digitalWrite(Room2, LOW);// If HIGH write room light to LOW
bitWrite(Rooms, 1, 0);// Writes 0 to the 1bit of the Rooms word
digitalWrite(latchPin, LOW);// So the LED’s dont change as bits get shifted
shiftOut(dataPin, clockPin, MSBFIRST, Rooms);// Send the Rooms word to the pins of the Shift Register
digitalWrite(latchPin, HIGH); // Turn the Latch Pin HIGH so the LED’s will light up
}
else
{
digitalWrite(Room2, HIGH);// If LOW write room light to HIGH
bitWrite(Rooms, 1, 1);// Writes 0 to the 1bit of the Rooms word
digitalWrite(latchPin, LOW);// So the LED’s dont change as bits get shifted
shiftOut(dataPin, clockPin, MSBFIRST, Rooms);// Send the Rooms word to the pins of the Shift Register
digitalWrite(latchPin, HIGH); // Turn the Latch Pin HIGH so the LED’s will light up
}
break;
case ‘3’:
DigitalState = digitalRead(Room3); // read value currently for Room 3
if (DigitalState == HIGH)// check if value is high
{
digitalWrite(Room3, LOW);// If HIGH write room light to LOW
bitWrite(Rooms, 2, 0);// Writes 0 to the 2bit of the Rooms word
digitalWrite(latchPin, LOW);// So the LED’s dont change as bits get shifted
shiftOut(dataPin, clockPin, MSBFIRST, Rooms);// Send the Rooms word to the pins of the Shift Register
digitalWrite(latchPin, HIGH); // Turn the Latch Pin HIGH so the LED’s will light up
}
else
{
digitalWrite(Room3, HIGH);// If LOW write room light to HIGH
bitWrite(Rooms, 2, 1);// Writes 0 to the 2bit of the Rooms word
digitalWrite(latchPin, LOW);// So the LED’s dont change as bits get shifted
shiftOut(dataPin, clockPin, MSBFIRST, Rooms);// Send the Rooms word to the pins of the Shift Register
digitalWrite(latchPin, HIGH); // Turn the Latch Pin HIGH so the LED’s will light up
}
break;
case ‘4’:
DigitalState = digitalRead(Room4); // read value currently for Room 4
if (DigitalState == HIGH)// check if value is high
{
digitalWrite(Room4, LOW);// If HIGH write room light to LOW
bitWrite(Rooms, 3, 0);// Writes 0 to the 3bit of the Rooms word
digitalWrite(latchPin, LOW);// So the LED’s dont change as bits get shifted
shiftOut(dataPin, clockPin, MSBFIRST, Rooms);// Send the Rooms word to the pins of the Shift Register
digitalWrite(latchPin, HIGH); // Turn the Latch Pin HIGH so the LED’s will light up
}
else
{
digitalWrite(Room4, HIGH);// If LOW write room light to HIGH
bitWrite(Rooms, 3, 1);// Writes 0 to the 3bit of the Rooms word
digitalWrite(latchPin, LOW);// So the LED’s dont change as bits get shifted
shiftOut(dataPin, clockPin, MSBFIRST, Rooms);// Send the Rooms word to the pins of the Shift Register
digitalWrite(latchPin, HIGH); // Turn the Latch Pin HIGH so the LED’s will light up
}
break;
case ‘5’:
DigitalState = digitalRead(Room5); // read value currently for Room 5
if (DigitalState == HIGH)// check if value is high
{
digitalWrite(Room5, LOW);// If HIGH write room light to LOW
bitWrite(Rooms, 4, 0);// Writes 0 to the 4bit of the Rooms word
digitalWrite(latchPin, LOW);// So the LED’s dont change as bits get shifted
shiftOut(dataPin, clockPin, MSBFIRST, Rooms);// Send the Rooms word to the pins of the Shift Register
digitalWrite(latchPin, HIGH); // Turn the Latch Pin HIGH so the LED’s will light up
}
else
{
digitalWrite(Room5, HIGH);// If LOW write room light to HIGH
bitWrite(Rooms, 4, 1);// Writes 0 to the 4bit of the Rooms word
digitalWrite(latchPin, LOW);// So the LED’s dont change as bits get shifted
shiftOut(dataPin, clockPin, MSBFIRST, Rooms);// Send the Rooms word to the pins of the Shift Register
digitalWrite(latchPin, HIGH); // Turn the Latch Pin HIGH so the LED’s will light up
}
break;
case ‘6’:
DigitalState = digitalRead(Room6); // read value currently for Room 6
if (DigitalState == HIGH)// check if value is high
{
digitalWrite(Room6, LOW);// If HIGH write room light to LOW
bitWrite(Rooms, 5, 0);// Writes 0 to the 5bit of the Rooms word
digitalWrite(latchPin, LOW);// So the LED’s dont change as bits get shifted
shiftOut(dataPin, clockPin, MSBFIRST, Rooms);// Send the Rooms word to the pins of the Shift Register
digitalWrite(latchPin, HIGH); // Turn the Latch Pin HIGH so the LED’s will light up
}
else
{
digitalWrite(Room6, HIGH);// If LOW write room light to HIGH
bitWrite(Rooms, 5, 1);// Writes 0 to the 5bit of the Rooms word
digitalWrite(latchPin, LOW);// So the LED’s dont change as bits get shifted
shiftOut(dataPin, clockPin, MSBFIRST, Rooms);// Send the Rooms word to the pins of the Shift Register
digitalWrite(latchPin, HIGH); // Turn the Latch Pin HIGH so the LED’s will light up
}
break;
case ‘7’:
DigitalState = digitalRead(Room7); // read value currently for Room 7
if (DigitalState == HIGH)// check if value is high
{
digitalWrite(Room7, LOW);// If HIGH write room light to LOW
bitWrite(Rooms, 6, 0);// Writes 0 to the 6bit of the Rooms word
digitalWrite(latchPin, LOW);// So the LED’s dont change as bits get shifted
shiftOut(dataPin, clockPin, MSBFIRST, Rooms);// Send the Rooms word to the pins of the Shift Register
digitalWrite(latchPin, HIGH); // Turn the Latch Pin HIGH so the LED’s will light up
}
else
{
digitalWrite(Room7, HIGH);// If LOW write room light to HIGH
bitWrite(Rooms, 6, 1);// Writes 0 to the 6bit of the Rooms word
digitalWrite(latchPin, LOW);// So the LED’s dont change as bits get shifted
shiftOut(dataPin, clockPin, MSBFIRST, Rooms);// Send the Rooms word to the pins of the Shift Register
digitalWrite(latchPin, HIGH); // Turn the Latch Pin HIGH so the LED’s will light up
}
break;
case ‘8’:
DigitalState = digitalRead(Room8); // read value currently for Room 8
if (DigitalState == HIGH)// check if value is high
{
digitalWrite(Room8, LOW);// If HIGH write room light to LOW
bitWrite(Rooms, 7, 0);// Writes 0 to the 7bit of the Rooms word
digitalWrite(latchPin, LOW);// So the LED’s dont change as bits get shifted
shiftOut(dataPin, clockPin, MSBFIRST, Rooms);// Send the Rooms word to the pins of the Shift Register
digitalWrite(latchPin, HIGH); // Turn the Latch Pin HIGH so the LED’s will light up
}
else
{
digitalWrite(Room8, HIGH);// If LOW write room light to HIGH
bitWrite(Rooms, 7, 1);// Writes 0 to the 7bit of the Rooms word
digitalWrite(latchPin, LOW);// So the LED’s dont change as bits get shifted
shiftOut(dataPin, clockPin, MSBFIRST, Rooms);// Send the Rooms word to the pins of the Shift Register
digitalWrite(latchPin, HIGH); // Turn the Latch Pin HIGH so the LED’s will light up
}
break;
}
}
}
I can’t determine why it takes so long to recognize another button push.
Does anybody know what my problem is or knows another way to solve it?
Thanks!