Lights with Shift Registers and Buttons

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();
  }
}

Too much code!

Please remember to use code tags when posting code.

Here's a kickstart: (uncompiled, untested)

const byte buttonPins [] {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 22, 23, 24};
const int nButtons = sizeof (buttonPins);
int lastStates [nButtons];

int tDelay = 500;

struct {
  const byte latchPin;
  const byte clockPin;
  const byte dataPin;
} shiftDevices [2] {{52, 53, 51},
                    {49, 50, 48}};

void updateShiftRegister (int index, byte leds)
{
  digitalWrite(shiftDevices [index].latchPin, LOW);
  shiftOut(shiftDevices [index].dataPin, shiftDevices [index].clockPin, LSBFIRST, leds);
  digitalWrite(shiftDevices [index].latchPin, HIGH);
}

void setup()
{ 
  for (auto& shiftPins : shiftDevices) {
    pinMode(shiftPins.latchPin, OUTPUT);
    pinMode(shiftPins.dataPin, OUTPUT);
    pinMode(shiftPins.clockPin, 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.
  for (auto& buttonPin : buttonPins)
    pinMode(buttonPin, INPUT_PULLUP);
  updateShiftRegister (0, 0);
  updateShiftRegister (1, 0);
}

1 Like

Read the instructions and modify (pencil icon) your post.

Thanks for the assistance. I modified your code so that it would compile. Right now my code will only tell the serial monitor that the light is on but the light won't actually turn on. I believe it has to do something with the bit setting the shift register but I am not sure how to do that with multiple registers.

int BUTTON_PINS[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 22, 23, 24, 25};
int lastState1 = HIGH; // the previous state from the input pin
int currentState1;    // the current reading from the input pin
int buttonPin = 0;
int tDelay = 500;

int i = 0;
struct {
  const byte latchPin;
  const byte clockPin;
  const byte dataPin;
} shiftDevices [2] {{52, 53, 51},
  {49, 50, 48}
};
//Byte
byte leds = 0;


void updateShiftRegister (int index, byte leds)
{
  digitalWrite(shiftDevices [index].latchPin, LOW);
  shiftOut(shiftDevices [index].dataPin, shiftDevices [index].clockPin, LSBFIRST, leds);
  digitalWrite(shiftDevices [index].latchPin, HIGH);
}


void setup()
{
  for (auto& shiftPins : shiftDevices) {
    pinMode(shiftPins.latchPin, OUTPUT);
    pinMode(shiftPins.dataPin, OUTPUT);
    pinMode(shiftPins.clockPin, 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.
   
   
    for (auto& buttonPin : BUTTON_PINS){
      pinMode(buttonPin, INPUT_PULLUP);
    updateShiftRegister (0, 0);
    updateShiftRegister (1, 0);

  }
  }
}
  void loop() {
    // read the state of the switch:
    currentState1 = digitalRead(2);

    if (lastState1 == LOW && currentState1 == HIGH)
      Serial.println("Light 1 On");
    // save the last state
    lastState1 = currentState1;
    if (currentState1 = HIGH)
      updateShiftRegister(1,0);
  
    
  }

Oops.

Some of your loop nesting in setup has gone a bit crazy

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.