diff --git a/examples/ServoStop/ServoStop.ino b/examples/ServoStop/ServoStop.ino new file mode 100644 index 0000000..0ece456 --- /dev/null +++ b/examples/ServoStop/ServoStop.ino @@ -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 +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 + } +} diff --git a/examples/ServoStop/ServoStop.md b/examples/ServoStop/ServoStop.md new file mode 100644 index 0000000..639454c --- /dev/null +++ b/examples/ServoStop/ServoStop.md @@ -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) diff --git a/examples/ServoStop/ServoStop.png b/examples/ServoStop/ServoStop.png new file mode 100644 index 0000000..dfc1005 Binary files /dev/null and b/examples/ServoStop/ServoStop.png differ