#include <Servo.h>

int s,state =3;
int IR=A0;
int dist;
int schalt=0;
Servo tServo;

void setup() {
  Serial.begin(9600);
  tServo.attach(6);

}

void loop() {
   while(s < 180 && state == 0){
 
 tServo.write(s);
 dist = analogRead(IR);
 Serial.print(" Winkel: ");
 Serial.print(s);
 Serial.print(" Abstand: ");
 Serial.println(dist);
 schalt++;
 if(schalt == 3){
 s=s+20;
 schalt = 0;
 }
 if ( s == 180){
state = 1;
 }
 delay(100);
 }
 
 while(s > 0 && state == 1){
 tServo.write(s);
 dist = analogRead(IR);
 Serial.print(" Winkel : ");
 Serial.print(s);
 Serial.print(" Abstand: ");
 Serial.println(dist);
 schalt++;
 if(schalt == 3){
 s=s-20;
 schalt = 0;
 }
 if ( s == 0){
 state = 0;
 }
 
 delay(100);
 }


}
