// Code based on shiftOutCode, Hello World // By : Carlyn Maw,Tom Igoe, David A. Mellis // Updated by Daniel Hirschmann to offer a way to control // cascading shift registers //This example is intended to create a night rider effect - speed based on a potentiometer int latchPin = 8; //Pin connected to latch pin (ST_CP) of 74HC595 int clockPin = 12; //Pin connected to clock pin (SH_CP) of 74HC595 int dataPin = 11; //Pin connected to Data in (DS) of 74HC595 int totalLeds = 8; int ledPos = 0; int ledDirection = 1; int delayPeriod = 10; int potVal = 50; void setup() { //set pins to output because they are addressed in the main loop pinMode(latchPin, OUTPUT); pinMode(dataPin, OUTPUT); pinMode(clockPin, OUTPUT); Serial.begin(9600); Serial.println("STARTED"); } void loop() { // potVal = analogRead(0)/4; //get the pot reading for the speed. //Handle the night rider effect ledPos+=ledDirection; // same as: ledPos = ledPos + ledDirection if (ledPos >= totalLeds - 1) { ledDirection = - 1; } else if (ledPos <= 0) { ledDirection = 1; } Serial.println(ledPos); shiftToRegister(ledPos,HIGH); delay(potVal); } // Function to send bits to the shift register: void shiftToRegister(int whichPin, int whichState) { digitalWrite(latchPin, LOW); //Prepares the SR to receive data byte shiftRegisterByte = encodeBits(whichPin, whichState); shiftOut(dataPin, clockPin, MSBFIRST, shiftRegisterByte); digitalWrite(latchPin, HIGH); //Tell the SR's to show us what we've just sent it! } //Function to encode a position value into a resulting position on the shift registers byte encodeBits(int pos, int val) { byte result = 1 << pos; return result; }