The learner will review code and answer questions about a switch statement.
All the flashcards in this set deal with the code below:
int currentPin;
void setup()
{
Serial.begin(9600);
for (currentPin = 0; currentPin < 8; currentPin++)
pinMode(currentPin, OUTPUT);
}
void main()
{
char inByte;
if (Serial.available() > 0)
{
inByte = Serial.read();
switch (inByte)
{
case 'a':
digitalWrite(1, HIGH);
break;
case 'b':
digitalWrite(2, HIGH);
break;
case 'c':
digitalWrite(4, HIGH);
break;
case 'd':
digitalWrite(5, HIGH);
break;
case 'e':
digitalWrite(7, HIGH);
break;
default:
for (currentPin = 0; currentPin < 8; currentPin++)
digitalWrite(currentPin, LOW);
}
}
}
This code is displayed in the image below, which will be on each card, but you may want to make note of it before going on.
All the flashcards in this set deal with the code below:
int currentPin;
void setup()
{
Serial.begin(9600);
for (currentPin = 0; currentPin < 8; currentPin++)
pinMode(currentPin, OUTPUT);
}
void main()
{
char inByte;
if (Serial.available() > 0)
{
inByte = Serial.read();
switch (inByte)
{
case 'a':
digitalWrite(1, HIGH);
break;
case 'b':
digitalWrite(2, HIGH);
break;
case 'c':
digitalWrite(4, HIGH);
break;
case 'd':
digitalWrite(5, HIGH);
break;
case 'e':
digitalWrite(7, HIGH);
break;
default:
for (currentPin = 0; currentPin < 8; currentPin++)
digitalWrite(currentPin, LOW);
}
}
}
This code is displayed in the image below, which will be on each card, but you may want to make note of it before going on.
Got it!
All the flashcards in this set deal with the code below:
int currentPin;
void setup()
{
Serial.begin(9600);
for (currentPin = 0; currentPin < 8; currentPin++)
pinMode(currentPin, OUTPUT);
}
void main()
{
char inByte;
if (Serial.available() > 0)
{
inByte = Serial.read();
switch (inByte)
{
case 'a':
digitalWrite(1, HIGH);
break;
case 'b':
digitalWrite(2, HIGH);
break;
case 'c':
digitalWrite(4, HIGH);
break;
case 'd':
digitalWrite(5, HIGH);
break;
case 'e':
digitalWrite(7, HIGH);
break;
default:
for (currentPin = 0; currentPin < 8; currentPin++)
digitalWrite(currentPin, LOW);
}
}
}
This code is displayed in the image below, which will be on each card, but you may want to make note of it before going on.
Got it!
True or False: All lights will be turned off if the character 'A' is received.
True
Since Serial.read returns the first byte of information and 'A' is not represented by the same byte as 'a', the switch statement will go to its default case and set all the pins to LOW.
True or False: All lights will be turned off if the character 'A' is received.
True
Since Serial.read returns the first byte of information and 'A' is not represented by the same byte as 'a', the switch statement will go to its default case and set all the pins to LOW.
Which output is set to high if the character 'b' is received?
2
This is case 'b', so the code under it will be executed, first digitalWrite(2, HIGH); setting output 2 to high, then break; ending the switch statement.
Which output is set to high if the character 'b' is received?
2
This is case 'b', so the code under it will be executed, first digitalWrite(2, HIGH); setting output 2 to high, then break; ending the switch statement.
Will the switch statement be executed if there is no serial input?
No
The switch statement will only execute if there is actual input, which is what Serial.available() > 0 is checking for.
Will the switch statement be executed if there is no serial input?
No
The switch statement will only execute if there is actual input, which is what Serial.available() > 0 is checking for.
If the input received is 'E', will digital output 7 be turned on?
No
Since Serial.read returns the first byte of information and 'E' is not represented by the same byte as 'e', the switch statement will go to its default case and set all the pins to LOW.
If the input received is 'E', will digital output 7 be turned on?
No
Since Serial.read returns the first byte of information and 'E' is not represented by the same byte as 'e', the switch statement will go to its default case and set all the pins to LOW.
True or False: The variable inByte could be of type integer and the program would function the same.
True or False: The variable inByte could be of type integer and the program would function the same.
True
Since Serial.read returns the first byte of information, it will read the inputs and compare them to the cases (as written) the same if inByte is a char variable type or an int variable type.
True or False: The variable inByte could be of type integer and the program would function the same.
True
Since Serial.read returns the first byte of information, it will read the inputs and compare them to the cases (as written) the same if inByte is a char variable type or an int variable type.
What is the scope of the currentPin variable?
Global
The currentPin variable is declared outside of any of the functions, so it has global scope.
What is the scope of the currentPin variable?
Global
The currentPin variable is declared outside of any of the functions, so it has global scope.
The decision that determines which output is turned on is based on what?
The decision that determines which output is turned on is based on what?
inByte
The switch command has inByte as its argument, so the value of inByte is what the cases are checked against.
The decision that determines which output is turned on is based on what?
inByte
The switch command has inByte as its argument, so the value of inByte is what the cases are checked against.
The switch statement is used to handle what?
The switch statement is used to handle what?
data that is char, Boolean, or integer type
The argument of the switch function needs to be easily comparable using an equality comparison, which limits our options (basically) to those in the correct answer: char, Boolean, and int.
The switch statement is used to handle what?
data that is char, Boolean, or integer type
The argument of the switch function needs to be easily comparable using an equality comparison, which limits our options (basically) to those in the correct answer: char, Boolean, and int.
You have answered 5 of 10 questions correctly.
50%