Go Down

Topic: Problems with bitWrite (Read 981 times) previous topic - next topic


I thought that it would turn be like if x is 101 in binary, then A0 would be high, A1 would be low, and A2 would be high.

Yes it would but you said it toggled the bits, that is not what you described above. Also note that as you never set these pins to be outputs then you will never see anything coming out.

Have you tried, as a test, replacing the
Code: [Select]
bitWrite(KMAP, x, digitalRead(in));
Code: [Select]
bitWrite(KMAP, x, 1);


I just did so, uploaded to the board, and it still gives me 0 in KMAP and "All conditions are false." in BOOL.  I've also tried moving the digitalRead outside of the function like so:
Code: [Select]
byte n = digitalRead(in);
bitWrite(KMAP, x, n);

Which did not work either.


You have a global variable named KMAP and a local variable, in READ() called KMAP. It is the local variable that you are manipulating. Which one are you printing? Clearly not the one in READ() as there are no Serial.print() statements there. It is not the local one you are printing outside of READ().


Thanks a bunch, I didn't realize that.  Now that I've removed the byte in front of KMAP in READ() it starts working correctly, thank you!


Was that supposed to toggle the pins one at a time?  If x =3 youre actually turning on two pins
  When x=7 three pins.  If you want to go through the pins one at a time then you need to write 1,2,4,8,16,32,64, 128 to the port, not 1,2,3,4,5,6,7,8.

Go Up