Advertisement
Guest User

Servo durdurma

a guest
Oct 16th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.74 KB | None | 0 0
  1. // Includes the Servo library
  2. #include <Servo.h>.
  3. // Defines Tirg and Echo pins of the Ultrasonic Sensor
  4. const int trigPin = 10;
  5. const int echoPin = 11;
  6. // Variables for the duration and the distance
  7. long duration;
  8. int distance;
  9. int b=0;
  10. int kosul1=1;
  11. int servokontrol;
  12.  
  13. int sk2;
  14.  
  15. Servo myServo; // Creates a servo object for controlling the servo motor
  16. void setup() {
  17.   pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  18.   pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  19.   Serial.begin(9600);
  20.   myServo.attach(12); // Defines on which pin is the servo motor attached
  21. }
  22. void loop() {
  23.   tara();
  24.   myServo.write(tara());
  25.  
  26. }
  27.  
  28.   int tara(){
  29.   int sk;
  30.  
  31. calculateDistance();
  32.  
  33.   if(distance>=20){  // ilk servo konumundaki mesafeye bakıyor.
  34.     kosul1=1;
  35.     }
  36.     else{
  37.       kosul1=0;
  38.     }
  39.  
  40.   while(kosul1=1){      // tarama döngüsü
  41.    
  42.     for( int i=50; i<100; i++){
  43.      
  44.         sk++;
  45.       myServo.write(i);
  46.       delay(10);
  47.       calculateDistance();
  48.       delayMicroseconds(20);
  49.      
  50.       // her açıdaki distance kontrolü
  51.       if(distance<20){
  52.        sk=i;        
  53.         kosul1=0;
  54.         break;  
  55.         }
  56.         //distance 20 den küçük değilse for döngüsü devam eder
  57.        }
  58.        
  59.        break;
  60.     }
  61.     return sk;
  62.     }//fonksiyon sonu
  63.  
  64.  
  65.  
  66.  int calculateDistance(){
  67.  
  68.   digitalWrite(trigPin, LOW);
  69.   delayMicroseconds(2);
  70.   // Sets the trigPin on HIGH state for 10 micro seconds
  71.   digitalWrite(trigPin, HIGH);
  72.   delayMicroseconds(10);
  73.   digitalWrite(trigPin, LOW);
  74.   duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
  75.   distance= duration*0.034/2;
  76.  
  77.   return distance;
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement