#include <Ultrasonic.h>
#include <Servo.h>  
Ultrasonic ultrasonic(2,3); //Init an Ultrasonic object
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
int distance = 0;
int angle = 0;  // variable to store the servo angel 
int ledPin = 7;

void setup() {
  // put your setup code here, to run once:
     myservo.attach(5);  // attaches the servo on pin 5 to the servo object 
     myservo.write(10);
     pinMode(ledPin,OUTPUT);
     Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
  distance=ultrasonic.Ranging(CM);//get the current result;
  Serial.println(distance);
  delay(1000);
  if(distance<10){
      if(angle != 180){
      myservo.write(180);
      angle = 180;
      }
      digitalWrite(ledPin,LOW); 
      delay(2000);
    }
   else{
        if(angle != 10){
        myservo.write(0);
        angle = 0;
        }
        digitalWrite(ledPin,HIGH);
    }
}
