Newbie with shift register in need of help.

Hi all, What i have is 2 shift registers connected together via the serial out pin of the first, i am using them to control 16 leds.

if( (recieve == '4') && (state==3) ) { digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, MSBFIRST, 240); shiftOut(dataPin, clockPin, MSBFIRST, 252); digitalWrite(latchPin, HIGH);

"4" is the number that i am sending through the serial box and "state" is a variable.

When all conditions are met the code runs but the is a significant delay between the leds connected to the 1st shift register and the second. 6 leds connected the 1st light up immediatly but the 4 leds connected the 2nd shift register take about 5-10 seconds to turn on.

Is there something i can do to fix this delay, thanks.

Please paste the complete code in code tags!

You can find a very good library and tutorials on bildr.org
This can surely help you out.

Cheers

If you swap the 2 shift register chips does it do anything different? If yes, likely a bad chip. If no, likely a wiring issues with the LEDs or2nd shift register.

Have you put the 1uF capacitor on the latch line like the tutorial says? If so remove it as it is wrong. Put it instead across the power and ground.

However a 5 second delay is not likely to be hardware it is likely to be something in the software. Try cutting down the code to the simplest you can get that shows the fault and post that.

here is my code:

int latchPin = 2;
int clockPin = 4;
int dataPin = 7;
int recieve;
int state =0;    // lighting state
int pwm_a = 3;  
int pwm_b = 11;  
int dir_a = 12;  
int dir_b = 13;
int motortime = 10;
int duration;                  
int distance;                     
int srfPin = 8;
int lightPin = 0; 

void setup() {

  Serial.begin(9600);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(pwm_a, OUTPUT);  
  pinMode(pwm_b, OUTPUT);
  pinMode(dir_a, OUTPUT);
  pinMode(dir_b, OUTPUT);
  digitalWrite(latchPin, LOW);
  pinMode(srfPin, OUTPUT);
  

    
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 240);
    shiftOut(dataPin, clockPin, MSBFIRST, 0);  
    digitalWrite(latchPin, HIGH);
  

}

void loop() {

  if( Serial.available()) {
    recieve = Serial.read(); 
  }

  //-----Coding for headlights-----

  if( recieve == '1' ) {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 240);
  shiftOut(dataPin, clockPin, MSBFIRST, 252);  
    digitalWrite(latchPin, HIGH);
    state = 1; // value signifys all lights on
  }

  if( recieve == '2' ) {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 240);
  shiftOut(dataPin, clockPin, MSBFIRST, 0);  
    digitalWrite(latchPin, HIGH);
    state = 2; 
  }

  if( recieve == '3' ) {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 240);
    shiftOut(dataPin, clockPin, MSBFIRST, 254);
    digitalWrite(latchPin, HIGH);
    state = 3; // value signifys high beam is on
  }

  if( (recieve == '4') && (state==3) ) {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 240);
    shiftOut(dataPin, clockPin, MSBFIRST, 252); 
    digitalWrite(latchPin, HIGH);
    state = 4; 
  }

  //------Coding for motors----- 

  if( recieve == '6' ) {
    digitalWrite(dir_a, LOW);  
    digitalWrite(dir_b, LOW);  
    analogWrite(pwm_a, 255);  
    analogWrite(pwm_b, 255);
    delay(motortime); 
  }
  
  if( recieve == '7' ) {
    digitalWrite(dir_a, HIGH);  
    digitalWrite(dir_b, HIGH);  
    analogWrite(pwm_a, 255);  
    analogWrite(pwm_b, 255);
    delay(motortime); 
  }
  
  if( recieve == '8' ) {
    digitalWrite(dir_a, LOW);  
    digitalWrite(dir_b, HIGH);  
    analogWrite(pwm_a, 255);  
    analogWrite(pwm_b, 255);
    delay(motortime); 
  }
  
  if( recieve == "9'' ) {
    digitalWrite(dir_a, HIGH);  
    digitalWrite(dir_b, LOW);  
    analogWrite(pwm_a, 255);  
    analogWrite(pwm_b, 255);
    delay(motortime); 
  }
  
  
  
//-----Coding for indicator lights left-----

if( (recieve == '8') && (state ==1) ) {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 252);   
    shiftOut(dataPin, clockPin, MSBFIRST, 252);  
    digitalWrite(latchPin, HIGH);
    delay(100);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 240);    
    shiftOut(dataPin, clockPin, MSBFIRST, 252);  
    digitalWrite(latchPin, HIGH);

i was able to get the:

  if( (recieve == '4') && (state==3) ) {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 240);
    shiftOut(dataPin, clockPin, MSBFIRST, 252); 
    digitalWrite(latchPin, HIGH);
    state = 4;

part to work without delaying by swapping the shift registers. but im still having trouble with the

if( (recieve == '8') && (state ==1) ) {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 252);   
    shiftOut(dataPin, clockPin, MSBFIRST, 252);  
    digitalWrite(latchPin, HIGH);
    delay(100);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 240);    
    shiftOut(dataPin, clockPin, MSBFIRST, 252);  
    digitalWrite(latchPin, HIGH);

part of the code, i has hoping the have 2 the leds connected to shift register 2 flash quickly but they stay on permanently.

Thanks.

240 = 1111 0000 252 = 1111 1100

So the LEDs would start at 11111100 11110000 from the earlier shift?

And then the later shifts happen: First pair of shift outs result in 11111100 11111100 and then 100mS later 11111100 11110000

So if these are LED 7-0, you want 3 & 2 to blink on? On = 1?

Code looks correct. You don't have any capacitors on any control lines, yes?

No I don't have any capacitors on the control lines.

Yes I want led 2 and 3 to blink if state==1.

Can you put in some Serial.prints to ensure that the if( (recieve == '8') && (state ==1) ) is actually being met?

Ok tried this:

if( (recieve == '8') && (state == 1) ) {
    Serial.print("conditions met!");
  digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 252);   
    shiftOut(dataPin, clockPin, MSBFIRST, 252);  
    digitalWrite(latchPin, HIGH);
    delay(100);
    Serial.print("delayed");
     digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 240);    
    shiftOut(dataPin, clockPin, MSBFIRST, 252);  
    digitalWrite(latchPin, HIGH);
  }

And "conditions met!" and "delayed" we printed over and over and led 2 and 3 slashed continually.

I was wanting the leds to flash just once. Any ideas?

As part of the condition met code, reset the the 'recieve' value (and possibly the state?) so it is not valid again until the next Serial.read.

Ah ha! that worked perfectly! Thanks for Help!

Cool.