4. What will the following Arduino code do? Please explain in a few words. (4 points) You may want to revisit the Arduino coding assignments from previous weeks for this question. In this setup, a motor is connected to an L298n board, and the board is connected to an Arduino by pin11 and pin 10. Thus , these two pins control the motor. A distance sensor (HCSR04) is also connected to Arduino. Its trigger is connected to pin 2, and echo is connected to pin 3. Here is the code: int motorPin1 = 11; int motorPin2 = 10; int trigger_pin = 2; int echo_pin = 3; long pulse_duration, distance_in_inches; void setup() { pinMode(motorPin1, OUTPUT); pinMode(motorPin2, OUTPUT); pinMode(trigger_pin, OUTPUT); pinMode(echo_pin, INPUT); Serial.begin(9600); } void loop() { // read the distance digitalWrite(trigger_pin, LOW); delayMicroseconds(2); digitalWrite(trigger_pin, HIGH); delayMicroseconds(10); digitalWrite(trigger_pin, LOW); pulse_duration = pulseIn(echo_pin, HIGH); distance_in_inches = pulse_duration / 147.82; Serial.println(distance_in_inches); delay(10); // do stuff if (distance_in_inches > 10) { digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, LOW); } else { if (distance_in_inches < 2) { digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, HIGH); } else { digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); } } }
Added by Linda C.
Close
Step 1
The code sets up the pins for the motor (motorPin1 and motorPin2) and the distance sensor (trigger_pin and echo_pin). Show more…
Show all steps
Your feedback will help us improve your experience
Supreeta N and 91 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Connecting a circuit and writing codes for PROCESSING AND ARDUINO
Carson M.
Reading results of two thermal sensors are shown in the table below in which reading #1 represents the ideal output value of the sensors and #2 and #3 represent two different readings from same sensor. Considering Sensor 1 and Sensor 2 in the table, explain and prove by numbers (a) Which sensor has a higher sensitivity? (5 marks) (b) Explain how do you compare precision and accuracy of these sensors? (10 marks) (c) Compare offset of these sensors? (5 marks) (d) Using a block diagram, show how do you connect any of these sensors to a microcontroller which has a 5V power supply? What are necessary blocks between your sensor and the microcontroller? (5 marks)
Sri K.
a) An analog-to-digital converter (ADC) measures voltages in the range of 0 to 25 V and has 12-bit accuracy. What is the smallest voltage step that the ADC can resolve? b) A ramp-type ADC system uses a 10 MHz clock generator and a ramp voltage that increases from 0 V to 1.25 V in a time of 125 ms. Determine the number of clock pulses counted into the register when V = 0.9 V, and when it is 0.75 V. c) If 3.45 V is applied to a 4-bit successive-approximation-type A/D converter which has a reference voltage of 5 V, what will be the digital output of the ADC? d) An 8-bit ADC is converting a temperature signal which has a measuring range of 0 °C to 800 °C. Calculate the resolution of the temperature-measuring instrument. e) Determine the number of output bits required for an ADC so that quantizing error is less than 1%. f) The resistance of a thermistor is 800 Ω at 50°C and 4 kΩ at the ice point. Calculate the characteristic constants (A, B) for the thermistor and the variations in resistance between 30°C and 100°C.
Adi S.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Transcript
Watch the video solution with this free unlock.
EMAIL
PASSWORD