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);
// }