The learner will review code and answer questions about the analogRead and analogWrite functions.
All the flashcards in this set deal with the following code:
/*Assume an 8-bit analog output. Assume a 10-bit analog input. Assume maximum analog output is 5 VDC, therefore a 5 volt output would be generated by writing 255 to the analog output. A 2.5 volt output would be generated by writing 128, and a 0 volt output would be generated by writing 0.*/
int OutputPin = 3;
int analogPin = 1;
int value = 0;
void setup()
{
pinMode(OutputPin, OUTPUT);
}
void main()
{
value = analogRead(analogPin);
value = value / 4;
analogWrite(OutputPin, value);
}
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 an 8-bit analog output. Assume a 10-bit analog input. Assume maximum analog output is 5 VDC, therefore a 5 volt output would be generated by writing 255 to the analog output. A 2.5 volt output would be generated by writing 128, and a 0 volt output would be generated by writing 0.*/
int OutputPin = 3;
int analogPin = 1;
int value = 0;
void setup()
{
pinMode(OutputPin, OUTPUT);
}
void main()
{
value = analogRead(analogPin);
value = value / 4;
analogWrite(OutputPin, value);
}
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 an 8-bit analog output. Assume a 10-bit analog input. Assume maximum analog output is 5 VDC, therefore a 5 volt output would be generated by writing 255 to the analog output. A 2.5 volt output would be generated by writing 128, and a 0 volt output would be generated by writing 0.*/
int OutputPin = 3;
int analogPin = 1;
int value = 0;
void setup()
{
pinMode(OutputPin, OUTPUT);
}
void main()
{
value = analogRead(analogPin);
value = value / 4;
analogWrite(OutputPin, value);
}
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!
Which analog output is being used?
3
The code analogWrite(OutputPin, value) is where we actually send our output to the pin OutputPin. Since OutputPin has value 3, 3 is the analog output being used.
Which analog output is being used?
3
The code analogWrite(OutputPin, value) is where we actually send our output to the pin OutputPin. Since OutputPin has value 3, 3 is the analog output being used.
True or False: OutputPin could have been declared as a Float type variable.
False
We should never use float variables for values we are going to test for equality or as arguments in functions that take integer arguments. Both the pinMode and the analogWrite functions take integer arguments.
True or False: OutputPin could have been declared as a Float type variable.
False
We should never use float variables for values we are going to test for equality or as arguments in functions that take integer arguments. Both the pinMode and the analogWrite functions take integer arguments.
True or False: Essentially, this program reads the analog input 1, scales it by a factor of 4, and then writes the scaled value to analog output 3.
True or False: Essentially, this program reads the analog input 1, scales it by a factor of 4, and then writes the scaled value to analog output 3.
True
That is a valid explanation of what this program does.
True or False: Essentially, this program reads the analog input 1, scales it by a factor of 4, and then writes the scaled value to analog output 3.
True
That is a valid explanation of what this program does.
True or False: We could rewrite the lines value = analogRead(analogPin); and value = value / 4; by replacing them with value = analogRead(analogPin) / 4; and the program would work produce the same results.
True or False: We could rewrite the lines value = analogRead(analogPin); and value = value / 4; by replacing them with value = analogRead(analogPin) / 4; and the program would work produce the same results.
True
value = analogRead(analogPin) / 4; would read from the pin analogPin, divide the value by 4, and store that as value, just like the separate lines of code value = analogRead(analogPin); and value = value / 4;.
True or False: We could rewrite the lines value = analogRead(analogPin); and value = value / 4; by replacing them with value = analogRead(analogPin) / 4; and the program would work produce the same results.
True
value = analogRead(analogPin) / 4; would read from the pin analogPin, divide the value by 4, and store that as value, just like the separate lines of code value = analogRead(analogPin); and value = value / 4;.
If we replaced all three lines of code in the main function with analogWrite(OutputPin, analogRead(analogPin)/4); would this program function the same?
If we replaced all three lines of code in the main function with analogWrite(OutputPin, analogRead(analogPin)/4); would this program function the same?
Yes
The program would function the same (with the exception that value would not store anything other than 0); analogWrite(OutputPin, analogRead(analogPin)/4); will read the value on pin analogPin, scale it by a factor of 4, and then analog write that scaled value to pin OutputPin.
If we replaced all three lines of code in the main function with analogWrite(OutputPin, analogRead(analogPin)/4); would this program function the same?
Yes
The program would function the same (with the exception that value would not store anything other than 0); analogWrite(OutputPin, analogRead(analogPin)/4); will read the value on pin analogPin, scale it by a factor of 4, and then analog write that scaled value to pin OutputPin.
True or False: In the line analogWrite(OutputPin, value); the value parameter is an optional parameter.
True or False: In the line analogWrite(OutputPin, value); the value parameter is an optional parameter.
False
The analogWrite function requires two inputs, where to write (here OutputPin) and what to write (here value).
True or False: In the line analogWrite(OutputPin, value); the value parameter is an optional parameter.
False
The analogWrite function requires two inputs, where to write (here OutputPin) and what to write (here value).
If value had not been scaled by a factor of 4 in this program, what would the analog output be if the analog input was 180 hex?
If value had not been scaled by a factor of 4 in this program, what would the analog output be if the analog input was 180 hex?
80 hex
Since we only have an 8-bit analog output, we can only write two hex digits of output; thus 80 hex will be output when 180 hex is input.
If value had not been scaled by a factor of 4 in this program, what would the analog output be if the analog input was 180 hex?
80 hex
Since we only have an 8-bit analog output, we can only write two hex digits of output; thus 80 hex will be output when 180 hex is input.
What is the numerical content of value in the analogWrite function if 2.5 VDC is being output?
What is the numerical content of value in the analogWrite function if 2.5 VDC is being output?
128
Since there is an 8-bit output and the maximum output is 5 VDC, then an output of 2.5 VDC must be generated by half of the maximum output, or 128. Note that the value being referenced here has already been scaled by a factor of 4.
What is the numerical content of value in the analogWrite function if 2.5 VDC is being output?
128
Since there is an 8-bit output and the maximum output is 5 VDC, then an output of 2.5 VDC must be generated by half of the maximum output, or 128. Note that the value being referenced here has already been scaled by a factor of 4.
If the analog read results in a value of 3 being read into value, what would be sent to the analog output?
If the analog read results in a value of 3 being read into value, what would be sent to the analog output?
0
Since value is an int type, when value = value / 4; is executed, the division 3 / 4 will result in 0, so 0 will be assigned to value.
If the analog read results in a value of 3 being read into value, what would be sent to the analog output?
0
Since value is an int type, when value = value / 4; is executed, the division 3 / 4 will result in 0, so 0 will be assigned to value.
True or False: value could have effectively been divided by 4 by arithmetically shifting it right twice.
True or False: value could have effectively been divided by 4 by arithmetically shifting it right twice.
True
Division by 4 and an arithmetic shift right by two places has the same result.
True or False: value could have effectively been divided by 4 by arithmetically shifting it right twice.
True
Division by 4 and an arithmetic shift right by two places has the same result.
You have answered 5 of 10 questions correctly.
50%