Question

What will the following Arduino code do? Please explain in a few words.    In this...

What will the following Arduino code do? Please explain in a few words.   

In this setup, a motor is connected to an H-bridge, which is controlled by pin11 and pin 10. A distance sensor is connected to pin 2.
Here is the code:

int  motorPin1 = 11; // Variables defined before setup and loop functions can be used anywhere in any function.     
int  motorPin2 = 10;   
int distance_sensor_pin = 2;  
long int pulse_duration, distance_in_inches;  

void setup() {     // The setup function   
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
Serial.begin(9600);  
}    // end of setup function   

void loop() {   // The loop function   
// read the distance
pinMode( distance_sensor_pin, OUTPUT) ;
digitalWrite( distance_sensor_pin , LOW);   
delayMicroseconds(2);
digitalWrite( distance_sensor_pin , HIGH);   
delayMicroseconds(5);   
digitalWrite( distance_sensor_pin , LOW);
pinMode( distance_sensor_pin , INPUT);   
pulse_duration = pulseIn( distance_sensor_pin , HIGH);
distance_in_inches = pulse_duration / 74 / 2;   
Serial.println(distance_in_inches);   
delay(200);  

// 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);
}
}
} // end of loop function

Homework Answers

Answer #1

Hello,
       Please find the answer attached below. If the answer has helped you please give a thumbs up

rating. Thank you and have a nice day!

The H bridge motor setup is used to change the direction of the motor by toggling the states of the two (or four) switches connected to the bridge. In this case, the control is achieved by toggling the motorPin1 and motorPin2 outputs. The toggling is in turn controlled by the measurement of the distance as reported by the distance sensor. If the distance sensor reads a distance of less than 2 inches, the motor is rotated in a particular direction and if the distance is greater than 10 inches, then the motor is rotated in the reverse direction. If the distance is between 2 and 10 inches, the H bridge is shut down (by openong both the switches), and the motor does not rotate.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
What will the following Arduino code do? Please explain in a few words. In this setup,...
What will the following Arduino code do? Please explain in a few words. In this setup, a motor is connected to an H-bridge, which is controlled by pin11 and pin 10. A distance sensor is connected to pin 2. Here is the code: int  motorPin1 = 11; // Variables defined before setup and loop functions can be used anywhere in any function.      int  motorPin2 = 10;    int distance_sensor_pin = 2;   long int pulse_duration, distance_in_inches;   void setup() {     // The setup...
What will the following 2 Arduino code print on the serial monitor?   int x =1 ;...
What will the following 2 Arduino code print on the serial monitor?   int x =1 ; void setup() {        Serial.begin(9600);   }    void loop() {        Serial.println(x);   x = x+1; }        And int x =1 ; void setup() {        Serial.begin(9600);   }    void loop() {    if (x < 10) {    Serial.println(x);   x = x+1; } else {   Serial.println(x);   } }
I have wrote this code for arduino to turn the LED light off when an object...
I have wrote this code for arduino to turn the LED light off when an object is not detected and the LED light on when an object is detected, although the code is not working. How can I improve this? #define trigPin 12 #define echoPin 11 int ledPin= 1; //Connect LEd pin to 6 int duration, distance; //to measure the distance and time taken void setup() { Serial.begin (9600); //Define the output and input objects(devices) pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(ledPin,...
q : explain the code for a beginner in c what each line do Question 2....
q : explain the code for a beginner in c what each line do Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly ? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++)...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue...
QUESTION 1 What does the following code segment output? int red, blue; red = 7; blue = red + 2 * 5 red++; blue = blue + red; cout << blue; 4 points    QUESTION 2 Is the following statement true or false? The Boolean expression in the following if statement will be true for all values of x in the range from 10 to 20 (including the endpoints) and false for all other values: int x; if (x >=...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT