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
Changes from 2 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
34 changes: 34 additions & 0 deletions examples/ServoStop/ServoStop.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <Servo.h>
byte trigPin = 4;
byte echoPin = 2;
byte servoPin = 9;

Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
myservo.attach(servoPin); // attaches the Servo on pin 9 to the servo object
per1234 marked this conversation as resolved.
Show resolved Hide resolved
}

void loop() {
unsigned long duration;
unsigned int distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

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

Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");

if (distance < 17) {
myservo.write(0); // tell servo to go to position 0
} else {
myservo.write(90); // tell servo to go to position 90
}
}