Improving switch case.
i have been working on switch case serial char messages, ran in little problem.
i can not separate "q" "qe" .. without starting 2 cases. of course can throw if statement "q" know if can done switch case.
the serial commands comes windows driver , quite time sensitive. don't know if switch case faster else if statements, perhaps can tell me ?
here code:
i can not separate "q" "qe" .. without starting 2 cases. of course can throw if statement "q" know if can done switch case.
the serial commands comes windows driver , quite time sensitive. don't know if switch case faster else if statements, perhaps can tell me ?
here code:
code: [select]
void setup() {
serial.begin(9600);
}
void loop() {
if (serial.available() > 0) { // listen on serialport
string cmd = serial.readstringuntil('#'); // reads string until "#"
// available commands: ":u#", ":gd#", ":gr#", ":sd#", ":sr#", ":mn#", ":ms#", ":me#", ":mw#", ":rs#", ":rm#", ":rc#", ":rg#", ":q#", ":qe#", ":qw#", ":qn#", ":qs#"
switch (cmd.charat(1)){ // reads 2. char.
case 'u': // if 2. char==u execute case 'u'
serial.println("u#");
break;
case 'g': //if 2. char==g move on
case 's': //if 2. char==s move on
case 'm': //if 2. char==m move on
case 'r': //if 2. char==r move on
switch (cmd.charat(2)){ // , if 3. char==d execute case 'd'
case 'd':
serial.println("gd#");
break;
case'd':
serial.println("sd#");
break;
case 'r':
serial.println("gr#");
break;
case'r':
serial.println("sr#");
break;
case'n':
serial.println("mn#");
break;
case's':
serial.println("ms#");
break;
case'e':
serial.println("me#");
break;
case'w':
serial.println("mw#");
break;
case's':
serial.println("rs#");
break;
case'm':
serial.println("rm#");
break;
case'c':
serial.println("rc#");
break;
case'g':
serial.println("rg#");
break;
}
}
switch (cmd.charat(1)){ // reads 2. char.
case'q':
serial.println("q#");
break;
}
switch (cmd.charat(1)){ // reads 2. char.
case'q':
switch (cmd.charat(2)){ // reads 3. char.
case'e':
serial.println("qe#");
break;
case'w':
serial.println("qw#");
break;
case'n':
serial.println("qn#");
break;
case's':
serial.println("qs#");
break;
}
}
}//end serial
}// end loop
i don't know if switch case faster else if statements, perhaps can tell me ?the generated code identical (for avr processors).
Arduino Forum > Using Arduino > Programming Questions > Improving switch case.
arduino
Comments
Post a Comment