digitalRead causes Arduino Mega crash?

Hey, I’ve got the following code running on an Arduino Mega 2560:

const int outputPins[] = {
  2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 22, 23, 24, 25
};
const int inputPins[] = {
  46, 47, 48, 49, 50, 51, 52, 53
};

void setup() {

  Serial.begin(9600);

  for(int i = 0; i < sizeof(outputPins); i++) {
    pinMode(outputPins[i], OUTPUT);
  }

  for(int i = 0; i < sizeof(inputPins); i++) {
    pinMode(inputPins[i], INPUT);
  }
  
  Serial.println(readGBAddress(0x134));
  
}

void loop() {
  
  Serial.println("TEST!");
  digitalWrite(LED_BUILTIN, HIGH);

}

byte readGBAddress(unsigned int address) {
  for(int i = 15; i >= 0; i++) {
    if(address > (1 << i)) {
      digitalWrite(outputPins[i], HIGH);
    } else {
      digitalWrite(outputPins[i], LOW);
    }
  }
  delay(100); // 1/10 seconds
  byte data = 0x00;
  for(int i = 7; i >= 0; i++) {
    if(digitalRead(inputPins[i]) == HIGH){
      data += (1 << i);
    }
  }
  return data;
}

For some reason, I get no output on the serial monitor and the built-in LED doesn’t light up… Is readGBAddress causing some sort of crash?

const int outputPins = {

for(int i = 0; i < sizeof(outputPins); i++) {

sizeof(int) == 2

Okay, I’ve edited the code to not use sizeof() anymore! But it still doesn’t work…

Interestingly, now that I’ve switched Serial.println("TEST!"); to before when I call readGBAddress, the serial output monitor displays “TE” and nothing more. Also, the built-in LED still doesn’t come on.

const int outputPins[] = {
  2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 22, 23, 24, 25
};
const int inputPins[] = {
  46, 47, 48, 49, 50, 51, 52, 53
};

void setup() {
  Serial.begin(9600);

  for(int i = 0; i < 16; i++) {
    pinMode(outputPins[i], OUTPUT);
  }

  for(int i = 0; i < 8; i++) {
    pinMode(inputPins[i], INPUT);
  }
}

void loop() {
  Serial.println("TEST!");
  Serial.println(readGBAddress(0x134));
  digitalWrite(LED_BUILTIN, HIGH);
}

byte readGBAddress(int address) {
  for(int i = 15; i >= 0; i++) {
    if(address > (1 << i)) {
      digitalWrite(outputPins[i], HIGH);
      address -= (1 << i);
    } else {
      digitalWrite(outputPins[i], LOW);
    }
  }
  delay(100); // 1/10 seconds
  byte data = 0x00;
  for(int i = 7; i >= 0; i++) {
    if(digitalRead(inputPins[i]) == HIGH){
      data += (1 << i);
    }
  }
  return data;
}

Sorry, this is probably just a daft mistake on my part.

  for(int i = 15; i >= 0; i++) {
  for(int i = 7; i >= 0; i++) {

Ups.

:o

I'm such an airhead... Thanks a million! It works like a charm!