HC-SR04 to relay with timed delayed high/low response

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

Do you want a canned solution, or do you want to learn how to do it?

I think the logic is pretty simple:

repeat
{
  if object detected
  {
  relay on
  wait 1 second
  relay off
  wait x seconds
  }
}

Hi, im trying to learn code as I go with various small projects. Ill give your code a try, its all a bit new to me, but I think im slowly getting the hang of it, thank you.

thanks, but this hasn't worked im afraid.

What is the "this" that hasn't worked? Nobody except yourself can see the code.

I hope you didn't try to compile my pseudocode? :slight_smile: :slight_smile: :slight_smile:

yeah.... so that's what I did. like I said, im very new to this game

Well, you need to apply the lens of plausibility to things. Do you think that a dumb machine could apply a command like

if object detected

unambiguously? What object, detected how?

Coding is complex sometimes but it doesn't defy common sense.

Pseudocode is a way of expressing programming ideas that need to be translated into code.

If you can't translate what I posted into some attempt at code, you are simply way too far ahead of yourself. If so, you need to go follow a linear learning path, so that you fully understand or gain full understanding of each aspect, as you go along.

The Arduino ecosystem is specifically tailored to that. Don't skip lessons.

Finally,

so that's what I did

You should always report what you did. We can't see over your shoulder, what you are doing.

As a simple exercise, translate

  wait 1 second

into an Arduino command... your response will help me understand what level to address your questions at.

Further to that, did you write the code that you originally posted?

Additionally, please answer the first question in reply #2.

Thanks for your time. Now looking at what you suggested and re-reading back our conversation I realise that I was being a little silly in literally copy and pasting your suggestion. I didn't write the code, I found a code online that could make the sensor work with a relay. The more threads I read and the more im playing with code, the more im learning how it works, the code I showed you I have played with along other codes with switches to relays etc etc and managed to grasp a very basic understanding on how it works. Im literally two weeks into looking at arduinos and the code that runs/controls them. im currently watching countless videos, reading threads and asking many questions to make sense of arduino logic, I understand that this can be infuriating to some, but we do have to start somewhere. Already today I have learnt a lot about flags, the use of common sense in a very literal way to describe what you want the machine to do.

To summarise, I haven't got a clue, but im trying to make sense out of it all.

almost think of it like this, when you're in another country and you kind of understand the written language, but you haven't a clue how to speak it. That's where im at.

It is as though you didn't register what I said. You are bringing the cluenessness on yourself, by not following a real learning program. Playing is fine, but not by itself.

I'm not infuriated (yet :slight_smile: ) but it saddens me to see people floundering around in junk approaches to learning. Most of the stuff you see in videos, etc., is produced by people who really don't have much of a clue. How can you really learn from that, other than the wrong ways to do things?

Have you worked through the IDE example sketches yet? Tutorials on this site? The plethora of C/C++ reference and tutorial sites?...

we do have to start somewhere

I did. We all did. But it's not where we start, it's where we go. To me, it seems like you must have skipped over some basics.

when you're in another country and you kind of understand the written language, but you haven't a clue how to speak it

It actually happened to me, for 2 years... however I bought some study books, began to systematically learn the language. Starting from the numbers 1-10... and I had to learn the basics quickly, for things like giving taxi cabs directions...

All the basic Arduino features and functions are documented on this site in reference section. By going back and forth between those and the example sketches, you can gain an understanding of how they work

Then you might be able to fully understand some larger, random sketch on the internet (poorly written and undocumented except for chatter and self aggrandizement).

When you go to a restaurant, do you go in the front door, or to the bin out back, for the best food?

What I want you to do:

  1. read the pseudocode and try to understand the logic. It's written for humans.
  2. think about each line, what it does and how it would be implemented in Arduino language
  3. try coding it. Don't mess with a random internet sketch. Assemble it logically, step by step from fundamentals.

There is not much in the requirements that is subtle or difficult, conceptually. It's actually a very pedestrian application. So give it a try.

ive literally just cracked it, I added an additional line

digitalWrite(relayPin, LOW);
delay(5000);

and it does what I need it to do after I set the following at the start

const int relayPin = 2;
int relayState = LOW;

the LED tutorial actually helped. I think you have to put your mind into line with how the logic works, its starting to make sense.. SLOWLY! Thank you :slight_smile:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.