int datapin = 4;
int clockpin = 3;
int latchpin = 2;
// sketch written by SparkFun Electronics
byte data = 0;
void setup()
{
// Set the three SPI pins to be outputs:
pinMode(datapin, OUTPUT);
pinMode(clockpin, OUTPUT);
pinMode(latchpin, OUTPUT);
}
void loop()
{
// To try the different functions below, uncomment the one
// you want to run, and comment out the remaining ones to
// disable them from running.
binaryCount(); // Bit patterns from 0 to 255
}
void shiftOutput(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val) {
uint8_t i;
for(i = 0; i < 8; i++) {
if(bitOrder == LSBFIRST)
digitalWrite(dataPin, !!(val & (1 << i)));
else
digitalWrite(dataPin, !!(val & (1 << (7 - i))));
digitalWrite(clockPin, HIGH);
digitalWrite(clockPin, LOW);
}
}
void binaryCount()
{
// This function creates a visual representation of the on/off pattern
// of bits in a byte.
int delayTime = 200; // time (milliseconds) to pause between LEDs
// make this smaller for faster switching
// Send the data byte to the shift register:
// shiftOut(datapin, clockpin, MSBFIRST, data);
shiftOutput(datapin, clockpin, MSBFIRST, 15);
// Toggle the latch pin to make the data appear at the outputs:
digitalWrite(latchpin, HIGH);
digitalWrite(latchpin, LOW);
// Add one to data, and repeat!
// (Because a byte type can only store numbers from 0 to 255,
// if we add more than that, it will "roll around" back to 0
// and start over).
data++;
// Delay so you can see what's going on:
delay(delayTime);
}