hello guys,
i am working with CD4021B shift in IC and i found it really great with no hardware problems, i tried the sketches from the tutorial here on this site and it works totally fine and i get to see the ZEROES and ONES right in place. So what i need is to understand well the codes, i managed to get the idea of the first example Hello World except these couple of lines here :
(here is the link for the example i am referring to for now because there is 3 more examples later : https://www.arduino.cc/en/Tutorial/ShftIn11
byte switchVar1 = 72; //01001000
Question 1: i did not understand why do we have to start by 72 and 0, it says in the comments in help troubleshooting well i don't see how it helps and does this mean i can start by 0 ?
Question 2: why did we put these 2 lines here in the shiftIn() function ? it's already done in the setup function !
for (i=7; i>=0; i--)
{
digitalWrite(myClockPin, 0);
delayMicroseconds(0.2);
temp = digitalRead(myDataPin);
if (temp) {
pinState = 1;
//set the bit to 0 no matter what
myDataIn = myDataIn | (1 << i);
}
else {
//turn it off -- only necessary for debuging
//print statement since myDataIn starts as 0
pinState = 0;
}
Question 3: in this for loop, i can't seem to get how myDataIn = myDataIn OR (1<< i); seem to set the bit to 0 no matter what !! can anyone explain please ? from what i know is that 1<<i is shifting the 1 i times to the left that clear and myDataIn is the bit coming from the data pin of the IC after Reading it so from what i learned about OR is that if data Pin is 1 and (1 << i) is 0 the bit will be 1 no matter what because one of 2 bits must be 1 if both are 0 then the result is 0 so how come we set the bit to 0 if 1 will make it 1 !!
example to start from for the | operator :
myDataPin: 00000001
(1 << 1): 00000010
result : 00000011
It is a terribly written program, but it might be working. If you're not seeing 1's clocked in, you might have a hardware problem. If you are trying to learn from the program, it would be better to find a better written example.
aarg:
It is a terribly written program, but it might be working. If you're not seeing 1's clocked in, you might have a hardware problem. If you are trying to learn from the program, it would be better to find a better written example.
hahahahaha ! as i mentioned before this program is NOT MINE ! it's from THIS WEBSITE it's an example from the tutorial section arduino.cc !
there is NO HARDWARE PROBLEM AS I SAID. Everything works fine and i see the 1s when i click the buttons but i need to understand the program written to be able to make my own
firashelou:
hahahahaha ! as i mentioned before this program is NOT MINE ! it's from THIS WEBSITE it's an example from the tutorial section arduino.cc !
there is NO HARDWARE PROBLEM AS I SAID. Everything works fine and i see the 1s when i click the buttons but i need to understand the program written to be able to make my own
That's good. What do you want your program to do?
All you can learn from a poorly written program, is how to program poorly (at least, in the beginning). That is my point.
"Question 1:
i did not understand why do we have to start by 72 and 0, it says in the comments in help troubleshooting well i don't see how it helps and does this mean i can start by 0 ?"
He's assigning a known value for troubleshooting. If it was 0x00 or 0xFF it would be "hard" to see an error right away (maybe). So he's looking for something identifiable to start with. If that mashes out wrong, then he/you can say " :-S " and "Now what?"
(I like to use 0x66, 0x55, 0xAA).
but i don't get it troubleshoot what ?! because when i plug in the arduino which is connected to the circuit of the IC with buttons what happened is that i see 0 or the monitor but i click a button then the 1 will appear in the right place
Ah, sorry! I believe question #1 and #2 have been answered. Your question #3 is hard to understand. Can you clarify please?
The code 1.1 contains this, which has a confusing or misguided comment:
if (temp) {
pinState = 1;
//set the bit to 0 no matter what
myDataIn = myDataIn | (1 << i);
}
which to my way of thinking should say:
if (temp) {
pinState = 1;
//set the i'th bit to 1
myDataIn = myDataIn | (1 << i);
}
...or something like that.
Demonstrating what I said earlier, that it's hard to learn from someone who is confused.
aarg:
Ah, sorry! I believe question #1 and #2 have been answered. Your question #3 is hard to understand. Can you clarify please?
The code 1.1 contains this, which has a confusing or misguided comment:
if (temp) {
pinState = 1;
//set the bit to 0 no matter what
myDataIn = myDataIn | (1 << i);
}
which to my way of thinking should say:
if (temp) {
pinState = 1;
//set the i'th bit to 1
myDataIn = myDataIn | (1 << i);
}
...or something like that.
Demonstrating what I said earlier, that it's hard to learn from someone who is confused.
well exactly that's what i am asking about !! seems that you had answered the question !! it is setting the bit to 1 no matter what because this exact bit is originally 0
that is how i understand it :
if (temp == HIGH) {
pinState = 1; //the pin state is high which will help later for choosing which LED to set ON ??
//set the i'th bit to 1
myDataIn = myDataIn | (1 << i);
}
about question 1 and 2 no it hasn't been answered yet
aarg:
Question #1 was answered in reply #9.
Question #2 can be answered by considering the poor quality of the code. It's a mistake!
ok about question 2
about question 1, i replied after that i didn't really get it ! i mean i see 0s when i start the monitor and after i click the tactile button it turns to 1 of course the right bit but i can't understand what does the 72 do here