Hi,
I am trying to build a 24-LED array. I need a grid of 4 x 6 LEDS at a specific distance from one another. I want to be able to determine for each individual LED whether it is on or off, and I want to be able to determine the frequency (the length of ON time and of OFF time) of the blinking of each set of 6 LEDs (that is, the ones that are destined to be on).
I have connected 4 shift registers (74HC595), each to their own three pins of an Arduino Micro. To each of the 4 shift registers I have connected 6 LEDs.
I connected the 595's according to the Arduino Shift Out tutorial.
https://www.arduino.cc/en/tutorial/ShiftOut
But, instead of connecting the second, third and forth shift register to the previous one, I connected each shift register to a separate set of three pins:
Pin 5.5 V Arduino to volt rail bread board
Pin ground Arduino to ground rail bread board
Cathodes LEDs to common ground
Anodes LEDS to pins shift registers
Shift registers:
Q1 to Q6 (pin 1 - 6) to anodes 6 LEDs
Q7 (pin 7) floating
GND (pin 8) to common ground bread board
Q7" (pin 9) floating
MR10 (pin 10) volt rail bread board
SH-CP (pin 11) latch pin to Arduino pin 11, 8, 5, 2 respectively
ST-CP (pin 12) clock pin to Arduino pin 10, 7, 4, 13 respectively
OE (pin 13) to common ground bread board
DS (pin 14) data pin to Arduino pin 12, 9, 6, 3 respectively
Q0 (pin 15) floating
Vcc (pin 16) volt rail bread board
I made a sketch based on code by Amanda Ghassaei, which allows me to determine which LEDs are on and which are off. This part works. If I connect the Arduino and upload the sketch the LEDS that should turn on, turn on, and the others don't.
But, to also give me control over the frequency of blinking, I added code, also from Amanda, using the millis function. This doesn't have any effect. I suspect that once the data is sent from the shift register to the pins, you can't just undo this by setting the latch pin to LOW. But, I am a bit at a loss here. So, any suggestion would be greatly appreciated.
Many thanks in advance for having a look at the circuit and the code.
//Sketch based on code from Amanda Ghassaei Intermediate Arduino: Inputs and Outputs Instructables
//set 595_1 state
int clockPin1 = 10;
int latchPin1 = 11;
int dataPin1 = 12;
int intervalON1 = 100; //length of time the LED is on (pulse width)
int intervalOFF1 = 900; //length of time the LED is off (1 Hz)
int ledState = LOW; // current state of the LED
unsigned long timeOfLastLedEvent = 0; //the last time the LED was updated
//set 595_2 state
int clockPin2 = 7;
int latchPin2 = 8;
int dataPin2 = 9;
int intervalON2 = 100; //length of time the LED is on (pulse width)
int intervalOFF2 = 400; //length of time the LED is off (2 Hz)
//set 595_3 state
int clockPin3 = 4;
int latchPin3 = 5;
int dataPin3 = 6;
int intervalON3 = 100; //length of time the LED is on (pulse width)
int intervalOFF3 = 9900; //length of time the LED is off (0.1 Hz)
//set 595_4 state
int clockPin4 = 13;
int latchPin4 = 2;
int dataPin4 = 3;
int intervalON4 = 100; //length of time the LED is on (pulse width)
int intervalOFF4 = 4900; //length of time the LED is off (0.2 Hz)
byte numberToDisplay1 = 682;
byte numberToDisplay2 = 27;
byte numberToDisplay3 = 7;
byte numberToDisplay4 = 44;
void setup() {
//all connections to 595 are outputs
pinMode(latchPin1, OUTPUT);
pinMode(clockPin1, OUTPUT);
pinMode(dataPin1, OUTPUT);
digitalWrite(latchPin1, ledState);
pinMode(latchPin2, OUTPUT);
pinMode(clockPin2, OUTPUT);
pinMode(dataPin2, OUTPUT);
digitalWrite(latchPin2, ledState);
pinMode(latchPin3, OUTPUT);
pinMode(clockPin3, OUTPUT);
pinMode(dataPin3, OUTPUT);
digitalWrite(latchPin3, ledState);
pinMode(latchPin4, OUTPUT);
pinMode(clockPin4, OUTPUT);
pinMode(dataPin4, OUTPUT);
digitalWrite(latchPin4, ledState);
}
void loop() {
unsigned long currentMillis = millis();
if (ledState == LOW){//if the LED is already off
if (currentMillis - timeOfLastLedEvent > intervalOFF1){//and enough time has passed
digitalWrite(latchPin1, HIGH);//turn latchPin1 on
if (currentMillis - timeOfLastLedEvent > intervalOFF2){
digitalWrite(latchPin2, HIGH);
if (currentMillis - timeOfLastLedEvent > intervalOFF3){
digitalWrite(latchPin3, HIGH);
if (currentMillis - timeOfLastLedEvent > intervalOFF4){
digitalWrite(latchPin4, HIGH);
ledState = HIGH;//store its current state
timeOfLastLedEvent = currentMillis;//update the time of this new event
shiftOut(dataPin1, clockPin1, LSBFIRST, numberToDisplay1);
shiftOut(dataPin2, clockPin2, LSBFIRST, numberToDisplay2);
shiftOut(dataPin3, clockPin3, LSBFIRST, numberToDisplay3);
shiftOut(dataPin4, clockPin4, LSBFIRST, numberToDisplay4);
}
}
}
}
} else {//if the LED is already on
if (currentMillis - timeOfLastLedEvent > intervalON1){
digitalWrite(latchPin1, LOW); //turn latchpin1 off
if (currentMillis - timeOfLastLedEvent > intervalON2){
digitalWrite(latchPin2, LOW);
if (currentMillis - timeOfLastLedEvent > intervalON3){
digitalWrite(latchPin3, LOW);
if (currentMillis - timeOfLastLedEvent > intervalON4){
digitalWrite(latchPin4, LOW);
ledState = LOW;
timeOfLastLedEvent = currentMillis;
}
}
}
}
}
}