Switch case using ranges and variables

Hi all,

I would like to set a speed according to an interval. If an interval is within a certain range the speed should be set to slow/medium/fast

Currently the code below works:

#include <Streaming.h>                              // for output

String Speed;
int Interval;

void setup() {
}

void loop() {
  
  Interval = random(0,501);                       // generates random numbers in the range [0,1,2,....,499,500] 
                                                  // INcluding the first parameter (0) but EXcluding the last parameter (501)
                                                     
  switch(Interval){
    
    case 0 ... 30:                                // fast:  when intInterval is in the range [0,1,2, ... ,29,30]
      Speed = "Fast";                             
    break;
    
    case 31 ... 50:                               // medium:  when intInterval is in the range [31,32, ... ,49,50]
      Speed = "Medium";
    break;
    
    case 51 ... 500:                              // slow:  when intInterval is in the range [51,52, ... ,499,500]
      Speed = "Slow";
    break;
    
  }

  Serial << "Interval is: " << Interval << "   speed is: " << Speed << endl;

}

However... instead of using hardcoded ranges, I would like the ranges to be variables, something like this:

#include <Streaming.h>                              // for output

String Speed;
int Interval;

int tresholdFast = 30;
int tresholdMedium = 50;
int tresholdSlow = 500;

void setup() {
}

void loop() {
  
  Interval = random(0,501);                                                 // generates random numbers in the range [0,1,2,....,499,500] 
                                                                            // INcluding the first parameter (0) but EXcluding the last parameter (501)
                                                     
  switch(Interval){
    
    case 0 ... tresholdFast:                                                // fast:  when intInterval is in the range [0,1,2, ... ,29,30]
      Speed = "Fast";                             
    break;
    
    case (tresholdFast + 1) ... tresholdMedium:                               // medium:  when intInterval is in the range [31,32, ... ,49,50]
      Speed = "Medium";
    break;
    
    case (tresholdMedium + 1) ... tresholdSlow:                              // slow:  when intInterval is in the range [51,52, ... ,499,500]
      Speed = "Slow";
    break;
    
  }

  Serial << "Interval is: " << Interval << "   speed is: " << Speed << endl;

}

This code gives an error :frowning:

"the value of 'tresholdFast' is not usable in a constant expression"

Yeah, I could use an if structure like:
if( 0 <= interval && interval <= tresholdFast){...}

But I would like to know if this could work with a switch case.

Is there a way to use variables in a switch/case ?

Thanks in advance !

The case values CANNOT be variables. They MUST be compile-time constants.

Regards,
Ray L.

Yeah this was a lesson, search the forum before you post :slight_smile:
It could be a nice reference for others though....

I had a look at Switch (case) statement with variable - Programming Questions - Arduino Forum

so.... changing this bit:

int tresholdFast = 30;
int tresholdMedium = 50;
int tresholdSlow = 500;

To this:

const int tresholdFast = 30;
const int tresholdMedium = 50;
const int tresholdSlow = 500;

Made it work !

Yay !