Found CASE instruction syntax in CoDeSys from ABB, and I am curious, does arrays for the cases available in standard C++? Or is it possible to modify the macro, so that it could work the same way?
CASE instruction
With the CASE instructions one can combine several conditioned instructions with the same condition variable in one construct.
Syntax:
CASE OF
: <Instruction 1>
: <Instruction 2>
<Value3, Value4, Value5>: <Instruction 3>
<Value6 .. Value10>: <Instruction 4>
...
:
ELSE
END_CASE;
A CASE instruction is processed according to the following model:
If the variable in has the value , then the instruction is executed.
If <Var 1> has none of the indicated values, then the is executed.
If the same instruction is to be executed for several values of the variables, then one can write these values one after the other separated by commas, and thus condition the common execution.
If the same instruction is to be executed for a value range of a variable, one can write the initial value and the end value separated by two dots one after the other. So you can condition the common condition.
Example:
CASE INT1 OF
1, 5: BOOL1 := TRUE;
BOOL3 := FALSE;
2: BOOL2 := FALSE;
BOOL3 := TRUE;
10..20: BOOL1 := TRUE;
BOOL3:= TRUE;
ELSE
BOOL1 := NOT BOOL1;
BOOL2 := BOOL1 OR BOOL2;
END_CASE;
DarK_AlximiK:
Check the syntax of ABB CoDeSys of the CASE statement, it has arrays in it!
Example:
1,3,5:
6..20,22..30:
Wouldn't it be great to have the same macro?
You can say:
case 1:
case 3:
case 5:
do_something();
break;
but in C++ switch statements there is no equivalent of the range notation (e.g. 6..20) you used in your example.
Combining the two suggestions you can have ranges and non-contiguous tests:
void setup ()
{
int var = 42;
switch(var)
{
case 1 ... 5:
case 15:
case 20 ... 30:
// do whatever
break;
case 6 ... 10:
break;
default:
break;
} // end of switch
} // end of setup
void loop () { }
/IDLE RUN STATE/
void idleRunUpdate(){
// Serial.println("idleRunUpdate");
//Conditions for transition to a Line Avoid State
if((sensorSum>=16 && sensorSum<=63)||(sensorSum>=80 && sensorSum<=127)||(sensorSum>=208 && sensorSum<=255)){
stateMachine.transitionTo(lineAvoidState);
}
//Conditions for transition to a Attack State
if(sensorSum==2||sensorSum==4||sensorSum==6||sensorSum==7||sensorSum==14){
stateMachine.transitionTo(attackState);
}
//Conditions for transition to a Spin State
if(sensorSum==1||sensorSum==3||sensorSum==8||sensorSum==12){
stateMachine.transitionTo(spinState);
}
//Conditions for transition to a Overfall Avoid State
if((sensorSum>=64 && sensorSum<=79)||(sensorSum>=128 && sensorSum<=143)||(sensorSum>=192 && sensorSum<=207)){
stateMachine.transitionTo(overfallAvoidState);
}
}
to this:
/IDLE RUN STATE/
void idleRunUpdate(){
// Serial.println("idleRunUpdate");
switch(sensorSum){
//Conditions for transition to a Line Avoid State
case 16 ... 63:
case 80 ... 127:
case 208 ... 255:
stateMachine.transitionTo(lineAvoidState);
break;
//Conditions for transition to a Attack State
case 2:
case 4:
case 6:
case 7:
case 14:
stateMachine.transitionTo(attackState);
break;
//Conditions for transition to a Spin State
case 1:
case 3:
case 8:
case 12:
stateMachine.transitionTo(spinState);
break;
//Conditions for transition to a Overfall Avoid State
case 64 ... 79:
case 128 ... 143:
case 192 ... 207:
stateMachine.transitionTo(overfallAvoidState);
break;
}
}
wildbill:
Looks the same to me. If you have doubts, you could always write a little test harness to test both versions and verify that they do the same thing.
Less coding bro, more clear/accurate and saves bytes