Hello everyone!
I got stuck with my new project. I inserted a 74HC595 shift register to control 8 LEDs. In addition I added a push button to switch on/off the (change state) of the LEDs. The problem is that the sequence that I have uploaded to the shift register does not repeat forever but it just completes the program once and then the LEDs turn off. When clicking the toggle button it changes the state successfully but only after the sequence ends - it does not interrupt the operation anytime I press the button.
So basically the problems are the following:
- The sequence commanded by the shift register does not loop forever - it just goes through the sequence once and then it ends.
- Toggle button does not interrupt the sequence to change it at any given time - the sequence gotta finish before changing the state.
Here is the scheme - the wiring is correct - it works fine without the toggle button:
So the problem looks to be somewhere in the coding section which I am not able to solve:
int latchPin=11;
int clockPin=9;
int dataPin=12;
byte LEDS1=0b10101010;
byte LEDS2=0b01010101;
byte LEDS3=0b00001111;
byte LEDS4=0b11110000;
byte LEDS5=0b11111111;
byte LEDS6=0b00000000;
int dt=500;
int buttonRead=2;
int ledState=0;
int OLD=1;
int NEW;
void setup() {
// put your setup code here, to run once:
pinMode (latchPin, OUTPUT);
pinMode (clockPin, OUTPUT);
pinMode (dataPin, OUTPUT);
pinMode (buttonRead, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
NEW=digitalRead(buttonRead);
Serial.println(NEW);
if (OLD==0 && NEW==1) {
if (ledState == 0) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, LEDS1);
digitalWrite(latchPin, HIGH);
delay(dt);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, LEDS2);
digitalWrite(latchPin, HIGH);
delay(dt);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, LEDS3);
digitalWrite(latchPin, HIGH);
delay(dt);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, LEDS4);
digitalWrite(latchPin, HIGH);
delay(dt);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, LEDS5);
digitalWrite(latchPin, HIGH);
delay(dt);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, LEDS6);
digitalWrite(latchPin, HIGH);
delay(dt);
ledState=1;
}
else {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, LEDS6);
digitalWrite(latchPin, HIGH);
ledState=0;
}
}
OLD=NEW;
}
======================
I will be more than happy if you assist me with this project.
Thank you in advance!