Shifting using Clocks


The following code demonstrates how the clocks work. Notice that the shifting occurs in the direction from pins Q0 to Q7. If you do not want to see all of the shifting steps, comment out the toggleClock(latch) calls and add a digitalWrite(latch,HIGH) at the bottom of the loop:

int data = 2; 
int clock = 3;
int latch = 4;                      

void setup()
{
  pinMode(data, OUTPUT);
  pinMode(clock, OUTPUT);  
  pinMode(latch, OUTPUT);  
}


void loop()                     // run over and over again
{
   int delayTime=400;
   updateLEDs(5);     //start with the pattern 00000101
   delay (delayTime);
   digitalWrite(latch,LOW); //just because the updateLEDs leaves the latch HIGH
                            //need to set it to LOW Otherwise first shift not shown
   //shift in 0 0 1 1 0 0 1 1 and display the registers
   digitalWrite(data, LOW);
   toggleClock(clock);
   toggleClock(latch);
   delay(delayTime);
   digitalWrite(data, LOW);
   toggleClock(clock);
   toggleClock(latch);
   delay(delayTime);
   digitalWrite(data, HIGH);
   toggleClock(clock);
   toggleClock(latch);
   delay(delayTime);
   digitalWrite(data, HIGH);
   toggleClock(clock);
   toggleClock(latch);
   delay(delayTime);
   digitalWrite(data, LOW);
   toggleClock(clock);
   toggleClock(latch);
   delay(delayTime);
   digitalWrite(data, LOW);
   toggleClock(clock);
   toggleClock(latch);
   delay(delayTime);
   digitalWrite(data, HIGH);
   toggleClock(clock);
   toggleClock(latch);
   delay(delayTime);
   digitalWrite(data, HIGH); 
   toggleClock(clock);
   toggleClock(latch);
   delay(delayTime*4);
}

/*
 * updateLEDs() - sends the LED states set in ledStates to the 74HC595
 * sequence
 */
void updateLEDs(int value){
  digitalWrite(latch, LOW);     //Pulls the chips latch low
  shiftOut(data, clock, LSBFIRST, value); //Shifts out the 8 bits to the shift register
  digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
}

/*
 * toggleClock() - toggles the clock pin that is sent in as an argument
 */
void toggleClock(int whichClock)
{
    digitalWrite(whichClock,HIGH);
    delay(1);
    digitalWrite(whichClock,LOW);
}