I'm following a beginner tutorial about "Switch Case" with this code:
if (Serial.available() > 0) {
char inByte = Serial.read();
// do something different depending on the character received.
// The switch statement expects single number values for each case;
// in this exmaple, though, you're using single quotes to tell
// the controller to get the ASCII value for the character. For
// example 'a' = 97, 'b' = 98, and so forth:
switch (inByte) {
case 'a':
digitalWrite(2, HIGH);
break;
case 'b':
digitalWrite(3, HIGH);
break;
case 'c':
digitalWrite(4, HIGH);
break;
case 'd':
digitalWrite(5, HIGH);
break;
case 'e':
digitalWrite(6, HIGH);
break;
case 'on':
for (int thisPin = 2; thisPin < 8; thisPin++) {
digitalWrite(thisPin, HIGH);
delay (100);
}
break;
What I don't understand is that on the video, when the guy enter the last paragraph of the code:
" case 'on': "
the character 'on' goes blue on the IDE, like it does with 'a' 'b' etccc
But on my IDE, it doesnt change the color to blue...
And then when I use the serial monitor to send 'on', it doesnt work....
Does it make sense ?
The guy is using the example named "switchcase2" , and simply changes "int inByte" by "char inByte" on lign 35 and then add a case with the ascii 'on'...
So why on my IDE, case 'on' doesnt get the color blue ^ ??
Then the guy in the video doesn't know much about coding...
'on' will appear to work, but it won't work as expected: it will have the same effect as just 'n', because it will discard anything else than the last character inside the quotes.
The blue appearance of the word 'on' means that somewhere it is defined as keyword which resolves to a case value accepted by switch.
That is not standard in the Arduino ide. My experience is like yours. The word is not in blue, and the switch case does not respond to "on" entered in the Serial Monitor.
Can you post the complete code and a link to the video?
I agree with @ whandall
So you found an error in a video. Probably there are plenty of them.
You could also build a table of acceptable commands, along with a corresponding constant, then have a function that returns the constant for any command parsed. Then, you can use the constant in a switch statement.
Waouu !! Thank you for all those replies.. for a first time coming here on this forum, it feels pretty great.
So from your replies, what I understand it's that with the variable char , it should be impossible to use a two characters command.
That the serial port and so the serial.read will just look at the last character so the n on that case
But it doesnt explain why it is working on the video and why it changes to blue on his IDE as soon as he types the word 'on' ( And he is not using a single keystroke that would be 'on' on his keyboard ??? he is writing the letter o then the letter n )
So as cattledog says, it must be that "means that somewhere it is defined as keyword which resolves to a case value accepted by switch. "
So as demanded here a link to the video (I just shared it on Hubic):
jopoulos:
Waouu !! Thank you for all those replies.. for a first time coming here on this forum, it feels pretty great.
So from your replies, what I understand it's that with the variable char , it should be impossible to use a two characters command.
That the serial port and so the serial.read will just look at the last character so the n on that case
But it doesnt explain why it is working on the video and why it changes to blue on his IDE as soon as he types the word 'on' ( And he is not using a single keystroke that would be 'on' on his keyboard ??? he is writing the letter o then the letter n )
So as cattledog says, it must be that "means that somewhere it is defined as keyword which resolves to a case value accepted by switch. "
So as demanded here a link to the video (I just shared it on Hubic):
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
char t = 'on';
cout << t << endl;
return 0;
}
and here's the output from compile and run:
sh-4.3$ g++ -std=c++11 -o main *.cpp
main.cpp:9:13: warning: multi-character character constant [-Wmultichar]
char t = 'on';
^
main.cpp: In function 'int main()':
main.cpp:9:13: warning: overflow in implicit constant conversion [-Woverflow]
sh-4.3$ main
Hello World
n
sh-4.3$
So, it gives an overflow warning, and interprets it is the character 'n'.