Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- int echoPin = 2;
- int initPin = 3;
- unsigned long pulseTime = 0;
- unsigned long distance = 0;
- Servo myServo;
- int servoPin = 9;
- int pos = 0;
- void setup() {
- // put your setup code here, to run once:
- myServo.attach(servoPin);
- pinMode(initPin,OUTPUT);
- pinMode(echoPin,INPUT);
- Serial.begin(9600);
- }
- void cal(){
- digitalWrite(initPin,LOW);
- delayMicroseconds(50);
- digitalWrite(initPin,HIGH);
- delayMicroseconds(50);
- digitalWrite(initPin,LOW);
- pulseTime = pulseIn(echoPin,HIGH);
- distance = pulseTime/58;
- Serial.print(distance);
- Serial.println();
- }
- int cnt = 0;
- void loop() {
- // put your main code here, to run repeatedly:
- cnt += 1;
- Serial.println("one");
- for ( pos = 0 ; pos < 180 ; pos += 1 ){
- myServo.write(pos);
- cal();
- delay(15);
- }
- Serial.println("two");
- for ( pos = 180 ; pos > 0 ; pos -= 1 ){
- myServo.write(pos);
- cal();
- delay(15);
- }
- delay(100);
- if (cnt == 2) return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement