output controlling using 74HC595 help

hi all, help needed here. I’m using a 74HC595 serial to parallel shift register.
How to maintain a state/status of a pin?

a scenario here:
Step 1: after Button 1(as in comment in the code) was pressed Green LED lights up.
Step 2: then Button 3 was pressed and Red LED lights up but at the same time Green LED turn LOW.

I want to maintain the Green LED lights up until i press the Button 2 which turn it off.
But the problem is it will turn off automatically after Button 3 was pressed.

//............initialization code goes here....
const int latchPin = 8;
const int clockPin = 12;
const int dataPin = 11;

void setup()
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
}

void loop()
{	
   char button = 0;
  //............code goes here....
  //............code goes here.... 
  
     if(strcmp(button, "OnGreenLED")  == 0)        // <------------- Button 1 
     {
       registerWrite(0, HIGH);
     } 
     else if(strcmp(button, "OffGreenLED")  == 0)  // <------------- Button 2
     {
       registerWrite(0, LOW);
     }
     else if(strcmp(button, "OnRedLED")  == 0)     // <------------- Button 3
     {
       registerWrite(1, HIGH);
     } 
     else if(strcmp(button, "OffRedLED")  == 0)    // <------------- Button 4
     {
       registerWrite(1, LOW);
     }
  
}

void registerWrite(int whichPin, int whichState) {
// the bits you want to send
  byte 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);

  // shift the bits out:
  shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);

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

Move the

byte bitsToSend = 0;

declaration to top-level (outside of any function).

Variables declared inside a function only exist within the call to that function and get initialized every time it is called. Here you want its state to persist globally. Putting it at top level means it is global in extent.

thanks MarkT… it runs perfectly after make the variable global…
one more problem comes… which is after i put the capacitor 0.1uF between latch pin and ground pin… the output pin other than pin 0,1 will goes high as well or sometimes all the logic data will shift one step forward after when the serial port was closed…
but it goes well and perfect with no capacitor on the latch pin…
why the capacitor make things go wrong?
i refer the capacitor info from
http://processors.wiki.ti.com/index.php/MSP430_Launchpad_Shift_Register
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1260665365
it says : << 1 Capacitor of .1uF (may optionally be added between the latch pin and ground to stabilize the signal) >>
but mine goes weird… why??

void loop()
{
char button = 0;

while(Serial.available()>0) // <--------after closed then weird output comes with the existence of capacitor


if(strcmp(button, “OnGreenLED”) == 0) // <------------- Button 1
{
registerWrite(0, HIGH);
}
else if(strcmp(button, “OffGreenLED”) == 0) // <------------- Button 2
{
registerWrite(0, LOW);
}
else if(strcmp(button, “OnRedLED”) == 0) // <------------- Button 3
{
registerWrite(1, HIGH);
}
else if(strcmp(button, “OffRedLED”) == 0) // <------------- Button 4
{
registerWrite(1, LOW);
}

}

Comparing a string to a single char doesn't make any sense.
What are you trying to do with the strcmp?

Hi AWOL,
actually in the code i've done some comparison from the String/Byte where i retrieve from serial port.. But i didn't post it out because it will causing the question become mess-up and confusing.. so i throw away all those unrelated code and post a simple version here.. Thanks..

So, in order to reduce confusion, you introduced more confusion.
Fascinating.

so sorry… i will post again next time…

JPgs:
thanks MarkT… it runs perfectly after make the variable global…
one more problem comes… which is after i put the capacitor 0.1uF between latch pin and ground pin… the output pin other than pin 0,1 will goes high as well or sometimes all the logic data will shift one step forward after when the serial port was closed…
but it goes well and perfect with no capacitor on the latch pin…
why the capacitor make things go wrong?
i refer the capacitor info from
http://processors.wiki.ti.com/index.php/MSP430_Launchpad_Shift_Register
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1260665365
it says : << 1 Capacitor of .1uF (may optionally be added between the latch pin and ground to stabilize the signal) >>
but mine goes weird… why??

That looks like bogus advice to me, its just a logic signal.

You shouldn’t load an Arduino output pin with a large capacitive load like this, you will be overloading the output pin’s transistors. You do need lots of decoupling on the power rail directly at the HC595 though - 0.1uF plus 10uF there would be very useful. When switching high current loads very fast (logic speeds) you must have adequate decoupling on every device that switches current.

I am doing experiments with the 74HC595 right now.

  1. No cap at data-in, latch and clock. I did not use it…guess what… mine work fine. Except a cap of 0.1 uf at VCC and gnd of the chip.

  2. The /MR pin, connect to reset of the Ardiuno. It is a reset for the 74HC595, a neg pulse will reset it, also the arduino chip need a neg pulse to reset it. Without a reset signal to the chip, it may not work properly. I lean that using a counter chip 74LS90. Flip-Flop need to be reset properly.

Edit:
3. Optional…connect the /OE to a digital pin. or connect the /OE to gnd. ← edit: +5 v to gnd

  1. The MSB is Out 0 and the LSB is Out 7.

My test code was from the IDE → help–> reference → shiftOUT(). I just change the pin number to match my connection. I connect a limiting resistor of 330 at every output ( out 0 to out 7 ) connect to led. Run the code, work fine.

Right now, I am trying to use it with 3 digit 7 segment display. Later I will use it on a 8 X 8 led matrix ( red, green )

Thanks all, it works perfectly now... the only thing I'm not sure why after a capacitor placed between latch pin and ground pin will cause undesired output only if when i on and off serial port... only when dealing with serial port... otherwise... it works perfectly...
BTW... i'd remove the capacitor.. thanks all... thanks Techone, MarkT, AWOL... thanks for your time... truly appreciate it...

Oh ya... BTW... does the 74HC595 has total current limitation for driving output load ? Current 40mA per pin on Arduino I/O port... then how about current on I/O pin for 74HC595 ?