hi all
first time poster so bare with me
i am having some severe lag issues with the 74HC595, perhaps it is in my code
what i am attempting to do. using 74HC595 to put out appropriate pin
and light 3x common cathode RGB LED based on button combo
picture 3 buttons
push any single button and you get the corresponding blue
push any combination of 2 and you get corresponding green
push all three you get corresponding red
i am able to run pattern and it executes the loop successfully and precisely
the problem i encounter is with the push buttons
when i loop through the digitalRead for the three buttons
i have assigned values to the button to correspond with the output array index
ie: button 1 = 1, button 2 = 2, button 3 = 4
button 1 = index 1
button 2 = index 2
button 1 + 2 = index 3
button 3 = index 4
button 1 + 3 = index 5
button 2 + 3 = index 6
button 1 + 2 + 3 = index 7
anyway, see the code below for reference
the pattern() methods executes fine
the input() and lights() methods provide me with sever lag
meaning i press button 1, and the blue LED lights up instantaiously, but then takes about a second to turn off!?!?!?
any help and advice would be welcome!
//these are for the shift register
/*
Q1 -| U |- VCC
Q2 -| |- Q0
Q3 -| |- DS
Q4 -| |- OE
Q5 -| |- ST
Q6 -| |- SH
Q7 -| |- MR
GD -| |- Q7'
*/
#define LATCH_PIN 11 //Pin connected to ST_CP 74HC595
#define CLOCK_PIN 12 //Pin connected to SH_CP 74HC595
#define DATA_PIN 10 //Pin connected to DS of 74HC595
// pattern test
int counter = 0;
int powerup = 0;
/*
L = 1
M = 2
H = 4
L + M = 1 + 2 = 3
L + H = 1 + 4 = 5
M + H = 2 + 4 = 6
L + M + H = 1 + 2 + 4 = 7
*/
int output[8] = {
B00000000, // OFF
B10000000, // L
B00100000, // M
B01010000, // L + M
B00001000, // H
B01000100, // L + H
B00010100, // M + H
B00000010 // L + M + H
};
// pins
int buttonPins[3] = {2,3,4};
int buttonIndex = 0;
int values[3] = {1,2,4};
void setup()
{
//shift register pins are output.
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
//initiate pins as input
for(int n = 0; n < 3; n++)
{
pinMode(buttonPins[n], INPUT);
}
}
void loop()
{
if(powerup == 0){pattern();}
input();
}
void input()
{
// reset index values
buttonIndex = 0;
// read button states
for(int n = 0; n < 3; n++)
{
buttonIndex += digitalRead(buttonPins[n]) == 1 ? values[n] : 0;
}
lights();
}
void lights()
{
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, output[buttonIndex]);
digitalWrite(LATCH_PIN, HIGH);
digitalWrite(LATCH_PIN, LOW);
}
void pattern()
{
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN,MSBFIRST, output[counter]);
digitalWrite(LATCH_PIN, HIGH);
counter++;
if (counter > 8)
{
counter = 0;
powerup = 1;
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, output[0]);
digitalWrite(LATCH_PIN, HIGH);
}
delay(250);
}