Question

In: Electrical Engineering

b. Copy the text below into your program. /* * Purpose: Read a 0V-5V value from...

b. Copy the text below into your program. /*

* Purpose: Read a 0V-5V value from analog pin A1

* and convert it to a 10-bit (0-1023) number. */

void setup() {

Serial.begin(115200); //set serial comm to 115 kbps }

void loop()

{ int pot_val; //input - potentiometer value

// analogRead() converts 0V-5V to 0-1023 pot_val = analogRead(1);

//read from Analog pin 1 Serial.println(pot_val); //print to serial monitor }

c. Add the function delay() from lab0 to make the readings 1x per second.

d. Write code to display formatted output on the serial monitor as shown below:

Voltage Integer Binary HEX

0.406 83 1010011 53

0.415 85 1010101 55 ...

(ii) Write a formula for converting an integer value (0 to 1023) to an analog voltage, where 0V corresponds to integer 0 and 5V corresponds to integer 1023.

Solutions

Expert Solution

b) The analogRead(pin) function reads the value from the specified analog pin. It returns a 10-bit integer that maps input voltages between 0 and 5 volts to 0 and 1023. More information about the function can be found here: https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/

The original program is:

/*
* Purpose: Read a 0V-5V value from analog pin A1
* and convert it to a 10-bit (0-1023) number.
*/

void setup() {
Serial.begin(115200); // set serial comm to 115 kBaud
}

void loop() {

int pot_val; // input - potentiometer value

// analogRead() converts 0V-5V to 0-1023
pot_val = analogRead(1); // read from Analog pin 1

Serial.println(pot_val); // print to serial monitor
}

c) A delay can be realized by using the delay function. It accepts a single argument, the delay in miliseconds. The program with the added delay functionality:

/*
* Purpose: Read a 0V-5V value from analog pin A1
* and convert it to a 10-bit (0-1023) number.
*/

void setup() {
Serial.begin(115200); // set serial comm to 115 kBaud
}

void loop() {

int pot_val; // input - potentiometer value

// analogRead() converts 0V-5V to 0-1023
pot_val = analogRead(1); // read from Analog pin 1

Serial.println(pot_val); // print to serial monitor

delay(1000);
}

d) We can make use of the second argument of the Serial.print() function to specify the format of the output. More information about the function can be found here: https://www.arduino.cc/en/Serial/Print

/*
* Purpose: Read a 0V-5V value from analog pin A1
* and convert it to a 10-bit (0-1023) number.
*/

void setup() {
Serial.begin(115200); // set serial comm to 115 kBaud
Serial.println("Voltage Integer Binary HEX");
}

void loop() {

int pot_val; // input - potentiometer value

// analogRead() converts 0V-5V to 0-1023
pot_val = analogRead(1); // read from Analog pin 1

Serial.print(pot_val * 5.0 / 1023);
Serial.print("\t");

Serial.print(pot_val);
Serial.print("\t");

Serial.print(pot_val, BIN);
Serial.print("\t");

Serial.println(pot_val, HEX);

delay(1000);
}

(ii)

Please leave a positive rating if the answer is satisfactory or leave a comment if more explanation is needed.


Related Solutions

Write a complete C program that read the text below and save the text in a...
Write a complete C program that read the text below and save the text in a new file "second.txt" with the same text written in all uppercase. "Beedle the Bard was an English wizard and author of wizarding fairytales. Beedle was born in Yorkshire, England. At some point in his life he wrote The Tales of Beedle the Bard . The only known image of Beedle is a woodcut that shows him with a "luxuriant" beard. Beedle wrote in ancient...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
1 Purpose This is another program for reading from files. In this program we will read...
1 Purpose This is another program for reading from files. In this program we will read blocks of data from a (well-formatted) file that you will be given and make some computations on that data. The input file will be formatted as described later in this document. It will be similar the “block” data from Lecture 10. 2 Procedure You will create one source code program, Prog07.cpp. This program will 1. Prompt the user for an input filename. Store the...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Read about the Perry Preschool Program in Ypsilanti, Michigan in your text and describe it to...
Read about the Perry Preschool Program in Ypsilanti, Michigan in your text and describe it to me using a good paragraph. List at least 4 good reasons to continue using the program. (5 pts.)
Design a program that will read each line of text from a file, print it out...
Design a program that will read each line of text from a file, print it out to the screen in forward and reverse order and determine if it is a palindrome, ignoring case, spaces, and punctuation. (A palindrome is a phrase that reads the same both forwards and backwards.) Example program run: A Toyota's a Toyota atoyoT a s'atoyoT A This is a palindrome! Hello World dlroW olleH This is NOT a palindrome! Note: You are only designing the program...
Below is an email that needs significant revision. Copy the text below into a Google Doc,...
Below is an email that needs significant revision. Copy the text below into a Google Doc, rewrite the entire email, and submit the link. Be sure to pay close attention to the subject line, clarity, structure, support, and voice. To: Department Managers From: Aaron Alexander <[email protected]> Subject:   Invitation to Help Interview Possible Interns Cc: Bcc:         You are invited to help us interview six excellent student candidates for three paid internships. These potential interns from our local state university...
i-Read about the Oregon parenting skills program (OSLC) in your text and describe it to me...
i-Read about the Oregon parenting skills program (OSLC) in your text and describe it to me using a good paragraph. List at least 3 good reasons to continue using the program. (4 pts.)
(C++) Write a program to read from a grade database (data.txt). The database (text file) has...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has students names, and grades for 10 quizzes.Use the given function prototypes to write the functions. Have main call your functions. The arrays should be declared in main() and passed to the functions as parameters. This is an exercise in parallel arrays, int and char 2 dim arrays. Function prototypes: int readData(ifstream &iFile, int scores[][10], char names[][30]); This functions takes the file stream parameter inFile...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT