Question

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, OUTPUT);
}

void loop() {

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
//when distance is greater than or equal to 200 OR less than or equal to 0,the buzzer and LED are off
if (distance >= 200 || distance <= 0)
{
Serial.println("no object detected");

digitalWrite(ledPin,LOW);
}
else {
Serial.println("object detected \n");
Serial.print("distance= ");   
Serial.print(distance); //prints the distance if it is between the range 0 to 200

digitalWrite(ledPin,HIGH);
}
}

Homework Answers

Answer #1

Answer :- The code is correct logically. But as "distance & duration" have been defined with data type "int" it may fail to run as required. Note that function "pulseIn" returns value of type "unsigned long". So these two variables must be of type "unsigned long".

Second, inside " if " we should test with condition "distance >= 200 || distace < 0" and not less than equal to zero. Most of the time variable distance will have value in between 0 and 1 but due to "int" type it will be made to zero which will make condtion to be true and led will not glow.

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...
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...
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...
When I wrote this code in the Eclipse program, I did not see a output .....
When I wrote this code in the Eclipse program, I did not see a output .. Why? ___________________ public class YClass { private int a; private int b; public void one(){ } public void two ( int x , int y ) ; a=x; b=y; } public YClass() { a=0; b=0; } } class XClass extends YClass{ private int z; public void one() { } public XClass { z=0; } } YClass Object; XClass Object;
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
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...
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's...
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's length, width, and area Define an overloaded constructor and use it when creating the Rectangle object instead of using the setters. Change this program to calculate and display the rectangle's perimeter. Example: In feet, how wide is your house? 20 In feet, how long is your house? 25 The house is 20.00 feet wide. The house is 25.00 feet long. The house has 500.00...
So, i have this code in python that i'm running. The input file is named input2.txt...
So, i have this code in python that i'm running. The input file is named input2.txt and looks like 1.8 4.5 1.1 2.1 9.8 7.6 11.32 3.2 0.5 6.5 The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this Sample Program Output 70 - 510, [semester] [year] NAME: [put your name here] PROGRAMMING ASSIGN MENT #2 Enter...
Code Example 8-1 1. int count = 1; 2. int item_total = 0; 3. int item...
Code Example 8-1 1. int count = 1; 2. int item_total = 0; 3. int item = 0; 4. while (count < 4) { 5.      cout << "Enter item cost: "; 6.      cin >> item; 7.      item_total += item; 8.      ++count; 9. } 10. int average_cost = round(item_total / count); 11. cout << "Total cost: " << item_total << "\nAverage cost: " << average_cost; (Refer to Code Example 8-1.) If the user enters 5, 10, and 15 at the prompts, the output is: Total...
Question 1 Which statement is false about what Data Types defines Question 1 options: What values...
Question 1 Which statement is false about what Data Types defines Question 1 options: What values a variable cannot hold? How much memory will be reserved for the variable? What value a variable will hold? How the program will use the data type? Question 2 Using the structure below, which of the following statements about creating an array (size 20) of structures are not true? struct Employee{     string emp_id;     string emp_name;     string emp_sex; }; Question 2 options:...