/*
SparkFun Inventor's Kit
Example sketch 14
SHIFT REGISTER
Use a shift register to turn three pins into eight (or more!)
outputs
An integrated circuit ("IC"), or "chip", is a self-contained
circuit built into a small plastic package. (If you look closely
at your Arduino board you'll see a number of ICs.) There are
thousands of different types of ICs available that you can use
to perform many useful functions.
The 74HC595 shift register in your kit is an IC that has eight
digital outputs. To use these outputs, we'll use a new interface
called SPI (Serial Peripheral Interface). It's like the TX and
RX you're used to, but has an additional "clock" line that
controls the speed of the data transfer. Many parts use SPI
for communications, so the Arduino offers simple commands called
shiftIn() and shiftOut() to access these parts.
This IC lets you use three digital pins on your Arduino to
control eight digital outputs on the chip. And if you need
even more outputs, you can daisy-chain multiple shift registers
together, allowing an almost unlimited number of outputs from
the same three Arduino pins! See the shift register datasheet
for details:
http://www.sparkfun.com/datasheets/IC/SN74HC595.pdf
This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
This code is completely free for any use.
Visit http://learn.sparkfun.com/products/2 for SIK information.
Visit http://www.arduino.cc to learn about the Arduino.
Version 2.0 6/2012 MDG
*/
// Pin definitions:
// The 74HC595 uses a type of serial connection called SPI
// (Serial Peripheral Interface) that requires three pins:
int datapin = 2;
int clockpin = 3;
int latchpin = 4;
// We'll also declare a global variable for the data we're
// sending to the shift register:
byte data = 0;
void setup()
{
// Set the three SPI pins to be outputs:
pinMode(datapin, OUTPUT);
pinMode(clockpin, OUTPUT);
pinMode(latchpin, OUTPUT);
}
void loop()
{
// Call the blinking function
blinkLEDs(); // LEDs will blink on and off
}
void shiftWrite(int desiredPin, boolean desiredState)
{
// This function lets you make the shift register outputs
// HIGH or LOW in exactly the same way that you use digitalWrite().
bitWrite(data, desiredPin, desiredState); // Change desired bit to 0 or 1 in "data"
// Now we'll actually send that data to the shift register.
// The shiftOut() function does all the hard work of
// manipulating the data and clock pins to move the data
// into the shift register:
shiftOut(datapin, clockpin, MSBFIRST, data); // Send "data" to the shift register
// Toggle the latchPin to make "data" appear at the outputs
digitalWrite(latchpin, HIGH);
digitalWrite(latchpin, LOW);
}
void blinkLEDs()
{
int delayTime = 500; // Time (milliseconds) for the blink interval
// Turn all LEDs on
for (int index = 0; index <= 7; index++)
{
shiftWrite(index, HIGH);
}
delay(delayTime); // Wait for the specified delay
// Turn all LEDs off
for (int index = 0; index <= 7; index++)
{
shiftWrite(index, LOW);
}
delay(delayTime); // Wait for the specified delay
}