0

I’m completely new to arduino, so be polite. I’m trying to program my esp32 to drive neopixels as seven segment display. So I take a digit and pass it to switch case and in switch case I mention which led number should be lit. `

function showDigits(digit){

switch(digit){

 case 1:
   int a[]={1,3,4,.....};
  break;

 case 2:
    int a[]={1,3,4,6,8,...};
   break;
  .
  .
  .
default:
   int a[]={1,2,3,4,5,6,7,8,9,....};
  } 
}

Now this is throwing multiple errors including redeclare of array a in same scope. Is there any better way to code this?

3 Answers 3

3

If you want to declare multiple variables with the same name inside different case statements, you can enclose the code inside each of them inside curly braces:

case 1:{
    Code...
break;}
case 2:
...

The curly braces will set the limits for the variables scope to just this case.

0

While I don't discourage you from doing it yourself. I would go the easy route and use an available library. As is, or if need be, after modifying it. For me, it is the better way as I'm not that good at programming.

https://learn.adafruit.com/adafruit-neopixel-uberguide/neomatrix-library

And this looks good.

FastLED NeoMatrix

Better support for ESP32 than Adafruit::Neopixel

https://github.com/marcmerlin/FastLED_NeoMatrix

0

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.