I see several issues with your code.
First one is the issue of "scope". If you define the int a[]
inside the switch
it will be destroyed as soon as the switch
will finish and no code that runs outside of its brackets will be able to access it.
I also think that array of integers
is the wrong choice here. It would be much less memory consuming to use array of boolean
that mean if each LED should be lit or not. You can use it later like that:
if (a[4]) { digitalWrite(LED4, HIGH); } else { digitalWrite(LED4, LOW); }
or even better using "ternary operator":
digitalWrite(LED4, (a[4] ? HIGH : LOW));
Third problem is that your function is incorrectly defined and it does not contain any type information. This is C and not Python so the types are EXTREAMLY important! The correct definition of your function should be:
void showDigits(int digit)
Take a look at this example that compiled correctly:
void showDigits(int digit)
{
bool a[6] = {false, false, false, false, false, false};
switch (digit)
{
case 1:
a[0] = true;
a[1] = false;
break;
case 2:
a[1] = false;
break;
default:
a[0] = true;
}
if (a[4]) {
digitalWrite(4, HIGH);
} else {
digitalWrite(4, LOW);
}
};
The switch..case
is a very particular syntax. If you don't use the break;
at the end of a case
the execution will actually continue to next case
definition, and compiler told you that you tried to define int a[]
several times in the same scope. (which is a no-no)