Skip to content
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

Added a new Example : Servo Stop #45

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
59 changes: 59 additions & 0 deletions examples/ServoStop/ServoStop.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Set servo position to 0 when the ultrasonic sensor detects any object closer
than 17cm. Otherwise, set servo position to 90.


You can see the detailed description at https://github.com/arduino-libraries/Servo/blob/master/examples/ServoStop/ServoStop.md

The circuit:
- Ultrasonic Trig pin to digital pin 3 on arduino
- Ultrasonic Echo pin to digital pin 2 on arduino
- Servo pin to digital pin 4 on arduino

Example originally added on 21-03-2020
by Madhur Dixit https://github.com/Chester-King

*/


#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
}
}
26 changes: 26 additions & 0 deletions examples/ServoStop/ServoStop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# ServoStop

## Sketch Objective

Set servo position to 0 when the ultrasonic sensor detects any object closer
than 17cm. Otherwise, set servo position to 90.
by Madhur Dixit https://github.com/Chester-King

## Sketch Details and Working

**Connections:**

- Trig Pin on Ultrasonic sensor to GPIO 3 on Arduino
- Echo Pin on Ultrasonic sensor to GPIO 2 on Arduino
- Servo to GPIO 4 on Arduino
- Connect the Vcc of the Ultrasonic and Servo to 5V of Arduino
- Connect the Gnd of the Ultrasonic and Servo to Gnd of Arduino
- Upload the code to Arduino

## Result

Whenever any Object comes in 17cm Proximity of the Ultrasonic sensor the servo will set to the position 0 until the object leaves the ultrasonic sensor proximity.

## Required Circuit

You can see the required Circuit [here](https://github.com/arduino-libraries/Servo/blob/master/examples/ServoStop/ServoStop.png)
Binary file added examples/ServoStop/ServoStop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.