The learner will review code and answer questions about an if else statement.
All the flashcards in this set deal with the following code:
/*Assume a temperature sensor is connected to the analog pin and an alarm is connected to the digital output*/
const int tempPin = 1;
const int alarmPin = 0;
const int tempthreshold = 200;
int analogValue;
void setup()
{
pinmode(alarmPin, OUTPUT);
Serial.begin(9600);
}
void main()
{
analogValue = analogRead(tempPin);
if (analogValue > tempthreshold)
{
digitalWrite(alarmPin, HIGH);
}
else
{
digitalWrite(alarmPin, LOW);
}
Serial.println(analogValue);
}
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 following code:
/*Assume a temperature sensor is connected to the analog pin and an alarm is connected to the digital output*/
const int tempPin = 1;
const int alarmPin = 0;
const int tempthreshold = 200;
int analogValue;
void setup()
{
pinmode(alarmPin, OUTPUT);
Serial.begin(9600);
}
void main()
{
analogValue = analogRead(tempPin);
if (analogValue > tempthreshold)
{
digitalWrite(alarmPin, HIGH);
}
else
{
digitalWrite(alarmPin, LOW);
}
Serial.println(analogValue);
}
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 following code:
/*Assume a temperature sensor is connected to the analog pin and an alarm is connected to the digital output*/
const int tempPin = 1;
const int alarmPin = 0;
const int tempthreshold = 200;
int analogValue;
void setup()
{
pinmode(alarmPin, OUTPUT);
Serial.begin(9600);
}
void main()
{
analogValue = analogRead(tempPin);
if (analogValue > tempthreshold)
{
digitalWrite(alarmPin, HIGH);
}
else
{
digitalWrite(alarmPin, LOW);
}
Serial.println(analogValue);
}
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: The alarm will come on if the analog input value is 150.
False
The alarm will only be turned on if analogValue > tempthreshold. If the value read is 150, then analogValue > tempthreshold is 150 > 300, which is false. Therefore, the else code will run.
True or False: The alarm will come on if the analog input value is 150.
False
The alarm will only be turned on if analogValue > tempthreshold. If the value read is 150, then analogValue > tempthreshold is 150 > 300, which is false. Therefore, the else code will run.
True or False: The analogValue > tempthreshold condition of the if statement must be true in order for the alarm to be activated.
True or False: The analogValue > tempthreshold condition of the if statement must be true in order for the alarm to be activated.
True
The only place we turn on the alarm, digitalWrite(alarmPin, HIGH), is within the code of the if statement. The if statement will only execute its code when analogValue > tempthreshold.
True or False: The analogValue > tempthreshold condition of the if statement must be true in order for the alarm to be activated.
True
The only place we turn on the alarm, digitalWrite(alarmPin, HIGH), is within the code of the if statement. The if statement will only execute its code when analogValue > tempthreshold.
In order for the alarm to be turned off, the analog input value would have to be less than...
In order for the alarm to be turned off, the analog input value would have to be less than...
201
The alarm is turned off in the else case's code. That code will execute when the if case does not, so it will execute when analogValue > tempthreshold is false. Since tempthreshold is 200, analogValue needs to be less than 201 to make the statement false.
In order for the alarm to be turned off, the analog input value would have to be less than...
201
The alarm is turned off in the else case's code. That code will execute when the if case does not, so it will execute when analogValue > tempthreshold is false. Since tempthreshold is 200, analogValue needs to be less than 201 to make the statement false.
True or False: In this example, the curly brackets after the if and the else could have been eliminated.
True or False: In this example, the curly brackets after the if and the else could have been eliminated.
True
Since there is only one line of code after both the if and the else cases, we could have eliminated the curly braces and the program would have run the same.
True or False: In this example, the curly brackets after the if and the else could have been eliminated.
True
Since there is only one line of code after both the if and the else cases, we could have eliminated the curly braces and the program would have run the same.
True or False: In this program, analogValue is a constant.
False
analogValue is declared as an int type, but not a const.
True or False: In this program, analogValue is a constant.
False
analogValue is declared as an int type, but not a const.
True or False: If more than one line of code is to be executed when the if condition is true, curly brackets must be put around them.
True or False: If more than one line of code is to be executed when the if condition is true, curly brackets must be put around them.
True
If there are not curly brackets after the if statement, it will only execute (exactly) the next line of code when the condition is true. The program will then execute the line of code after that regardless of the condition's truth value.
True or False: If more than one line of code is to be executed when the if condition is true, curly brackets must be put around them.
True
If there are not curly brackets after the if statement, it will only execute (exactly) the next line of code when the condition is true. The program will then execute the line of code after that regardless of the condition's truth value.
True or False: Once the program is running, the value of tempthreshold can be changed.
False
tempthreshold is a constant in this program, so its value cannot be changed.
True or False: Once the program is running, the value of tempthreshold can be changed.
False
tempthreshold is a constant in this program, so its value cannot be changed.
True or False: If more than one line of code is to be executed when the else condition is true, curly brackets must be put around them.
True or False: If more than one line of code is to be executed when the else condition is true, curly brackets must be put around them.
True
Just like the if case, the else case also only executes (exactly) the next line of code by default. If you want it to execute more lines then that, you must use curly brackets.
True or False: If more than one line of code is to be executed when the else condition is true, curly brackets must be put around them.
True
Just like the if case, the else case also only executes (exactly) the next line of code by default. If you want it to execute more lines then that, you must use curly brackets.
True or False: The code in both the if and the else portions can be executed in the same pass of the program run.
True or False: The code in both the if and the else portions can be executed in the same pass of the program run.
False
Only one of the if or else cases can be executed in one pass. If the if condition is true, the if code will be executed. Otherwise, the else code will be executed.
True or False: The code in both the if and the else portions can be executed in the same pass of the program run.
False
Only one of the if or else cases can be executed in one pass. If the if condition is true, the if code will be executed. Otherwise, the else code will be executed.
What do the following lines of code do?
if (analogValue == tempthreshold)
digitalWrite(alarmPin, HIGH);
Assign the value of tempthreshold to analogValue and turn the alarm on.
Assign the value of tempthreshold to analogValue and leave the alarm in its current state.
Check if the value of analogValue is equal to tempthreshold and, if they are equal, turn the alarm on.
Check if the value of analogValue is equal to tempthreshold and, if they are equal, turn the alarm off.
What do the following lines of code do?
if (analogValue == tempthreshold)
digitalWrite(alarmPin, HIGH);
Check if the value of analogValue is equal to tempthreshold and, if they are equal, turn the alarm on.
The two equal signs check equality, so this statement sees if analogValue is equal to tempthreshold and, if so, turns the alarm on, digitalWrite(alarmPin, HIGH);.
What do the following lines of code do?
if (analogValue == tempthreshold)
digitalWrite(alarmPin, HIGH);
Check if the value of analogValue is equal to tempthreshold and, if they are equal, turn the alarm on.
The two equal signs check equality, so this statement sees if analogValue is equal to tempthreshold and, if so, turns the alarm on, digitalWrite(alarmPin, HIGH);.