Hi all,
Ive currently got a HC-SR04 connected to a Leonardo with a relay in a NO configuration set to within 125cm distance, this works perfectly in a sense that it triggers the relay to a closed position until the sensor path has been unobstructed, to which it the relay goes back to its NO/none triggered position.
Please see current code that works as described.
const int trigPin = 12;
const int echoPin = 13;
long duration;
int distance;
void setup()
{
pinMode (2,OUTPUT);
pinMode (trigPin,OUTPUT);
pinMode (echoPin,INPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(trigPin,LOW);
delayMicroseconds (2);
digitalWrite(trigPin,HIGH);
delayMicroseconds (10);
digitalWrite(trigPin,LOW);
duration = pulseIn(echoPin,HIGH);
digitalWrite(trigPin,HIGH);
distance = duration*0.034/2;
Serial.print("Distance: ");
Serial.println(distance);
if (distance <=125) //can be adjusted from 2cm to 400cm
{
digitalWrite(2,HIGH);
delay(100); // can be adjusted
}
else
{
digitalWrite(2,LOW);
delay(100); // can be adjusted
}
}
What I want to achieve is, when the sensor detects an object within range, I want the relay to flick on/off within a 1 sec interval like a blip. Then I want the sensor to ignore the range infringement/trigger for an amount of time (that can be changed when I like), then be able to be triggered again.
How do I achieve this? TIA