So i was trying to control 8 leds with only one button like
press once:
B00000001
press twice:
B00000010
But the i just can't write the code can anyone help?
I'm using arduino nano.
Here's the code thats not working as intended:
int latchPin = 4;
int clockPin = 3;
int dataPin = 5;
int buttonPin = 2;
int buttonState = 0;
int buttonPressCount = 0;
byte leds = 0;
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(buttonPin,INPUT);
}
void loop()
{
leds = 0;
buttonState = digitalRead(buttonPin);
delay(500);
if (buttonState == HIGH & buttonPressCount == 0){
if (buttonPressCount == 0 & buttonState == HIGH)
digitalWrite(latchPin,LOW);
shiftOut(dataPin,clockPin,MSBFIRST,B00000000);
digitalWrite(latchPin,HIGH);
delay(100);
if (buttonPressCount == 1& buttonState == HIGH)
digitalWrite(latchPin,LOW);
shiftOut(dataPin,clockPin,MSBFIRST,B10000000);
digitalWrite(latchPin,HIGH);
delay(100);
if (buttonPressCount == 2 & buttonState == HIGH)
digitalWrite(latchPin,LOW);
shiftOut(dataPin,clockPin,MSBFIRST,B01000000);
digitalWrite(latchPin,HIGH);
delay(100);
if (buttonPressCount == 3 & buttonState == HIGH)
digitalWrite(latchPin,LOW);
shiftOut(dataPin,clockPin,MSBFIRST,B00100000);
digitalWrite(latchPin,HIGH);
delay(100);
if (buttonPressCount == 4 & buttonState == HIGH )
digitalWrite(latchPin,LOW);
shiftOut(dataPin,clockPin,MSBFIRST,B00010000);
digitalWrite(latchPin,HIGH);
delay(100);
if (buttonPressCount == 5 & buttonState == HIGH)
digitalWrite(latchPin,LOW);
shiftOut(dataPin,clockPin,MSBFIRST,B00001000);
digitalWrite(latchPin,HIGH);
delay(100);
if (buttonPressCount == 6 & buttonState == HIGH)
digitalWrite(latchPin,LOW);
shiftOut(dataPin,clockPin,MSBFIRST,B00000100);
digitalWrite(latchPin,HIGH);
delay(100);
if (buttonPressCount == 7 & buttonState == HIGH)
digitalWrite(latchPin,LOW);
shiftOut(dataPin,clockPin,MSBFIRST,B00000010);
digitalWrite(latchPin,HIGH);
delay(100);
if (buttonPressCount == 8 & buttonState == HIGH)
digitalWrite(latchPin,LOW);
shiftOut(dataPin,clockPin,MSBFIRST,B00000001);
digitalWrite(latchPin,HIGH);
delay(100);
buttonPressCount++;
if(buttonPressCount>8)
buttonPressCount=0;
delay(300);
}
}