I am not good in coding but I get the result with below code. Can anyone explain that.
because when I use same function in state change example is stop working.
const int latchPin = 10;
const int clockPin = 9;
const int dataPin = 12;
const int OE = 11;
int Btn1 = A0; //button 1 connected to pin 1
int Btn2 = 5; //button 2 connected to pin 2
int Btn3 = 6; //button 3 connected to pin 3
int Btn4 = 7; //button 4 connected to pin 4
int Btn5 = A4; //button 5 connected to pin 5
int Btn6 = A3; //button 6 connected to pin 6
int Btn7 = A2; //button 7 connected to pin 7
int Btn8 = A1; //button 8 connected to pin 8
int buttonstate1 = 0; //state button 1
int buttonstate2 = 0; //state button 2
int buttonstate3 = 0; //state button 3
int buttonstate4 = 0; //state button 4
int buttonstate5 = 0; //state button 5
int buttonstate6 = 0; //state button 6
int buttonstate7 = 0; //state button 7
int buttonstate8 = 0; //state button 8
int Led1 = 0; //Light1 on/off
int Led2 = 0; //Light2 on/off
int Led3 = 0; //Light3 on/off
int Led4 = 0; //Light4 on/off
int Led5 = 0; //Light5 on/off
int Led6 = 0; //Light6 on/off
int Led7 = 0; //Light7 on/off
int Led8 = 0; //Light8 on/off
byte leds = 0;
void setup()
{
pinMode(OE, OUTPUT);
digitalWrite(OE, LOW);
pinMode(Btn1, INPUT_PULLUP);
pinMode(Btn2, INPUT_PULLUP);
pinMode(Btn3, INPUT_PULLUP);
pinMode(Btn4, INPUT_PULLUP);
pinMode(Btn5, INPUT_PULLUP);
pinMode(Btn6, INPUT_PULLUP);
pinMode(Btn7, INPUT_PULLUP);
pinMode(Btn8, INPUT_PULLUP);
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop()
{
buttonstate1 = digitalRead(Btn1);
buttonstate2 = digitalRead(Btn2);
buttonstate3 = digitalRead(Btn3);
buttonstate4 = digitalRead(Btn4);
buttonstate5 = digitalRead(Btn5);
buttonstate6 = digitalRead(Btn6);
buttonstate7 = digitalRead(Btn7);
buttonstate8 = digitalRead(Btn8);
if (buttonstate1 == LOW && Led1 == 0)
{
leds ^= B00000001;
updateShiftRegister();
delay(200);
}
if (buttonstate2 == LOW && Led2 == 0)
{
leds ^= B00000010;
updateShiftRegister();
delay(200);
}
if (buttonstate3 == LOW && Led3 == 0)
{
leds ^= B00000100;
updateShiftRegister();
delay(200);
}
if (buttonstate4 == LOW && Led4 == 0)
{
leds ^= B00001000;
updateShiftRegister();
delay(200);
}
if (buttonstate5 == LOW && Led5 == 0)
{
leds ^= B00010000;
updateShiftRegister();
delay(200);
}
if (buttonstate6 == LOW && Led6 == 0)
{
leds ^= B00100000;
updateShiftRegister();
delay(200);
}
if (buttonstate7 == LOW && Led7 == 0)
{
leds ^= B01000000;
updateShiftRegister();
delay(200);
}
if (buttonstate8 == LOW && Led8 == 0)
{
leds ^= B10000000;
updateShiftRegister();
delay(200);
}
}
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}