Week 4 : Rangefinder

19th of February 2020

This week we’re working on the rangefinders. The rangefinder acts like the eyes of the robot, measuring distance. It functions by sending out sound signals from the transmitter and receives these sounds after it gets deflected back by the object. These signals travel at the speed of sound.

One of the part controlled by the trigger pin sends out a pulse. That pulse is reflected off a surface in front of it, and that pulse is read by the part linked to the echo pin.

Drawing of Rangefinder

Code for Rangefinder

//Nathan Martin
//rangefinder code

//Change Pin names to identify them easier
int trigPin = 9;
int echoPin = 10;
long duration, cm, inches;

void setup()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT); //Rangefinder Pins
pinMode(echoPin, INPUT);
pinMode(3, OUTPUT); //Motor Pins
pinMode(4, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}

void loop()
{
long duration, distance; //defining variables

//pulse from HC-SR04
digitalWrite(trigPin, HIGH); //pulse for 20 micro seconds
delayMicroseconds(20);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = (duration/2)*0.034;

Serial.print(distance);
Serial.println(“cm”);

//check echo pulse
if (distance <20)
//(digitalRead(echoPin) == 1)
{

digitalWrite (3,LOW); //Set Both Motors to move “Clockwise” “Backwards”
digitalWrite (4,HIGH);
digitalWrite (6,LOW);
digitalWrite (7,HIGH);
delay(1000);

}
else
digitalWrite (3,HIGH); //Set Both Motors to move “Clockwise” “Backwards”
digitalWrite (4,LOW);
digitalWrite (6,HIGH );
digitalWrite (7,LOW);
{

}

}

Circuit diagram of how the rangefinder is connected to the Arduino by Nader Maloudi

Leave a comment

Design a site like this with WordPress.com
Get started