Playing night rider with three 74HC595s
Connected three 74HC595s in series and entertained myself by simulating KITT from Knightrider. Would have hooked up the fourth one but was running out of realestate and LEDs. I should have a another breadboard in a few days along with some 7 segment leds that I will be hooking up and controlling with the 74HC595s.
NOTE: The breadboarding of 4 seven-segment displays and driving them with the 74HC595's is an exercise in digital electronics and programming not meant to be a recommendation for implementation in an actual project. If you need a 7 segment display in your project I recommend using I2C like I am doing in this photo using the Adafruit 0.56 4-digit 7-Segment Display w/I2C backpack (assembly with soldering required).
Till then, enjoy the video and code.
UPDATE - Found that with zero delay the data is getting clocked to the registers at 58KHz. I thought it would be faster but then if you divide 58Khz into the 16MHz clock of the arduino you get 275 (275 machine cycles). That means it takes the Arduino 17 microseconds to complete the loop when there is no delay. Looking at the code it is hard to believe 275 machines cycles are required to prepare the next unsigned long integer to be shifted out then clock it out, but I do not know how many of those are used up by the ShiftOut() library function that is called three tiems to shift out all 24 bits.
/*
Using three 74HC595s to control 24 leds (outputs) on the arduino uno.
This is an improved version by John Rogers
*/
int latchPin = 5; // to pin 12
int clockPin = 6; // to pin 11
int dataPin = 4; // to pin 14
int numLights=24;
unsigned long light = 0; // Bit pattern for entire display
byte b1 = 0; // byte place holders for each shift register
byte b2 = 0; // extracted from "light"
byte b3 = 0;
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop()
{
for (int i = 0; i < numLights*2; i++)
{
int shift = i;
if( i >= numLights ) //if i is above numlights then wee need to start
{ // moving back to the left.
shift = numLights*2 - i - 1;
}
// Extract the bytes of the light patter to be sent out
light = (long)(1)<<shift;
b1 = (byte)((light>>16)&0xFF);
b2 = (byte)((light>>8)&0xFF);
b3 = (byte)((light)&0xFF);
updateShiftRegister(); // shift out b1, b2 and b3
delay(30);
}
}
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, b1);
shiftOut(dataPin, clockPin, MSBFIRST, b2);
shiftOut(dataPin, clockPin, MSBFIRST, b3);
digitalWrite(latchPin, HIGH);
// For debugging
/*Serial.print(i);
Serial.print(" ");
Serial.print(light);
Serial.print(" ");
Serial.print(b1);
Serial.print(" ");
Serial.print(b2);
Serial.print(" ");
Serial.println(b3);*/
}
David Riewe
Published
Tue 10 November 2015
Comments
comments powered by Disqus