Skip to content

Added a new Example : Servo Stop #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions examples/ServoStop/ServoStop.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Setting servo position to 0 whenever any object distance
with the ultrasonic sensor is less than 17cm. Setting Servo position to 90 otherwise
by Madhur Dixit https://github.com/Chester-King

You can see the detailed description and required circuit for the sketch at https://gist.github.com/Chester-King/fc19b83777ff629b6708731f09eeaa6c
*/


#include <Servo.h>
byte trigPin = 3;
byte echoPin = 2;
byte servoPin = 4;

Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup() {
Serial.begin(9600); //opens serial port, sets data rate to 9600 bps
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
myservo.attach(servoPin); // attaches the servo on pin 4 to the Servo object
}

void loop() {
unsigned long duration;
unsigned int distance;

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance = (duration / 2) / 29.1;

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");

// Checks if the distance between the object is less than 17cm
if (distance < 17) {
myservo.write(0); // if the distance between the object is less than 17cm then tell servo to go to position 0
} else {
myservo.write(90); // if the distance between the object is greater than or equal to 17cm then tell servo to go to position 90
}
}