Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Includes the Servo library
- #include <Servo.h>.
- // Defines Tirg and Echo pins of the Ultrasonic Sensor
- const int trigPin = 10;
- const int echoPin = 11;
- // Variables for the duration and the distance
- long duration;
- int distance;
- int b=0;
- int kosul1=1;
- int servokontrol;
- int sk2;
- Servo myServo; // Creates a servo object for controlling the servo motor
- void setup() {
- pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
- pinMode(echoPin, INPUT); // Sets the echoPin as an Input
- Serial.begin(9600);
- myServo.attach(12); // Defines on which pin is the servo motor attached
- }
- void loop() {
- tara();
- myServo.write(tara());
- }
- int tara(){
- int sk;
- calculateDistance();
- if(distance>=20){ // ilk servo konumundaki mesafeye bakıyor.
- kosul1=1;
- }
- else{
- kosul1=0;
- }
- while(kosul1=1){ // tarama döngüsü
- for( int i=50; i<100; i++){
- sk++;
- myServo.write(i);
- delay(10);
- calculateDistance();
- delayMicroseconds(20);
- // her açıdaki distance kontrolü
- if(distance<20){
- sk=i;
- kosul1=0;
- break;
- }
- //distance 20 den küçük değilse for döngüsü devam eder
- }
- break;
- }
- return sk;
- }//fonksiyon sonu
- int calculateDistance(){
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- // Sets the trigPin on HIGH state for 10 micro seconds
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
- distance= duration*0.034/2;
- return distance;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement