Daisy chained 74HC595 irregular behaviour

Hi,

I am already trying some time now to get two SN74HC595N daisy chained / cascaded to work properly with my arduino nano. What i try to accomplish is quite simple. I have 16 leds which i want to light up one by one from left to right, and then turn them off from right to left. Just like a VU meter.

The issue is that the leds during some intervals turn off, then some steps in the process the current number of leds go on.

The code is use;

int datapin = 9; 
int clockpin = 10;
int latchpin = 11;

void setup() {
  pinMode(datapin, OUTPUT);
  pinMode(clockpin, OUTPUT); 
  pinMode(latchpin, OUTPUT);

 Serial.begin(9600);

}

void loop() {

  enableSword2(true) ;
  delay(2000) ;
  disableSword2(true) ;
  

}


void enableSword2(boolean enable) {
  
  Serial.println(" ---- enablesword " ) ; 

  // 2 74HC595 shift registers each assigned one byte
  byte leftByte ;
  byte rightByte ;
  // longByte represents left + right byte
  int longByte ;

  // Start with 000000000000000
  longByte = 0 ;

  // Left shift 16 bits
  for (int x=0 ; x<16; x++) {
    
    longByte = longByte + (1 << x) ;
   
    // Print bits for debugging
    String bytestr ;
    for (int y=0 ; y<16 ; y++) {
      bytestr = String(bitRead(longByte,y)) + bytestr ;
     }  
    Serial.println("ByteBits: " + bytestr) ;

 
    // assign longByte to left and right byte
    for (int y=7 ; y>=0 ; y--) {
      bitWrite(leftByte,y,bitRead(longByte,y+7)) ;
      bitWrite(rightByte,y,bitRead(longByte,y)) ;
    }
    

    // Toggle latch to LOW to write new bits yo 74HC595's
    digitalWrite(latchpin, LOW);

    // Write two bytes
    shiftOut(datapin, clockpin, leftByte);
    shiftOut(datapin, clockpin, rightByte);

    // Toggle latch to HIGH to show new bits    
    digitalWrite(latchpin, HIGH);    
       
    delay(7) ;
   delay(2000) ;
 }
 

  Serial.println(" ---- enablesword done " ) ; 
  
}



void disableSword2(boolean enable) {

  Serial.println(" ---- disablesword " ) ;   
  
  // 2 74HC595 shift registers each assigned one byte
  byte leftByte ;
  byte rightByte ;
  // longByte represents left + right byte
  int longByte ;

  // Start with 1111111111111111
  longByte = 65535 ;
  
  // Right shift 16 bits  
  for (int x=0 ; x<16; x++) {
    
    longByte = 65535 >> x ;

    // Print bits for debugging   
    String bytestr ;
    for (int y=0 ; y<16 ; y++) {
      bytestr = String(bitRead(longByte,y)) + bytestr ;
    }  
    Serial.println("ByteBits: " + bytestr) ;

    // assign longByte to left and right byte
    for (int y=7 ; y>=0 ; y--) {
      bitWrite(leftByte,y,bitRead(longByte,y+7)) ;
      bitWrite(rightByte,y,bitRead(longByte,y)) ;
    }

    // Toggle latch to LOW to write new bits yo 74HC595's
    digitalWrite(latchpin, LOW);

    // Write two bytes
    shiftOut(datapin, clockpin, leftByte);
    shiftOut(datapin, clockpin, rightByte);
    
    // Toggle latch to HIGH to show new bits    
     digitalWrite(latchpin, HIGH);    
     
    delay(7) ;
    delay(2000) ;
   }
   
   
   
   Serial.println(" ---- disablesword done " ) ; 

  
}


}

And this is the result Youtube example

I already use a 100nF capacitor between pin 8 and 16 on the 74HC595,
tried a push-pull linedriver as suggested here.

Currently have no ideas left to get this working more stable.The debug output prints the expected enabled leds, and when the leds are on, the correct number of leds are active.

ByteBits: 0000000000000001
ByteBits: 0000000000000011
ByteBits: 0000000000000111
ByteBits: 0000000000001111
ByteBits: 0000000000011111
ByteBits: 0000000000111111
ByteBits: 0000000001111111
ByteBits: 0000000011111111
ByteBits: 0000000111111111
ByteBits: 0000001111111111
ByteBits: 0000011111111111
ByteBits: 0000111111111111
ByteBits: 0001111111111111
ByteBits: 0011111111111111
ByteBits: 0111111111111111
ByteBits: 1111111111111111
---- enablesword done
---- disablesword
ByteBits: 1111111111111111
ByteBits: 0111111111111111
ByteBits: 0011111111111111
ByteBits: 0001111111111111
ByteBits: 0000111111111111
ByteBits: 0000011111111111
ByteBits: 0000001111111111
ByteBits: 0000000111111111
ByteBits: 0000000011111111
ByteBits: 0000000001111111
ByteBits: 0000000000111111
ByteBits: 0000000000011111
ByteBits: 0000000000001111
ByteBits: 0000000000000111
ByteBits: 0000000000000011
ByteBits: 0000000000000001
---- disablesword done

Any ideas, suggestions that could help ??

Cheers,
Peter

http://fritzing.org/projects/nightrider-with-arduino-16-leds-and-two-74hc595

or Introduction to 74HC595 shift register - Controlling 16 LEDs - Protostack

Perhaps you can use some of it here???

Hi,

You are not using shiftOut() correctly. It should have 4 parameters.

Also have a look at lowByte() and highByte() to make your code simpler.

Paul

@gonnie08

Thanks for the links, but this article has much more detail ;

http://www.elcojacobs.com/using-shiftpwm-to-control-rgb-leds-with-arduino/

PaulRB:
Hi,

You are not using shiftOut() correctly. It should have 4 parameters.

Also have a look at lowByte() and highByte() to make your code simpler.

Paul

Thanks Paul,

I probably used an old version of the syntax of shiftout() because it was still accepted and seems to use MSBFIRST when only 3 arguments are supplied. Do not notice any difference when using 4 arguments with MSBFIRST or only three arguments.

Thanks for the tip on optimizing the code using lowByte() and highByte(), saves some bytes :slight_smile:

Finally found the issue... seems that i forgot one of the two 5v lines on one of the shift registers. :blush: