# Redeclaration

Hello,

I’m working on a code where you transform a 4x4 matrix whit 16 random numbers to allot more random numbers whit a button(number of times). Don’t ask me why, I just came with a stupid idea hehe.
This is the code:

``````const int button = 10;
boolean on = false;
int buttonState =0;
int buttonPresses = 0;
const int numRows = 4;
const int numCols = 4;
const int debounceTime = 20;
const  char keymap1[numRows][numCols] = {
{'A', 'B', 'C', 'D'},
{'E', 'F', 'G', 'H'},
{'I', 'J', 'K', 'L'},
{'M', 'N', 'O', 'P'},
};
const  char keymap2[numRows][numCols] = {
{'Q', 'R', 'S', 'T'},
{'U', 'V', 'W', 'X'},
{'Y', 'Z', ' ', ' '},
{' ', ' ', ' ', ' '},
};
const  char keymap3[numRows][numCols] = {
{'a', 'b', 's', 'd'},
{'e', 'f', 'g', 'h'},
{'i', 'j', 'k', 'l'},
{'m', 'n', 'o', 'p'},
};
const  char keymap4[numRows][numCols] = {
{'q', 'r', 's', 't'},
{'u', 'v', 'w', 'w'},
{'x', 'y', 'z', ' '},
{' ', ' ', ' ', ' '},
};
const  char keymap5[numRows][numCols] = {
{'1', '2', '3', '4'},
{'5', '6', '7', '8'},
{'9', '+', '-', '*'},
{'/', ' ', ' ', ' '},
};
const int rowPins[numRows] = {2, 3, 4, 5};
const int colPins[numCols] = {6, 7, 8, 9};
char getKey1(){
char key = 0;
for (int column = 0; column < numCols; column++){
digitalWrite(colPins[column], LOW);
for (int row = 0; row < numRows; row++){
delay(debounceTime);
key = keymap1[row][column];
}
}
digitalWrite(colPins[column], HIGH);
}
return key;
}
char getKey2(){
char key = 0;
for (int column = 0; column < numCols; column++){
digitalWrite(colPins[column], LOW);
for (int row = 0; row < numRows; row++){
delay(debounceTime);
key = keymap2[row][column];
}
}
digitalWrite(colPins[column], HIGH);
}
return key;
}
char getKey3(){
char key = 0;
for (int column = 0; column < numCols; column++){
digitalWrite(colPins[column], LOW);
for (int row = 0; row < numRows; row++){
delay(debounceTime);
key = keymap3[row][column];
}
}
digitalWrite(colPins[column], HIGH);
}
return key;
}
char getKey4(){
char key = 0;
for (int column = 0; column < numCols; column++){
digitalWrite(colPins[column], LOW);
for (int row = 0; row < numRows; row++){
delay(debounceTime);
key = keymap4[row][column];
}
}
digitalWrite(colPins[column], HIGH);
}
return key;
}
char getKey5(){
char key = 0;
for (int column = 0; column < numCols; column++){
digitalWrite(colPins[column], LOW);
for (int row = 0; row < numRows; row++){
delay(debounceTime);
key = keymap5[row][column];
}
}
digitalWrite(colPins[column], HIGH);
}
return key;
}
void setup(){
pinMode(10, INPUT);
Serial.begin(9600);
for (int row = 0; row < numRows; row++){
pinMode(rowPins[row], INPUT);
digitalWrite(rowPins[row], HIGH);
}
for (int column = 0; column < numCols; column++){
pinMode(colPins[column], OUTPUT);
digitalWrite(colPins[column], HIGH);
}
}

void loop(){
if(digitalRead(button) == HIGH){ // This will check how many times button1 is pressed and save the positions to an array depending on how many times it is pressed; switch/case works like a if statement
buttonPresses++;
switch(buttonPresses){
case 1:
char key = getKey1();
if (key !=  0){
Serial.print(key);
}
break;
case 2:
char key = getKey2();
if (key !=  0){
Serial.print(key);
}
break;
case 3:
char key = getKey3();
if (key !=  0){
Serial.print(key);
}
break;
case 4:
char key = getKey4();
if (key !=  0){
Serial.print(key);
}
break;
case 5:
char key = getKey5();
if (key !=  0){
Serial.print(key);
}
break;
}
}
}
``````

But the error I get is a redeclaration for the ‘char key’ in the void loop(). I taught that the return command it would work, but it doesn’t. Can someone help me understand what’s wrong with this code?
``````  if(digitalRead(button) == HIGH){ // This will check how many times button1 is pressed and save the positions to an array depending on how many times it is pressed; switch/case works like a if statement