I'm sure this is not new but I wanted to use 'case' with ranges of byte values and
of course
case 1...6 :
is not allowed. However, I found that by converting to char the following code works as expected; or does it? Is there something lurking there that will catch me out? after several days use there doesn't seem to be a gremlin.
Thanks
Dave
int inint;
int myinbufctr;
char myinbuf[6];
void setup() {
// put your setup code here, to run once:
Serial.begin(57600);
myinbufctr = 0;
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {
myinbuf[myinbufctr] = Serial.read();
if (myinbuf[myinbufctr] == 10) {
myinbuf[myinbufctr] = '\0';
myinbufctr = 0;
inint = atoi(myinbuf);
Serial.println(inint, HEX);
switch (char(inint)) {
case char(128)...char(255) :
// do something
Serial.println("128...255");
break;
case char(0)...char(127) :
// do something else
Serial.println("0...127");
break;
}
}
else {
myinbufctr++;
}
}
}
Hahaha thats some scary stuff right there, try this:
switch( range )
{
case 0:
case 1:
case 2:
case 3:
{
// executes for the range 0-3 inclussive
}
break;
case 5:
{
//only executes when range is 5
}
break;
}
tammytam:
Hahaha thats some scary stuff right there, try this:
switch( range )
{
case 0:
case 1:
case 2:
case 3:
{
// executes for the range 0-3 inclussive
}
break;
case 5:
{
//only executes when range is 5
}
break;
}
You want to do that for more than 4 consecutive numbers? you'ld be their all day typing. Say for example you want to split them into groups of 10 consecutive numbers, all the way from 0 to 255.
Dave
groundfungus:
Ellipses work with int if you put a space before and after the ellipses.
int num = 3;
void setup()
{
Serial.begin(9600);
}
void loop()
{
switch(num)
{
case 1 ... 5:
Serial.println("hit 1 to 5");
break;
case 6 ... 10:
Serial.println("hit 6 to 10");
break;
}
delay(5000);
}
Well how about that!!!! is that just a quirk of the arduino C++ or is it standard?
I had a conversation some weeks ago on here re case statements and no-one else mentioned that little quirk!!
Dave
BTW are there any other statements that are 'space' sensitive?
DavidI:
Well how about that!!!! is that just a quirk of the arduino C++ or is it standard?
I had a conversation some weeks ago on here re case statements and no-one else mentioned that little quirk!!
Dave
BTW are there any other statements that are 'space' sensitive?
Thanks, I will and before I have a heart attack what is the above doing? incrementing i by 1 five times then adding g and leaving the result in k.
I still hate C
Seems there is a little inconsistency here in the compiler:
1...6: doesn't work
1 ... 6: does
but so does
'1'...'6'
and char(1)...char(6); does as well
and I'd bet on spaces before and after the ellipses would also work in the last two examples.
so only in the single case of pure numbers are spaces required. But might it be that for the compiler the presence of a non numeral before and after the ellipses is sufficient?
We've moved away from my original query of the possibility of gremlins in my coding but it's been fun
Dave
I guess the question is, should the compiler assume you know what you are doing and try every possible permutation of the tokens to find something that works, or just use a best guess, and if it is not understandable throw an error?
What if there are two variations that work? Which should the compiler use?