Problem with Switch command

Why does this example not work?

void loop () {
  switch (Program) {
          case 1: 
          BeaconTurnLeftRound(InitNumberofLedsOn, false);
          break; 
          case 2:
          BeaconTurnLeftRound(InitNumberofLedsOn, true);
          break;
  }      
}

and this example does work?

void loop () {
  switch (Program) {
          case 1: 
          int a = 1;
          BeaconTurnLeftRound(InitNumberofLedsOn, false);
          break; 
          case 2:
          BeaconTurnLeftRound(InitNumberofLedsOn, true);
          break;
  }      
}

I don't have a clue.

hum... missing context. read How to get the best out of this forum to get example of what's expected for code questions.

in the second example, you can't define a local variable in a case without creating a compound statement around it

void loop () {
  switch (Program) {
          case 1: 
            {
              int a = 1;
              BeaconTurnLeftRound(InitNumberofLedsOn, false);
            }
            break; 
         case 2:
          BeaconTurnLeftRound(InitNumberofLedsOn, true);
          break;
  }      
}

(although the compiler will throw it away since a is not used)


if you are saying code is working if you mess with the stack, it likely means you have a memory overflow somewhere

Adding an initialized local variable inside a switch statement will cause all cases beyond that point to not be reachable. The fact that making BeaconTurnLeftRound(InitNumberofLedsOn, true); unreachable makes your sketch 'work' tends to indicate a problem in 'BeaconTurnLeftRound(InitNumberofLedsOn, true);' that causes problems.

I don't get your question. The first one is not compiling and the second is compiling. There is only one function.

Only ad it so it compiles. What is wrong with the function call?

probably error somewhere else... post the compiler output / error message (using code tags)
We are shooting in the dark because you failed to post enough information. Please read the link above.

c:\users\f.voogel\documents\arduino-1.8.16-windows\arduino-1.8.16\hardware\tools\avr\avr\include\util\delay.h: In function 'BeaconTurnLeftRound':
c:\users\f.voogel\documents\arduino-1.8.16-windows\arduino-1.8.16\hardware\tools\avr\avr\include\util\delay.h:187:28: error: __builtin_avr_delay_cycles expects a compile time integer constant
  __builtin_avr_delay_cycles(__ticks_dc);
                            ^
c:\users\f.voogel\documents\arduino-1.8.16-windows\arduino-1.8.16\hardware\tools\avr\avr\include\util\delay.h:187:28: error: __builtin_avr_delay_cycles expects a compile time integer constant
  __builtin_avr_delay_cycles(__ticks_dc);
                            ^
c:\users\f.voogel\documents\arduino-1.8.16-windows\arduino-1.8.16\hardware\tools\avr\avr\include\util\delay.h:187:28: error: __builtin_avr_delay_cycles expects a compile time integer constant
  __builtin_avr_delay_cycles(__ticks_dc);
                            ^
c:\users\f.voogel\documents\arduino-1.8.16-windows\arduino-1.8.16\hardware\tools\avr\avr\include\util\delay.h:187:28: error: __builtin_avr_delay_cycles expects a compile time integer constant
  __builtin_avr_delay_cycles(__ticks_dc);
                            ^
c:\users\f.voogel\documents\arduino-1.8.16-windows\arduino-1.8.16\hardware\tools\avr\avr\include\util\delay.h:187:28: error: __builtin_avr_delay_cycles expects a compile time integer constant
  __builtin_avr_delay_cycles(__ticks_dc);
                            ^
c:\users\f.voogel\documents\arduino-1.8.16-windows\arduino-1.8.16\hardware\tools\avr\avr\include\util\delay.h:187:28: error: __builtin_avr_delay_cycles expects a compile time integer constant
  __builtin_avr_delay_cycles(__ticks_dc);
                            ^
c:\users\f.voogel\documents\arduino-1.8.16-windows\arduino-1.8.16\hardware\tools\avr\avr\include\util\delay.h:187:28: error: __builtin_avr_delay_cycles expects a compile time integer constant
  __builtin_avr_delay_cycles(__ticks_dc);
                            ^
c:\users\f.voogel\documents\arduino-1.8.16-windows\arduino-1.8.16\hardware\tools\avr\avr\include\util\delay.h:187:28: error: __builtin_avr_delay_cycles expects a compile time integer constant
  __builtin_avr_delay_cycles(__ticks_dc);
                            ^
c:\users\f.voogel\documents\arduino-1.8.16-windows\arduino-1.8.16\hardware\tools\avr\avr\include\util\delay.h:187:28: error: __builtin_avr_delay_cycles expects a compile time integer constant
  __builtin_avr_delay_cycles(__ticks_dc);
                            ^
c:\users\f.voogel\documents\arduino-1.8.16-windows\arduino-1.8.16\hardware\tools\avr\avr\include\util\delay.h:187:28: error: __builtin_avr_delay_cycles expects a compile time integer constant
  __builtin_avr_delay_cycles(__ticks_dc);
                            ^
c:\users\f.voogel\documents\arduino-1.8.16-windows\arduino-1.8.16\hardware\tools\avr\avr\include\util\delay.h:187:28: error: __builtin_avr_delay_cycles expects a compile time integer constant
  __builtin_avr_delay_cycles(__ticks_dc);
                            ^
c:\users\f.voogel\documents\arduino-1.8.16-windows\arduino-1.8.16\hardware\tools\avr\avr\include\util\delay.h:187:28: error: __builtin_avr_delay_cycles expects a compile time integer constant
  __builtin_avr_delay_cycles(__ticks_dc);
                            ^
lto-wrapper.exe: fatal error: C:\Users\f.voogel\Documents\arduino-1.8.16-windows\arduino-1.8.16\hardware\tools\avr/bin/avr-gcc returned 1 exit status
compilation terminated.
c:/users/f.voogel/documents/arduino-1.8.16-windows/arduino-1.8.16/hardware/tools/avr/bin/../lib/gcc/avr/7.3.0/../../../../avr/bin/ld.exe: error: lto-wrapper failed
collect2.exe: error: ld returned 1 exit status
exit status 1
Fout bij het compileren voor board ATtiny

Your both right. I called

if (IsBeacon) {DelayTimeToGoRound(63);} else {DelayTimeToGoRound(1);}

with a variable. That was the problem because _delay_ms is expecting an int constant on compiler time. Changed it and now it's acting normal again. Thanks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.