I am trying to control LEDs with a several buttons and shift registers. The issue I am having is that when I press a button several lights turn on instead of just one. It seems like it is never the same light:(
Code:
//Constant Integers
const int BUTTON_PIN1 = 2;
const int BUTTON_PIN2 = 3;
const int BUTTON_PIN3 = 4;
const int BUTTON_PIN4 = 5;
const int BUTTON_PIN5 = 6;
const int BUTTON_PIN6 = 7;
const int BUTTON_PIN7 = 8;
const int BUTTON_PIN8 = 9;
const int BUTTON_PIN9 = 10;
const int BUTTON_PIN10 = 11;
const int BUTTON_PIN11 = 12;
const int BUTTON_PIN12 = 13;
const int BUTTON_PIN13 = 22;
const int BUTTON_PIN14 = 23;
const int BUTTON_PIN15 = 24;
int lastState1 = HIGH; // the previous state from the input pin
int currentState1; // the current reading from the input pin
int lastState2 = HIGH; // the previous state from the input pin
int currentState2; // the current reading from the input pin
int lastState3 = HIGH; // the previous state from the input pin
int currentState3; // the current reading from the input pin
int lastState4 = HIGH; // the previous state from the input pin
int currentState4; // the current reading from the input pin
int lastState5 = HIGH; // the previous state from the input pin
int currentState5; // the current reading from the input pin
int lastState6 = HIGH; // the previous state from the input pin
int currentState6; // the current reading from the input pin
int lastState7 = HIGH; // the previous state from the input pin
int currentState7; // the current reading from the input pin
int lastState8 = HIGH; // the previous state from the input pin
int currentState8; // the current reading from the input pin
int lastState9 = HIGH; // the previous state from the input pin
int currentState9; // the current reading from the input pin
int lastState10 = HIGH; // the previous state from the input pin
int currentState10; // the current reading from the input pin
int lastState11 = HIGH; // the previous state from the input pin
int currentState11; // the current reading from the input pin
int lastState12 = HIGH; // the previous state from the input pin
int currentState12; // the current reading from the input pin
int lastState13 = HIGH; // the previous state from the input pin
int currentState13; // the current reading from the input pin
int lastState14 = HIGH; // the previous state from the input pin
int currentState14; // the current reading from the input pin
int lastState15 = HIGH; // the previous state from the input pin
int currentState15; // the current reading from the input pin
int tDelay = 500;
int latchPin1 = 52; // (52) ST_CP [RCK] on 74HC595
int clockPin1 = 53; // (53) SH_CP [SCK] on 74HC595
int dataPin1 = 51; // (51) DS [S1] on 74HC595
int latchPin2 = 49; // (49) ST_CP [RCK] on 74HC595
int clockPin2 = 50; // (50) SH_CP [SCK] on 74HC595
int dataPin2 = 48; // (48) DS [S1] on 74HC595
int i = 0;
//Byte
byte leds = 0;
void updateShiftRegister1()
{
digitalWrite(latchPin1, LOW);
shiftOut(dataPin1, clockPin1, LSBFIRST, leds);
digitalWrite(latchPin1, HIGH);
}
void updateShiftRegister2()
{
digitalWrite(latchPin2, LOW);
shiftOut(dataPin2, clockPin2, LSBFIRST, leds);
digitalWrite(latchPin2, HIGH);
}
void setup()
{
pinMode(latchPin1, OUTPUT);
pinMode(dataPin1, OUTPUT);
pinMode(clockPin1, OUTPUT);
pinMode(latchPin2, OUTPUT);
pinMode(dataPin2, OUTPUT);
pinMode(clockPin2, OUTPUT);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// initialize the pushbutton pin as an pull-up input
// the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed.
pinMode(BUTTON_PIN1, INPUT);
pinMode(BUTTON_PIN2, INPUT);
pinMode(BUTTON_PIN3, INPUT);
pinMode(BUTTON_PIN4, INPUT);
pinMode(BUTTON_PIN5, INPUT);
pinMode(BUTTON_PIN6, INPUT);
pinMode(BUTTON_PIN7, INPUT);
pinMode(BUTTON_PIN8, INPUT);
pinMode(BUTTON_PIN9, INPUT);
pinMode(BUTTON_PIN10, INPUT);
pinMode(BUTTON_PIN11, INPUT);
pinMode(BUTTON_PIN12, INPUT);
pinMode(BUTTON_PIN13, INPUT);
pinMode(BUTTON_PIN14, INPUT);
pinMode(BUTTON_PIN15, INPUT);
updateShiftRegister1();
bitClear(leds, 0);
updateShiftRegister1();
updateShiftRegister2();
bitClear(leds, 0);
updateShiftRegister2();
}
void loop() {
// read the state of the switch:
currentState1 = digitalRead(BUTTON_PIN1);
currentState2 = digitalRead(BUTTON_PIN2);
currentState3 = digitalRead(BUTTON_PIN3);
currentState4 = digitalRead(BUTTON_PIN4);
currentState5 = digitalRead(BUTTON_PIN5);
currentState6 = digitalRead(BUTTON_PIN6);
currentState7 = digitalRead(BUTTON_PIN7);
currentState8 = digitalRead(BUTTON_PIN8);
currentState9 = digitalRead(BUTTON_PIN9);
currentState10 = digitalRead(BUTTON_PIN10);
currentState11 = digitalRead(BUTTON_PIN11);
currentState12 = digitalRead(BUTTON_PIN12);
currentState13 = digitalRead(BUTTON_PIN13);
currentState14 = digitalRead(BUTTON_PIN14);
currentState15 = digitalRead(BUTTON_PIN15);
if (lastState1 == LOW && currentState1 == HIGH)
Serial.println("Light 1 On");
// save the last state
lastState1 = currentState1;
if (currentState1 == HIGH) {
updateShiftRegister1();
bitSet(leds, 7);
updateShiftRegister1();
}
if (lastState2 == LOW && currentState2 == HIGH)
Serial.println("Light 2 On");
// save the last state
lastState2 = currentState2;
if (currentState2 == HIGH) {
updateShiftRegister1();
bitSet(leds, 0);
updateShiftRegister1();
}
if (lastState3 == LOW && currentState3 == HIGH)
Serial.println("Light 3 On");
// save the last state
lastState3 = currentState3;
if (currentState3 == HIGH) {
updateShiftRegister1();
bitSet(leds, 1);
updateShiftRegister1();
}
if (lastState4 == LOW && currentState4 == HIGH)
Serial.println("Light 4 On");
// save the last state
lastState4 = currentState4;
if (currentState4 == HIGH) {
updateShiftRegister1();
bitSet(leds, 2);
updateShiftRegister1();
}
if (lastState5 == LOW && currentState5 == HIGH)
Serial.println("Light 5 On");
// save the last state
lastState5 = currentState5;
if (currentState5 == HIGH) {
updateShiftRegister1();
bitSet(leds, 3);
updateShiftRegister1();
}
if (lastState6 == LOW && currentState6 == HIGH)
Serial.println("Light 6 On");
// save the last state
lastState6 = currentState6;
if (currentState1 == HIGH) {
updateShiftRegister1();
bitSet(leds, 4);
updateShiftRegister1();
}
if (lastState7 == LOW && currentState7 == HIGH)
Serial.println("Light 7 On");
// save the last state
lastState7 = currentState7;
if (currentState7 == HIGH) {
updateShiftRegister1();
bitSet(leds, 5);
updateShiftRegister1();
}
if (lastState8 == LOW && currentState8 == HIGH)
Serial.println("Light 8 On");
// save the last state
lastState8 = currentState8;
if (currentState8 == HIGH) {
updateShiftRegister1();
bitSet(leds, 6);
updateShiftRegister1();
}
if (lastState9 == LOW && currentState9 == HIGH)
Serial.println("Light 9 On");
// save the last state
lastState9 = currentState9;
if (currentState9 == HIGH) {
updateShiftRegister2();
bitSet(leds, 7);
updateShiftRegister2();
}
if (lastState10 == LOW && currentState10 == HIGH)
Serial.println("Light 10 On");
// save the last state
lastState10 = currentState10;
if (currentState10 == HIGH) {
updateShiftRegister2();
bitSet(leds, 0);
updateShiftRegister2();
}
if (lastState11 == LOW && currentState11 == HIGH)
Serial.println("Light 11 On");
// save the last state
lastState11 = currentState11;
if (currentState11 == HIGH) {
updateShiftRegister2();
bitSet(leds, 1);
updateShiftRegister2();
}
if (lastState12 == LOW && currentState12 == HIGH)
Serial.println("Light 12 On");
// save the last state
lastState12 = currentState12;
if (currentState12 == HIGH) {
updateShiftRegister2();
bitSet(leds, 2);
updateShiftRegister2();
}
if (lastState13 == LOW && currentState13 == HIGH)
Serial.println("Light 13 On");
// save the last state
lastState13 = currentState13;
if (currentState13 == HIGH) {
updateShiftRegister2();
bitSet(leds, 3);
updateShiftRegister2();
}
if (lastState14 == LOW && currentState14 == HIGH)
Serial.println("Light 14 On");
// save the last state
lastState14 = currentState14;
if (currentState14 == HIGH) {
updateShiftRegister2();
bitSet(leds, 4);
updateShiftRegister2();
}
if (lastState15 == LOW && currentState15 == HIGH)
Serial.println("Light 15 On");
// save the last state
lastState15 = currentState15;
if (currentState15 == HIGH) {
updateShiftRegister2();
bitSet(leds, 5);
updateShiftRegister2();
}
}