Go Down

Topic: Trouble with Shiftout tutorial - Keeping previous LED's lit (Read 1 time) previous topic - next topic

ScooterinAB

I've been going through the 595 Shift Register tutorial 1.3 (http://arduino.cc/en/Tutorial/ShftOut13), and I've hit a wall.

I'm trying to get the previously lit LEDs to stay on (at least until the end of the 16 passes, when it will clear), so that there is a growing row of LEDs instead of just a single lit one. I've gone through every line of code in the example, and cannot find out where it is turning the previous LED's off before turning on the next.

Can anyone help me out? I'm still learning, so I'm having some trouble following the code in the example.

Thanks.

cmiyc

Code: [Select]
   // if this is not the first LED, turn off the previous LED:
   if (thisLed > 0) {
     registerWrite(thisLed - 1, LOW);
   }


Looks like this is the code that turns off the previous LED...
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

ScooterinAB

When testing it, I completely removed that section, and even rewrote the entire void loop. Oddly, it didn't seem to change anything.

cmiyc


When testing it, I completely removed that section, and even rewrote the entire void loop. Oddly, it didn't seem to change anything.

It helps if you post the code you as you rewrote it.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

ScooterinAB

Here's what I'm using. I removed the header comments, because they aren't needed for troubleshooting.

As I mentioned above, I removed the aforementioned section of code during a rewrite. Oddly, it does not seem to have done anything either way. This leads me to suspect that the code that clears the previous LEDs before moving on is somewhere else.

Code: [Select]
//Pins connected to 74HC595
const int dataPin = 4;
const int clockPin = 5;
const int latchPin = 6;

void setup()
{
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT); 
  pinMode(clockPin, OUTPUT);
}

void loop() {
  // iterate over the 16 outputs of the two shift registers
  for (int thisLed = 0; thisLed < 16; thisLed++)
  {
    // write data to the shift registers:
    registerWrite(thisLed, HIGH);
    // pause between LEDs:
    delay(250);
  }

}

// This method sends bits to the shift registers:

void registerWrite(int whichPin, int whichState)
{
  // the bits you want to send. Use an unsigned int,
  // so you can use all 16 bits:
  unsigned int bitsToSend = 0;   

  // turn off the output so the pins don't light up
  // while you're shifting bits:
  digitalWrite(latchPin, LOW);

  // turn on the next highest bit in bitsToSend:
  bitWrite(bitsToSend, whichPin, whichState);

  // break the bits into two bytes, one for
  // the first register and one for the second:
  byte registerOne = highByte(bitsToSend);
  byte registerTwo = lowByte(bitsToSend);

  // shift the bytes out:
  shiftOut(dataPin, clockPin, MSBFIRST, registerTwo);
  shiftOut(dataPin, clockPin, MSBFIRST, registerOne);

  // turn on the output so the LEDs can light up:
  digitalWrite(latchPin, HIGH);
}



Ultimately, I'd be ok with a way to control individual LEDs, as there is a program I will be writing in the future based on this one that needs to light specific LEDs during the sequence. But any result is fine, as I can cross that bridge when I get to it.

Thanks.

cmiyc

Looking a little more closely at the code, it is because of how registerWrite works.

You send it a pin number and a state.  It create a variable called "bitsToSend" and sets the appropriate bit of that variable based on the pin you've asked to change.  See the problem?  Every time that function is called, bitsToSend is reset to zero.  There is no memory of which bit was set on the last call.

One approach would be to make bitsToSend a global variable so that it retains its value.  Another would be to declare it as static.  (The former is probably easier to do and understand for now.)
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

ScooterinAB

I see what you mean now about the variable resetting to zero. Being green, I didn't notice it until now.

I moved bitsToSend to the start of the program to make it a global variable. It looks like it's achieving the effect I'm looking for, although the first LED to be lit is from the second Shift Reg (weird). Easy enough to rewire.

Thanks a bunch for your help, and mad quick responses.

Go Up