#include "NewPing.h"
#include <Servo.h>
#define trigPin 13
#define echoPin 12
#define MAX_DISTANCE 500
const int blaster = 11;

NewPing sonar(trigPin,echoPin,MAX_DISTANCE);

int angle = 0;

Servo servo;


void setup() {
  Serial.begin(115200);
  pinMode(blaster, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  servo.attach(10);
  // put your setup code here, to run once:

}

void loop() {
  int duration, distance,pos = 0, i;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  Serial.print(distance);
  Serial.println("cm");
  if (distance <= 15) {
    digitalWrite(blaster, LOW);
    servo.write(90);  
  }
  else {
    digitalWrite(blaster, HIGH);
    for (angle = 0; angle < 180; angle++){
      servo.write(angle);
      delay(15);
    }
    for (angle = 180; angle > 0; angle--){ 
    }
    delay(450);
  }
  // put your main code here, to run repeatedly:

}