The learner will review code and answer questions about the while command.
All the flashcards in this set deal with the code below:
const int Pressuresensor = 0;
const int PressureLed = 9;
const int CalibrateLedPin = 13;
const int pushbuttonPin = 2;
int sensorMin = 1023;
int sensorMax = 0;
int sensorValue = 0;
void setup()
{
pinMode(CalibrateLedPin, OUTPUT);
pinMode(PressureLed, OUTPUT);
pinMode(pushbuttonPin, INPUT);
}
void calibrate()
{
digitalWrite(CalibrateLedPin, HIGH);
sensorValue = analogRead(Pressuresensor);
if (sensorValue > sensorMax)
sensorMax = sensorValue;
if (sensorValue < sensorMin)
sensorMin = sensorValue;
}
void main()
{
while (digitalRead(pushbuttonPin) == HIGH)
{
calibrate();
}
digitalWrite(CalibrateLedPin, LOW);
sensorValue = analogRead(Pressuresensor);
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
analogWrite(PressureLed, sensorValue);
}
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:
const int Pressuresensor = 0;
const int PressureLed = 9;
const int CalibrateLedPin = 13;
const int pushbuttonPin = 2;
int sensorMin = 1023;
int sensorMax = 0;
int sensorValue = 0;
void setup()
{
pinMode(CalibrateLedPin, OUTPUT);
pinMode(PressureLed, OUTPUT);
pinMode(pushbuttonPin, INPUT);
}
void calibrate()
{
digitalWrite(CalibrateLedPin, HIGH);
sensorValue = analogRead(Pressuresensor);
if (sensorValue > sensorMax)
sensorMax = sensorValue;
if (sensorValue < sensorMin)
sensorMin = sensorValue;
}
void main()
{
while (digitalRead(pushbuttonPin) == HIGH)
{
calibrate();
}
digitalWrite(CalibrateLedPin, LOW);
sensorValue = analogRead(Pressuresensor);
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
analogWrite(PressureLed, sensorValue);
}
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:
const int Pressuresensor = 0;
const int PressureLed = 9;
const int CalibrateLedPin = 13;
const int pushbuttonPin = 2;
int sensorMin = 1023;
int sensorMax = 0;
int sensorValue = 0;
void setup()
{
pinMode(CalibrateLedPin, OUTPUT);
pinMode(PressureLed, OUTPUT);
pinMode(pushbuttonPin, INPUT);
}
void calibrate()
{
digitalWrite(CalibrateLedPin, HIGH);
sensorValue = analogRead(Pressuresensor);
if (sensorValue > sensorMax)
sensorMax = sensorValue;
if (sensorValue < sensorMin)
sensorMin = sensorValue;
}
void main()
{
while (digitalRead(pushbuttonPin) == HIGH)
{
calibrate();
}
digitalWrite(CalibrateLedPin, LOW);
sensorValue = analogRead(Pressuresensor);
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
analogWrite(PressureLed, sensorValue);
}
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 calibrate function is called when digitalRead(pushbuttonPin) is HIGH.
True
In the main section of code, the calibrate function is called exactly when digitalRead(pushbuttonPin) is HIGH.
True or False: The calibrate function is called when digitalRead(pushbuttonPin) is HIGH.
True
In the main section of code, the calibrate function is called exactly when digitalRead(pushbuttonPin) is HIGH.
True or False: If the while statement were changed to read
while !(digitalRead(pushbuttonPin) == HIGH), a HIGH pushbuttonPin would cause the calibrate function to be called.
True or False: If the while statement were changed to read
while !(digitalRead(pushbuttonPin) == HIGH), a HIGH pushbuttonPin would cause the calibrate function to be called.
False
The ! in front of (digitalRead(pushbuttonPin) == HIGH) actually takes the negation of the value, so if
(digitalRead(pushbuttonPin) == HIGH) is true,
!(digitalRead(pushbuttonPin) == HIGH) will be false and the while command will not execute its code.
True or False: If the while statement were changed to read
while !(digitalRead(pushbuttonPin) == HIGH), a HIGH pushbuttonPin would cause the calibrate function to be called.
False
The ! in front of (digitalRead(pushbuttonPin) == HIGH) actually takes the negation of the value, so if
(digitalRead(pushbuttonPin) == HIGH) is true,
!(digitalRead(pushbuttonPin) == HIGH) will be false and the while command will not execute its code.
As the program is written, how many times will calibrate be called?
As the program is written, how many times will calibrate be called?
depends how long the push button is pressed
calibrate() will run if digitalRead(pushbuttonPin) is HIGH, but when it is done running and the program returns to the while statement, if digitalRead(pushbuttonPin) is still HIGH, it will run calibrate() again.
As the program is written, how many times will calibrate be called?
depends how long the push button is pressed
calibrate() will run if digitalRead(pushbuttonPin) is HIGH, but when it is done running and the program returns to the while statement, if digitalRead(pushbuttonPin) is still HIGH, it will run calibrate() again.
True or False: The following line of code is executed while the push button is pressed analogWrite(PressureLed, sensorValue);.
True or False: The following line of code is executed while the push button is pressed analogWrite(PressureLed, sensorValue);.
False
That code is not written until after the while loop has ended, meaning that it will not be executed until the button is no longer being pressed.
True or False: The following line of code is executed while the push button is pressed analogWrite(PressureLed, sensorValue);.
False
That code is not written until after the while loop has ended, meaning that it will not be executed until the button is no longer being pressed.
True or False: Since only one line of code is executed by the while loop in this example, the while loop body curly brackets could be removed with no change to the program functionality.
True or False: Since only one line of code is executed by the while loop in this example, the while loop body curly brackets could be removed with no change to the program functionality.
True
If the while command is used without any curly brackets, it will execute (exactly) the next line of code if its condition is met, so removing them in this instance would produce the same program behavior.
True or False: Since only one line of code is executed by the while loop in this example, the while loop body curly brackets could be removed with no change to the program functionality.
True
If the while command is used without any curly brackets, it will execute (exactly) the next line of code if its condition is met, so removing them in this instance would produce the same program behavior.
If we changed the while condition to be while (pushbuttonPin = HIGH), how many times will the while loop execute?
If we changed the while condition to be while (pushbuttonPin = HIGH), how many times will the while loop execute?
forever
Because there is only one =, the place in our while command that should have a comparison has an assignment instead. This will cause the while loop to run forever because the value of pushbuttonPin will be set to HIGH which is TRUE, so the comparison will always be true.
If we changed the while condition to be while (pushbuttonPin = HIGH), how many times will the while loop execute?
forever
Because there is only one =, the place in our while command that should have a comparison has an assignment instead. This will cause the while loop to run forever because the value of pushbuttonPin will be set to HIGH which is TRUE, so the comparison will always be true.
Which line(s) of code are executed in this example if the button is pressed?
while (digitalRead(pushbuttonPin) == HIGH)
calibrate();
digitalWrite(CalibrateLedPin, LOW);
Which line(s) of code are executed in this example if the button is pressed?
while (digitalRead(pushbuttonPin) == HIGH)
calibrate();
digitalWrite(CalibrateLedPin, LOW);
calibrate();
Since there are no curly brackets after the while command, only the next line of code will execute as long as the condition is true.
Which line(s) of code are executed in this example if the button is pressed?
while (digitalRead(pushbuttonPin) == HIGH)
calibrate();
digitalWrite(CalibrateLedPin, LOW);
calibrate();
Since there are no curly brackets after the while command, only the next line of code will execute as long as the condition is true.
Is the while command a pre or post entry conditional test?
Pre
The while command is preconditional.
Is the while command a pre or post entry conditional test?
Pre
The while command is preconditional.
Which line(s) of code are executed in this example if the button is pressed?
while(digitalRead(pushbuttonPin) == HIGH)
{
calibrate();
digitalWrite(CalibrateLedPin, LOW);
}
Which line(s) of code are executed in this example if the button is pressed?
while(digitalRead(pushbuttonPin) == HIGH)
{
calibrate();
digitalWrite(CalibrateLedPin, LOW);
}
calibrate();
digitalWrite(CalibrateLedPin, LOW);
Since the statements after the while command are in curly brackets, both lines will be executed when the comparison is true.
Which line(s) of code are executed in this example if the button is pressed?
while(digitalRead(pushbuttonPin) == HIGH)
{
calibrate();
digitalWrite(CalibrateLedPin, LOW);
}
calibrate();
digitalWrite(CalibrateLedPin, LOW);
Since the statements after the while command are in curly brackets, both lines will be executed when the comparison is true.
True or False: While commands may run indefinately if the conditional never becomes false.
True or False: While commands may run indefinately if the conditional never becomes false.
True
A while command can run indefinitely if its conditional is true.
True or False: While commands may run indefinately if the conditional never becomes false.
True
A while command can run indefinitely if its conditional is true.
You have answered 5 of 10 questions correctly.
50%