// Include NewPing Library
#include "NewPing.h"
// Hook up HC-SR04 with Trig to Arduino Pin 9, Echo to Arduino pin 10
#define TRIGGER_PIN 9
#define ECHO_PIN 10
const int RELAY_PIN = 3;
// Maximum distance we want to ping for (in centimeters).
#define MAX_DISTANCE 400
// NewPing setup of pins and maximum distance.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
int length=sonar.ping_cm();
Serial.print("Distance = ");
Serial.print(length);
Serial.println(" cm");
delay(500);
if(length<60){
digitalWrite(RELAY_PIN, HIGH);
delay(500);
}
else{
digitalWrite(RELAY_PIN, LOW);
}
}