The learner will review code and answer questions about an analog read.
All the flashcards in this set deal with the following code:
/*Assume a 10 bit analog to digital converter. Assume maximum analog input is 5 VDC. Thus a 5 volt input would be read as 1023, a 2.5 volt input would be read as 512, and a 0 volt input would be read as 0. */
int analogPin = 1;
int value = 0;
void setup()
{
Serial.begin(9600);
}
void main()
{
value = analogRead(analogPin);
Serial.println(value);
}
The 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 10 bit analog to digital converter. Assume maximum analog input is 5 VDC. Thus a 5 volt input would be read as 1023, a 2.5 volt input would be read as 512, and a 0 volt input would be read as 0. */
int analogPin = 1;
int value = 0;
void setup()
{
Serial.begin(9600);
}
void main()
{
value = analogRead(analogPin);
Serial.println(value);
}
The 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 10 bit analog to digital converter. Assume maximum analog input is 5 VDC. Thus a 5 volt input would be read as 1023, a 2.5 volt input would be read as 512, and a 0 volt input would be read as 0. */
int analogPin = 1;
int value = 0;
void setup()
{
Serial.begin(9600);
}
void main()
{
value = analogRead(analogPin);
Serial.println(value);
}
The 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 input channel is read?
1
The line value = analogRead(analogPin); calls the analogRead function on the value stored in analogPin. That value was set to 1.
Which analog input channel is read?
1
The line value = analogRead(analogPin); calls the analogRead function on the value stored in analogPin. That value was set to 1.
The analogRead function accepts which type of argument?
Integer
The analogRead function takes an integer argument.
The analogRead function accepts which type of argument?
Integer
The analogRead function takes an integer argument.
Assume 2.5 VDC is applied to analog input 2 and we run the program. What number will value have?
Assume 2.5 VDC is applied to analog input 2 and we run the program. What number will value have?
unknown
Since our program is not reading from analog input 2, we would need to know the input, if any, on input 1 to know what value the variable value will have.
Assume 2.5 VDC is applied to analog input 2 and we run the program. What number will value have?
unknown
Since our program is not reading from analog input 2, we would need to know the input, if any, on input 1 to know what value the variable value will have.
Which input would analogRead attempt to read in this code: analogRead(analogPin + 3);
4
It would try to read input 4 since analogPin = 1, digitalRead(analogPin + 3) is the same as digitalRead(1 + 3) which is digitalRead(4).
Which input would analogRead attempt to read in this code: analogRead(analogPin + 3);
4
It would try to read input 4 since analogPin = 1, digitalRead(analogPin + 3) is the same as digitalRead(1 + 3) which is digitalRead(4).
The Serial.println command prints the numerical content of the variable value in this program. If 1.25 VDC is applied to analog input 1, what value would be printed?
The Serial.println command prints the numerical content of the variable value in this program. If 1.25 VDC is applied to analog input 1, what value would be printed?
256
Since the maximum analog input is 5 VDC which gives a reading of 1023, we can use a proportion to find the reading of 1.25 VDC. . Solving this proportion gives x is 255.75, which rounds to 256.
The Serial.println command prints the numerical content of the variable value in this program. If 1.25 VDC is applied to analog input 1, what value would be printed?
256
Since the maximum analog input is 5 VDC which gives a reading of 1023, we can use a proportion to find the reading of 1.25 VDC. . Solving this proportion gives x is 255.75, which rounds to 256.
Which input would be read if we used the code analogRead(analogpin);?
Compile error
The variable name analogpin is not defined. Recall that variables are case sensitive.
Which input would be read if we used the code analogRead(analogpin);?
Compile error
The variable name analogpin is not defined. Recall that variables are case sensitive.
What is the range of values analogPin can have in this example?
What is the range of values analogPin can have in this example?
0 to 1023
The analogPin variable could be any of the numbers from 0 to 1023. Note that 1024 is outside the range of what can be read (since we only have a 10 bit converter).
What is the range of values analogPin can have in this example?
0 to 1023
The analogPin variable could be any of the numbers from 0 to 1023. Note that 1024 is outside the range of what can be read (since we only have a 10 bit converter).
True or False: We could simplify the main function to the following single line: Serial.println(analogRead(analogPin));.
True or False: We could simplify the main function to the following single line: Serial.println(analogRead(analogPin));.
True
This would work (assuming we always wanted to print the value of analogPin instead of being able to easily comment that line out for when we are not debugging). The line Serial.println(analogRead(analogPin)); would first read the value of pin number analogPin. Then it would print that value.
True or False: We could simplify the main function to the following single line: Serial.println(analogRead(analogPin));.
True
This would work (assuming we always wanted to print the value of analogPin instead of being able to easily comment that line out for when we are not debugging). The line Serial.println(analogRead(analogPin)); would first read the value of pin number analogPin. Then it would print that value.
True or False: In this example, value could have been of type Char (an 8-bit data type).
False
A Char type would not be large enough to hold the 10-bit date created by the converter.
True or False: In this example, value could have been of type Char (an 8-bit data type).
False
A Char type would not be large enough to hold the 10-bit date created by the converter.
If we changed the code in the main function as written below, what would be printed if the analog input had 2.5 VDC applied to it?
Serial.println(value);
value = analogRead(analogPin);
If we changed the code in the main function as written below, what would be printed if the analog input had 2.5 VDC applied to it?
Serial.println(value);
value = analogRead(analogPin);
0
Since we set variable value = 0 when we initialized it and we are printing it before it is assigned the value from the analogRead function, it will still be 0 and that is what will be printed.
If we changed the code in the main function as written below, what would be printed if the analog input had 2.5 VDC applied to it?
Serial.println(value);
value = analogRead(analogPin);
0
Since we set variable value = 0 when we initialized it and we are printing it before it is assigned the value from the analogRead function, it will still be 0 and that is what will be printed.