/*
Arduino code for Individual control over each pin
Support for 40+ 74HC595 8 bit shift registers
http://bildr.org/2011/02/74hc595/
*/
#define dataPin 8 // Pin connected to DS of 74HC595
#define latchPin 9 // Pin connected to STCP of 74HC595
#define clockPin 10 // Pin connected to SHCP of 74HC595
word leds = 0; // Variable to hold the pattern of which LEDs are currently turned on or off
/*
setup() - this function runs once when you turn your Arduino on
We initialize the serial connection with the computer
*/
void setup()
{
// Set all the pins of 74HC595 as OUTPUT
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
/*
loop() - this function runs over and over again
*/
void loop()
{
leds = 0; // Initially turns all the LEDs off, by giving the variable 'leds' the value 0
updateShiftRegister();
delay(200);
for (int i = 0; i < 16; i++) // Turn all the LEDs ON one by one.
{
bitSet(leds, i); // Set the bit that controls that LED in the variable 'leds'
updateShiftRegister();
delay(300);
}
}
/*
updateShiftRegister() - This function sets the latchPin to low, then calls the Arduino function 'shiftOut' to shift out contents of variable 'leds' in the shift register before putting the 'latchPin' high again.
*/
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, leds >> 8);
shiftOut(dataPin, clockPin, MSBFIRST, leds >> 0);
digitalWrite(latchPin, HIGH);
}