This document describes two examples of using an Arduino to control a servo motor and ultrasonic sensor. The first example uses an ultrasonic sensor to detect obstacles less than 20cm away and turns a servo motor to the right if an obstacle is detected. If no obstacle is detected, the servo remains at a 90 degree angle. The second example uses multiple servo motors to control the movement of a robot. It uses an ultrasonic sensor to scan the area in front and on the sides to detect obstacles. If obstacles are detected, it compares the distances on the left and right to determine which direction to turn to find a clear path.
4. Örnek-1
#include <Servo.h>
#define trigpin 5//set trigpin
#define echopin 6//set echopin
Servo myservo;// declare servo name type servo
int duration, distance;//declare variable for unltrasonic sensor
void setup() {
Serial.begin(9600);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
myservo.attach(2);// attach your servo
myservo.writeMicroseconds(1500);
// put your setup code here, to run once:
}
void loop() {
myservo.write(90);// always set servo to 90 to position it to the
middle
//ultrasonic code
digitalWrite(trigpin,HIGH);
_delay_ms(500);
digitalWrite(trigpin, LOW);
duration=pulseIn(echopin,HIGH);
distance=(duration/2)/29.1;
if(distance <=20)// if ultrasonic sensor detects an obstacle less
than 20cm in 90 degree angle.
{
myservo.write(0); //servo rotates at full speed to the right
delay(600);
}
else
{
myservo.write(90);// else servo stays at 90 degree angle.
delay(600);
}
Serial.print("cm"); //print distance unit cm
Serial.println(distance);//distance
// put your main code here, to run repeatedly:
}
5. Örnek-2
#include <Servo.h> //include Servo library
const int RForward = 0;
const int RBackward = 180;
const int LForward = RBackward;
const int LBackward = RForward;
const int RNeutral = 90;
const int LNeutral = 90; //constants for motor speed
const int pingPin = 7;
const int irPin = 0; //Sharp infrared sensor pin
const int dangerThresh = 10; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;
Servo leftMotor;
Servo rightMotor; //declare motors
long duration; //time it takes to recieve PING))) signal
void setup()
{
rightMotor.attach(11);
leftMotor.attach(10);
panMotor.attach(6); //attach motors to proper pins
panMotor.write(90); //set PING))) pan to center
}
void loop()
{
int distanceFwd = ping();
if (distanceFwd>dangerThresh) //if path is clear
{
leftMotor.write(LForward);
rightMotor.write(RForward); //move forward
}
else //if path is blocked
{
leftMotor.write(LNeutral);
rightMotor.write(RNeutral);
panMotor.write(0);
delay(500);
rightDistance = ping(); //scan to the right
delay(500);
panMotor.write(180);
delay(700);
leftDistance = ping(); //scan to the left
delay(500);
panMotor.write(90); //return to center
delay(100);
compareDistance();
}
}
void compareDistance()
{
if (leftDistance>rightDistance) //if left is less obstructed
{
leftMotor.write(LBackward);
rightMotor.write(RForward); //turn left
delay(500);
}
else if (rightDistance>leftDistance) //if right is less obstructed
{
leftMotor.write(LForward);
rightMotor.write(RBackward); //turn right
delay(500);
}
else //if they are equally obstructed
{
leftMotor.write(LForward);
rightMotor.write(RBackward); //turn 180 degrees
delay(1000);
}
}
long ping()
{
// Send out PING))) signal pulse
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
//Get duration it takes to receive echo
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
//Convert duration into distance
return duration / 29 / 2;
}