Advertisement
ptnega

Untitled

May 6th, 2015
2,634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Servo.h>
  2.  
  3. int echoPin = 2;
  4. int initPin = 3;
  5. unsigned long pulseTime = 0;
  6. unsigned long distance = 0;
  7.  
  8. Servo myServo;
  9. int servoPin = 9;
  10. int pos = 0;
  11.  
  12. void setup() {
  13.   // put your setup code here, to run once:
  14.   myServo.attach(servoPin);
  15.   pinMode(initPin,OUTPUT);
  16.   pinMode(echoPin,INPUT);
  17.   Serial.begin(9600);  
  18. }
  19.  
  20. void cal(){
  21.     digitalWrite(initPin,LOW);
  22.     delayMicroseconds(50);
  23.     digitalWrite(initPin,HIGH);
  24.     delayMicroseconds(50);
  25.     digitalWrite(initPin,LOW);
  26.    
  27.     pulseTime = pulseIn(echoPin,HIGH);
  28.     distance = pulseTime/58;
  29.     Serial.print(distance);
  30.     Serial.println();
  31. }
  32.  
  33. int cnt = 0;
  34.  
  35. void loop() {
  36.   // put your main code here, to run repeatedly:
  37.     cnt += 1;
  38.    
  39.     Serial.println("one");
  40.    
  41.     for ( pos = 0 ; pos < 180 ; pos += 1 ){
  42.        
  43.       myServo.write(pos);
  44.       cal();
  45.       delay(15);
  46.     }
  47.    
  48.     Serial.println("two");
  49.    
  50.     for ( pos = 180 ; pos > 0 ; pos -= 1 ){
  51.       myServo.write(pos);
  52.       cal();
  53.       delay(15);
  54.     }
  55.    
  56.   delay(100);
  57.   if (cnt == 2) return;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement