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
"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 !