Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me to add comments to every line of this code. It's a C code for Arduino. #include // includes the LiquidCrystal Library LiquidCrystal

Please help me to add comments to every line of this code. It's a C code for Arduino.

#include // includes the LiquidCrystal Library

LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)

const int trigPin = 13;

const int echoPin = 12;

#define piezoPin 8

#define redLed 10

#define blueLed 11

long duration;

long distance;

int distanceCm, distanceInch;

int normalDistance = 200;

boolean triggered = false;

void setup() {

lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(redLed, OUTPUT);

pinMode(blueLed, OUTPUT);

pinMode(piezoPin, OUTPUT);

digitalWrite(redLed, HIGH); // to indicate that the calibration is in process

digitalWrite(blueLed, HIGH);

digitalWrite(piezoPin, HIGH);

while (millis() < 5000) {

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration / 2) / 29.1;

if (distance < normalDistance) {

normalDistance = distance;

}

}

digitalWrite(redLed, LOW); // finish calibration

digitalWrite(blueLed, LOW);

digitalWrite(piezoPin, LOW);

}

void loop() {

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distanceCm= duration*0.034/2;

distanceInch = duration*0.0133/2;

lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed

lcd.print("Distance: "); // Prints string "Distance" on the LCD

lcd.print(distanceCm); // Prints the distance value from the sensor

lcd.print(" cm");

delay(10);

lcd.setCursor(0,1);

lcd.print("Distance: ");

lcd.print(distanceInch);

lcd.print(" inch");

delay(10);

if (triggered) {

digitalWrite(redLed, HIGH);

digitalWrite(blueLed, LOW);

tone(piezoPin, 635);

delay(500);

digitalWrite(redLed, LOW);

digitalWrite(blueLed, HIGH);

tone(piezoPin, 912);

delay(500);

if (distanceInch >=25) {

triggered = false;

noTone(piezoPin);

digitalWrite(redLed, LOW);

digitalWrite(blueLed, LOW);

noTone(piezoPin);

}

} else {

long duration, distance;

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

if (distanceInch < 25) {

triggered = true;

}

delay(20);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle RMAN For Absolute Beginners

Authors: Darl Kuhn

1st Edition

1484207637, 9781484207635

More Books