The learner will review code and answer questions about the while command.

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.

Question Image
Question 1 of 10

While Command

Correct!

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.

question image

Correct Answer

Got it!

 Next Question
Question 1 of 10

While Command

Incorrect

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.

question image

Your Answer

Correct Answer

Got it!

 Next Question
Question 1 of 10

While Command

True or False: The calibrate function is called when digitalRead(pushbuttonPin) is HIGH.

Question Image
Question 1 of 10

While Command

Correct!

True or False: The calibrate function is called when digitalRead(pushbuttonPin) is HIGH.

question image

Correct Answer

True

Explanation:

In the main section of code, the calibrate function is called exactly when digitalRead(pushbuttonPin) is HIGH.

 Next Question
Question 1 of 10

While Command

Incorrect

True or False: The calibrate function is called when digitalRead(pushbuttonPin) is HIGH.

question image

Your Answer

Correct Answer

True

Explanation:

In the main section of code, the calibrate function is called exactly when digitalRead(pushbuttonPin) is HIGH.

 Next Question
Question 1 of 10

While Command

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.

Question Image
Question 1 of 10

While Command

Correct!

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.

question image

Correct Answer

False

Explanation:

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.

 Next Question
Question 1 of 10

While Command

Incorrect

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.

question image

Your Answer

Correct Answer

False

Explanation:

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.

 Next Question
Question 1 of 10

While Command

As the program is written, how many times will calibrate be called?

Question Image
Question 1 of 10

While Command

Correct!

As the program is written, how many times will calibrate be called?

question image

Correct Answer

depends how long the push button is pressed

Explanation:

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.

 Next Question
Question 1 of 10

While Command

Incorrect

As the program is written, how many times will calibrate be called?

question image

Your Answer

Correct Answer

depends how long the push button is pressed

Explanation:

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.

 Next Question
Question 1 of 10

While Command

True or False: The following line of code is executed while the push button is pressed analogWrite(PressureLed, sensorValue);.

Question Image
Question 1 of 10

While Command

Correct!

True or False: The following line of code is executed while the push button is pressed analogWrite(PressureLed, sensorValue);.

question image

Correct Answer

False

Explanation:

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.

 Next Question
Question 1 of 10

While Command

Incorrect

True or False: The following line of code is executed while the push button is pressed analogWrite(PressureLed, sensorValue);.

question image

Your Answer

Correct Answer

False

Explanation:

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.

 Next Question
Question 1 of 10

While Command

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.  

Question Image
Question 1 of 10

While Command

Correct!

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.  

question image

Correct Answer

True

Explanation:

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.  

 Next Question
Question 1 of 10

While Command

Incorrect

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.  

question image

Your Answer

Correct Answer

True

Explanation:

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.  

 Next Question
Question 1 of 10

While Command

If we changed the while condition to be while (pushbuttonPin = HIGH), how many times will the while loop execute?

Question Image
Question 1 of 10

While Command

Correct!

If we changed the while condition to be while (pushbuttonPin = HIGH), how many times will the while loop execute?

question image

Correct Answer

forever

Explanation:

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.

 Next Question
Question 1 of 10

While Command

Incorrect

If we changed the while condition to be while (pushbuttonPin = HIGH), how many times will the while loop execute?

question image

Your Answer

Correct Answer

forever

Explanation:

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.

 Next Question
Question 1 of 10

While Command

Which line(s) of code are executed in this example if the button is pressed?

while (digitalRead(pushbuttonPin) == HIGH)
calibrate();
digitalWrite(CalibrateLedPin, LOW);

Question Image
Question 1 of 10

While Command

Correct!

Which line(s) of code are executed in this example if the button is pressed?

while (digitalRead(pushbuttonPin) == HIGH)
calibrate();
digitalWrite(CalibrateLedPin, LOW);

question image

Correct Answer

calibrate();

Explanation:

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.

 Next Question
Question 1 of 10

While Command

Incorrect

Which line(s) of code are executed in this example if the button is pressed?

while (digitalRead(pushbuttonPin) == HIGH)
calibrate();
digitalWrite(CalibrateLedPin, LOW);

question image

Your Answer

Correct Answer

calibrate();

Explanation:

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.

 Next Question
Question 1 of 10

While Command

Is the while command a pre or post entry conditional test?

Question Image
Question 1 of 10

While Command

Correct!

Is the while command a pre or post entry conditional test?

question image

Correct Answer

Pre

Explanation:

The while command is preconditional.

 Next Question
Question 1 of 10

While Command

Incorrect

Is the while command a pre or post entry conditional test?

question image

Your Answer

Correct Answer

Pre

Explanation:

The while command is preconditional.

 Next Question
Question 1 of 10

While Command

Which line(s) of code are executed in this example if the button is pressed?

while(digitalRead(pushbuttonPin) == HIGH)
{
   calibrate();
   digitalWrite(CalibrateLedPin, LOW);
}

Question Image
Question 1 of 10

While Command

Correct!

Which line(s) of code are executed in this example if the button is pressed?

while(digitalRead(pushbuttonPin) == HIGH)
{
   calibrate();
   digitalWrite(CalibrateLedPin, LOW);
}

question image

Correct Answer

calibrate();
digitalWrite(CalibrateLedPin, LOW);

Explanation:

Since the statements after the while command are in curly brackets, both lines will be executed when the comparison is true.

 Next Question
Question 1 of 10

While Command

Incorrect

Which line(s) of code are executed in this example if the button is pressed?

while(digitalRead(pushbuttonPin) == HIGH)
{
   calibrate();
   digitalWrite(CalibrateLedPin, LOW);
}

question image

Your Answer

Correct Answer

calibrate();
digitalWrite(CalibrateLedPin, LOW);

Explanation:

Since the statements after the while command are in curly brackets, both lines will be executed when the comparison is true.

 Next Question
Question 1 of 10

While Command

True or False: While commands may run indefinately if the conditional never becomes false.

Question Image
Question 1 of 10

While Command

Correct!

True or False: While commands may run indefinately if the conditional never becomes false.

question image

Correct Answer

True

Explanation:

A while command can run indefinitely if its conditional is true.

 Finish
Question 1 of 10

While Command

Incorrect

True or False: While commands may run indefinately if the conditional never becomes false.

question image

Your Answer

Correct Answer

True

Explanation:

A while command can run indefinitely if its conditional is true.

 Finish
Question 1 of 10
While Command

Score

You have answered 5 of 10 questions correctly.

50%

Start Over

See other flashcards and apps related to:

Question 1 of 10
Author
Published
6/2/2015
Last Updated
6/2/2015
Tags

No Comments Yet