Distance measurement device sketch

Once you have assembled all the required parts together, load the following sketch. Before loading the sketch, make sure nothing is connected to Arduino's Tx (D1) and Rx (D0) pins.

The following sketch/code may be freely downloaded from the path mentioned in Chapter 1, Boot Camp. This is what a distance measurement sketch looks like.

//**********************************************************/ 
// Step-1: CONFIGURE VARIABLES
//**********************************************************/
#include <LiquidCrystal.h>
LiquidCrystallcd(12, 11, 5, 4, 3, 2); // create LCD object
int trigPin = 8;
int echoPin = 7;

//**********************************************************/
// Step-2: INITIALIZE I/O PARAMETERS
//**********************************************************/
void setup()
{
// one time setup code
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Distance:");
}

//**********************************************************/
// Step-3: MAIN PROGRAM
//**********************************************************/
void loop()
{
long duration, distance;
// Signal a quick LOW just before giving a HIGH signal
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// After 2 micro-seconds of LOW signal, give a HIGH signal
// to trigger the sensor
digitalWrite(trigPin, HIGH);
// Keep the digital signal HIGH for
// at least 10 micro-seconds
// (required by HC-SR04 to activate emission of
// ultra-sonic waves
delayMicroseconds(10);
// After 10 micro-seconds, signal a LOW
digitalWrite(trigPin, LOW);
// Now wait for the Ultra sonic echo to return from an
// obstacle
duration = pulseIn(echoPin, HIGH);
// Convert the distance to centimeters
distance = (duration/2) / 29.1;
// Print the distance on the Serial Monitor window
Serial.print(distance);
Serial.println(" cms");
// Clear the LCD
lcd.clear();
// Set the cursor position
lcd.setCursor(0, 0);
// Print the distance on the LCD screen
lcd.print("Distance (in cms):");
lcd.setCursor(0, 1);
lcd.print(distance);
// Check for distance again
delay(400);
}

Before operating the prototype, let's try to understand the sketch briefly:

The sketch starts with the customary section of including the Arduino header file for operating (or driving) the LCD panel using:

#include <LiquidCrystal.h> 

Version 1.0.5 of the library has been used in this chapter.

This is followed by defining a global variable to refer to the LCD panel. Throughout the sketch, this lcd variable is used to interact with the LCD panel.

LiquidCrystallcd(12, 11, 5, 4, 3, 2); 

By now, you will be able to understand the various lines of code in the setup() function. You will notice the familiar pinMode() function calls for configuring the pins interfacing with the ultrasonic sensor. The last few lines in the setup() function initialize the LCD panel.

lcd.begin(16, 2); 

The next line sets the location of the cursor to column 0 row 0 in the 16x2 LCD array.

lcd.setCursor(0, 0); 

The following line prints the Distance string on the LCD panel.

lcd.print("Distance:"); 

The following function call is used to read the duration taken by the ultrasonic waves to return to the HC-SR04 sensor.

duration = pulseIn(echoPin, HIGH); 

The pulseIn() function is used to read and measure the duration of an input signal on a particular pin. Based on the logic level specified as a parameter to the function, it will wait for and read the signal until its logic level changes. In the preceding statement, when reading a HIGH signal, it waits for a HIGH input signal and measures the duration until the signal level changes to LOW. This is how the sketch measures the duration taken by the ultrasonic waves to return to the HC-SR04 sensor.

The next step is to process and convert the duration into centimeters using the following logic:

distance = (duration/2) / 29.1; 

Eventually, clear the LCD screen.

lcd.clear(); 

As a last step, the sketch displays the distance on the LCD panel.

lcd.setCursor(0, 0); 

lcd.print("Distance (in cms):");

lcd.setCursor(0, 1);

lcd.print(distance);

After the sketch has been loaded successfully, unplug the Arduino from the USB B port (make sure the Arduino board is no longer connected to the computer).The next step is to connect all the assembled components to the Arduino board using the breadboard.

Once all the parts have been connected, plug in the 9-volt battery power source into Arduino's VIN and GND pins (as shown in the breadboard setup diagram). As soon as the power source is plugged in, the device prototype should get powered up.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.33.87