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,...
I have an arduino code for the line following car robot with CCD camera. However, I...
I have an arduino code for the line following car robot with CCD camera. However, I want to know what if ((tt++) % 10 < 5) is actually meaning. I am a beginner with the programming. can you explain it?? thanks. void loop() { if ((tt++) % 10 < 5) <<<<<<<<<<<<<<<<<------------------------- <<Here what is the meaning of this part??>> {    mainPage(1); mainPage(2); mainPage(3); mainPage(4);     mainPage(5); mainPage(6); } motorMOVE(FW, 130, 130);      Chk_speedX(&speedL, &speedR);      // tracking algoritm      tracking_algoritm();      Cam_plot(0); // clear      getCameraZ(); //...
Using the following code answer the next couple questions: #include<stdio.h> #include<stdlib.h> #include<string.h> /* Rewrite using a...
Using the following code answer the next couple questions: #include<stdio.h> #include<stdlib.h> #include<string.h> /* Rewrite using a pointer to char str[] */ void array_to_ptr () { int n=0, len; char str[ ] = "Hello World!"; len = strlen(str); for( n=0; n<len; n++) {     putc(str[n], stdout); } printf("\nlength = %d\n", n); } int contains (char * str, char c); int * makearray(int n); int main (void) { printf("Question #2 - array_to_ptr:\n"); array_to_ptr();   printf("\n------------------------------------\n\n"); printf("Question #3 - contains:\n"); printf("Test #1: "); if...
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; } /*...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output for proof. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Implement the following functions: Implement a function called readData int readData( int *arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr....
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...
Discuss the relationship between the C code and the Assembly code generated by the compiler??? Especially...
Discuss the relationship between the C code and the Assembly code generated by the compiler??? Especially focus on the difference between declaration of different types of variables in assembly for example difference between the declaration of an integer variable and a character variable. URGENT PLEASE BELOW IS THE C CODE: //Toggling PORTB in C Language for Dragon12 Plus Trainer Board //with HCS12 Serial Monitor Program installed. This code is for CodeWarrior IDE //On Dragon12+ Trainer board 8 LEDs are connected...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT