i.e. digital pin 53 on the arduino is represents the MSB, and pin 23 represents the LSB
I simply want to print out the address on those pins every time I access a new address on a RAM chip I have on the breadboard.
PROBLEM: This is the for loop I have programmed to do so:
unsigned int address = 0;
// prints the address on the address pins to Serial Monitor
for (int i = sizeof(ADDR_PINS)-1; i >= 0 ; i--){
int bit = digitalRead(ADDR_PINS[i]) ? 1: 0 ;
Serial.print(bit);
address = (address << 1) + bit;
}
It should iterate through addresses one by one from 0x0000 and print the address bits out bit by bit to the serial monitor (I call this for loop from a different simple function).
However, I am encountering the issue that the LSB never changes and is constantly 0. This means only addresses with even numbers are being printed, and I can't figure out why. I have tried changing the limit of i in the for loop condition to
i > 0
which resolves the LSB problem, but then causes the exact same to happen to the MSB.
You are starting at the LSB end (23) and ending with the MSB end (53) and shifting all the previous bits left before adding the new bit. That will put Pin 23 at the MSB and Pin 53 at the LSB.
Try: for (size_t i = 0; i < sizeof ADDR_PINS; i ++) {