Shift register and simple buttons

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
sbrooke
 
Posts: 27
Joined: Wed Nov 23, 2011 12:01 pm

Shift register and simple buttons

Post by sbrooke »

I think I know what I need to do but before I go down that path can someone confirm this.

Arduino using ShiftOut to a 595 shift register. The shift register is then driving a ULN2803 which is then driving relays. I want to have multiple buttons attached to the Arduino and when I push button "0" it turns on pin 0 on the output of the shift register. This I have working with no problem. The problem is when I press button "1" on the Arduino I want it to independently turn on pin 1 on the shift register. When I do that now I get results like the second output (which I have an LED on for now) will light but it will also turn off the first LED.

I figure this is because of having to shift out a byte from the Arduino and I think I get the concept of what I need to do. Do I need to account for each of 8x8 possible button states so I can send the correct byte out anytime a button changes? Is there not an easier way for something that's essentially a 1 to 1 relationship? Any examples someone can point me towards?

User avatar
adafruit_support_bill
 
Posts: 88096
Joined: Sat Feb 07, 2009 10:11 am

Re: Shift register and simple buttons

Post by adafruit_support_bill »

You need to keep a copy of the last byte shifted out. When you want to change the state of one output bit, you change just that one bit and shift it out again. Get familiar with the 'bitwise' operators. They will be very helpful here. http://www.cprogramming.com/tutorial/bi ... ators.html

sbrooke
 
Posts: 27
Joined: Wed Nov 23, 2011 12:01 pm

Re: Shift register and simple buttons

Post by sbrooke »

I looked at bitwise but I also went back and reviewed the ShiftOut tutorial. I took a little of both and came up with the following, which seems to work. This is just the modified function:

Code: Select all

// This method sends bits to the shift register:

void registerWrite(int whichPin, int whichState) {
// the bits you want to send
  byte bitsToSend = currentBits;

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

  // Modify the correct bit:
  bitWrite(bitsToSend, whichPin, whichState);

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

  currentBits = bitsToSend;
  
 // turn on the output enable so the LEDs can light up. OE is used to have a consistent state at startup:
  digitalWrite(oePin, LOW);

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

}
bitWrite seems to do the trick for modifying only the bit I want modified. See anything wrong with this?

Thanks!

User avatar
adafruit_support_bill
 
Posts: 88096
Joined: Sat Feb 07, 2009 10:11 am

Re: Shift register and simple buttons

Post by adafruit_support_bill »

Looks good! :D

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”