Dynamic Array of Integers

I am currently trying to make a small project where I read 16 inputs with 8 pins; 4 inputs, 4 outputs. Here is my plan:
Set the array to all zeros (sixteen of 'em). Scan four buttons, storing their state in the array. Repeat for the next three buttons. Print their state on one line in the serial monitor (print the array). Print a new line. Wait 500ms (half a second), repeat.

My code compiles and uploads. But when I look at the serial monitor, it always prints straight zeros on each line. This leads me to believe that my array is not updating. I've checked my wiring, the code, everything.

void setup(){
  for(int z=2;z<6;z++){
    pinMode(z,INPUT);
  }
  for(int z=8;z<12;z++){
    pinMode(z,OUTPUT);
  }
    Serial.begin(9600);
}

void loop(){
  int buttons[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
/*  for(int y=0;y<16;y++){
    buttons[y] = 0;
  }
 */ 
  digitalWrite(2,HIGH); digitalWrite(3,LOW); digitalWrite(4,LOW); digitalWrite(5,LOW);
  if(digitalRead(8)==1){
    buttons[0] = 1;
  }
  if(digitalRead(9)==1){
    buttons[4] = 1;
  }
  if(digitalRead(10)==1){
    buttons[8] = 1;
  }
  if(digitalRead(11)==1){
    buttons[12] = 1;
  }
  
  digitalWrite(2,LOW); digitalWrite(3,HIGH); digitalWrite(4,LOW); digitalWrite(5,LOW);
  if(digitalRead(8)==1){
    buttons[1] = 1;
  }
  if(digitalRead(9)==1){
    buttons[5] = 1;
  }
  if(digitalRead(10)==1){
    buttons[9] = 1;
  }
  if(digitalRead(11)==1){
    buttons[13] = 1;
  }
  
   digitalWrite(2,LOW); digitalWrite(3,LOW); digitalWrite(4,HIGH); digitalWrite(5,LOW);
  if(digitalRead(8)==1){
    buttons[2] = 1;
  }
  if(digitalRead(9)==1){
    buttons[6] = 1;
  }
  if(digitalRead(10)==1){
    buttons[10] = 1;
  }
  if(digitalRead(11)==1){
    buttons[14] = 1;
  }
   digitalWrite(2,LOW); digitalWrite(3,LOW); digitalWrite(4,LOW); digitalWrite(5,HIGH);
  if(digitalRead(8)==1){
    buttons[3] = 1;
  }
  if(digitalRead(9)==1){
    buttons[7] = 1;
  }
  if(digitalRead(10)==1){
    buttons[11] = 1;
  }
  if(digitalRead(11)==1){
    buttons[15] = 1;
  }
  for(int y=0;y<16;y++){
    Serial.print(buttons[y],DEC);
  }
  Serial.println();
  delay(500);
}

I also tried removing the if statements so that the array elements update to the pin state directly.

  digitalWrite(2,HIGH); digitalWrite(3,LOW); digitalWrite(4,LOW); digitalWrite(5,LOW);
//  if(digitalRead(8)==HIGH){
    buttons[0] = digitalRead(8);
//  }
//  if(digitalRead(9)==HIGH){
    buttons[4] = digitalRead(9);
//  }
//  if(digitalRead(10)==HIGH){
    buttons[8] = digitalRead(10);
//  }
//  if(digitalRead(11)==HIGH){
    buttons[12] = digitalRead(11);
//  }

You have your inputs and outputs mixed up.

pfft
What I get for coding at 1AM. But now, it's not storing all the inputs. I can flip some switches and it will show. But if I flip other, it will show less. For example, right now I have two flipped. It is showing "0000000001000010", which is correct. But if I flip another, it will show "0000000001001000" when it should show "0000000001001010". And when it should show "1010000001001010", it shows "0000000001000000". I don't really have schematics right now, but I will create some soon.

How are your pull down resistors connected? Post updated code when you make changes.

Your getting a new copy of buttons (set to 0) each time loop() is called. Make buttons static or global. Look up "scope of var in c/c++".

Mark

holmes4:
Your getting a new copy of buttons (set to 0) each time loop() is called. Make buttons static or global. Look up "scope of var in c/c++".

Mark

I know. It's supposed to reset at the beginning of the loop (and I just realise I can remove that line due to how I'm setting them).

Arrch:
How are your pull down resistors connected? Post updated code when you make changes.

I put them between the switches (which I've been calling buttons) and the Arduino (I'll create schematics later). And I'll post the new code after I re-update it.

void setup(){
  //define the inputs
  for(int z=2;z<6;z++){
    pinMode(z,INPUT);
  }
  //Define the outputs
  for(int z=8;z<12;z++){
    pinMode(z,OUTPUT);
  }
    Serial.begin(9600);
}
int buttons[16];
void loop(){

  //Read the first four inputs
  digitalWrite(8,HIGH); digitalWrite(9,LOW); digitalWrite(10,LOW); digitalWrite(11,LOW);

    buttons[0] = digitalRead(2);

    buttons[4] = digitalRead(3);

    buttons[8] = digitalRead(4);

    buttons[12] = digitalRead(5);

  //Read the next four
  digitalWrite(8,LOW); digitalWrite(9,HIGH); digitalWrite(10,LOW); digitalWrite(11,LOW);

    buttons[1] = digitalRead(2);

    buttons[5] = digitalRead(3);

    buttons[9] = digitalRead(4);

    buttons[13] = digitalRead(5);
 
   //Read the next four
   digitalWrite(8,LOW); digitalWrite(9,LOW); digitalWrite(10,HIGH); digitalWrite(11,LOW);

    buttons[2] = digitalRead(2);

    buttons[6] = digitalRead(3);

    buttons[10] = digitalRead(4);

    buttons[14] = digitalRead(5);
  
   //Read the last four
   digitalWrite(8,LOW); digitalWrite(9,LOW); digitalWrite(10,LOW); digitalWrite(11,HIGH);

    buttons[3] = digitalRead(2);

    buttons[7] = digitalRead(3);

    buttons[11] = digitalRead(4);

    buttons[15] = digitalRead(5);
  //Print the results
  for(int y=0;y<16;y++){
    Serial.print(buttons[y],DEC);
  }
  Serial.println();
  delay(500);
}

EDIT
I just figured out my problem. It's how I wired it. When there is voltage on four switches and I flip one, I put voltage on an input. That's how it should work. But when I flip a switch with no voltage on it, current travels back into the arduino; voltage drops to zero on the inputs.