I've got a TI CD4021BE hooked up and functioning on an UNO with 8 buttons attached to it. The data received from the buttons comes in as 10 for button one, 100 for button 2, etc. Or, 110 if buttons 1 & 2 are pressed at the same time.
I'm wanting to get an output of the entire set of buttons, kind of like an array ( [10011001] } or something so that I can see which buttons are pressed during each clock pulse. I just can't figure out how to format that correctly to get that to happen.
I'm getting some occasional 1s popping out of it. I'm assuming that is some noise, since I don't have a capacitor hooked up. Hence the cavoite to the If statement there. Any advise of how to get all the values?
That's up to you. It is already formatted, so to speak, it's an 8 bit byte.
There are as many ways of dealing with that as there are programmers. <- hyperbole.
Often you will see bit testing, like to check for switch N if you number the switches 0..7 you could
if (RegisterValue & (1 << N)) {
/* switch N is pressed! /
/ do whatever! */
}
But what means "something so that I can see which buttons are pressed during each clock pulse"? ShiftIn does all the clocking and returns the entire register… printing it in BIN format will show you what buttons are pressed each time through the loop().
Take the cavoite off you Serial.println and just let the result spin up the screen whilst you play with the buttons.
BTW your latch is up for an eternity, it's your time to waste but take a look at the CD4021 data sheet. Just sayin'.
The bulk of the code is just copied and pasted from others, so I'm just building on their foundation. That's where I got the 20ms from for the latch.
I'd just like to see the data represented as an array. It's gotta be either so simple that nobody want's to say it, or so freakin hard that nobody wants to say they don't know how.
How do I make this: 110 Look like this: 00000011 ???
Right then. Using Alto777's method (thank you!) this is what I'm now getting:
100000010 (for button 1)
100000100 (for button 2)
100001000 (for button 3)
100010000 (for button 4)
100100000 (for button 5)
101000000 (for button 6)
110000001 (for button 7)
Button 8 is not giving me anything at all. Going by the schematic that I now have up above, the button is hooked up to Q8. Which should, I think, be a viable pin to use for another input. Am I wrong on that? Looks like it is on the data sheet. Am I off a pin somewhere? Pin 1 is giving me a 10, not a 1. Is there something I missed?
Oh, my shipment of buttons came in this afternoon, so now I have all 8 hooked up. Probably should have mentioned that.
Updated code:
int dataPin = 9;
int clockPin = 7;
int latchPin = 8;
byte RegisterValue = 0;
void setup() {
Serial.begin(9600);
pinMode(dataPin, INPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop() {
digitalWrite(latchPin,1);
delayMicroseconds(15);
digitalWrite(latchPin,0);
RegisterValue = shiftIn(dataPin, clockPin, MSBFIRST);
int kk = 0x100 + RegisterValue;
if (RegisterValue > 0) {
Serial.println(kk, BIN);
//Serial.println(RegisterValue, BIN);
}
delay(200);
}
Edit - need more time to look at this, meanwhile this is interesting from the shiftIn() docs,
"If you’re interfacing with a device that’s clocked by rising edges, you’ll need to make sure that the clock pin is low before the first call to shiftIn(), e.g. with a call to digitalWrite(clockPin, LOW)."
My 8th button did not want to work, and I couldn't figure out why. Turns out that you do indeed need to set the clock HIGH between the latch high and lows to get the proper reading of all 8 buttons. The web sites I've seen this on say it may be some sort of a glitch in the library or something, but this works and gets all 8 buttons working the way I wanted them to. Other than adding the first 1 to the output, but I can find a way to deal with that.
Good Stuff!
int dataPin = 9;
int clockPin = 7;
int latchPin = 8;
byte RegisterValue = 0;
void setup() {
Serial.begin(9600);
pinMode(dataPin, INPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop() {
digitalWrite(latchPin,1);
digitalWrite(clockPin, 1);
delayMicroseconds(20);
digitalWrite(latchPin,0);
RegisterValue = shiftIn(dataPin, clockPin, MSBFIRST);
int kk = 0x100 + RegisterValue;
if (RegisterValue != 0) {
Serial.println(kk, BIN);
//Serial.println(RegisterValue, BIN);
}
delay(250);
}