The problem is that after the relay is triggered, it is switched ON or OFF every loop. How do I trigger the relay only once and thane keep checking the sensor if the reading changes?
Use another variable to store the state of the relay. Use another (nested) if statement to check that the relay isn't already in the desired state before setting it.
The problem is that after the relay is triggered, it is switched ON or OFF every loop.
Only if the distance changes. That code will turn the relay on every time the distance is 10 or less, but if the relay is on already, that shouldn't matter.
Using Tools + Auto Format would help with your random indenting. And, get rid of some of those blank lines. Group code together that belongs together. Use blank lines between blocks of code.
Finally, the code to operate the sensor belongs in a function called from loop, not in loop.
Thanks for the great advice about the formatting! It really helps to clear and to make it more readable.
So in a sense, if the relay is ON then it makes no difference in the program is trying to switch it ON again and again? I actually hear it make a low clicking sound every 1/2 second. Not as loud as when it turns ON/OFF but there is a noise.
I made some changes to the code, but can't get it to work. Would you mind having a look and pointing me to the right direction?
So in a sense, if the relay is ON then it makes no difference in the program is trying to switch it ON again and again? I actually hear it make a low clicking sound every 1/2 second. Not as loud as when it turns ON/OFF but there is a noise.
You need to tell us more about this relay, then. If it is a latching relay, setting the pin HIGH again will cause the latched relay to latch again, which is possibly what you are hearing.
It is not difficult to keep track of the state that you intend for the relay to have, and not call digitalWrite() if the destination state is the same as the current state.
but can't get it to work. Would you mind having a look and pointing me to the right direction?
I looked at it, but I can't discern what it actually does. You need to describe that.
Of course, there are problems.
if (distance <= 10 && state = 0)
Assigning a value to state here is probably not what you intend. = != ==.
The local variable state will go out of scope at the end of loop(). So, the value is not persisted. You want either a global variable or a static variable.
If you only want the relay to turn on ONE time, then why not just use a DPDT relay and use the other set of contacts so the relay can self-supply. When it comes on once, it will stay on forever.
The Idea of the setup is to activate the relay when ever the sensor indicates a proximity less then 10cm, and to deactivate when the reading is more then 10cm.
if (distance <= 10 && state = 0) Here I wanted to create an if loop that will turn the relay on only when the reading is less then 10cm and in the var "state" is 0 (that means that the relay is off) after running this loop the var "state" will be set to 1 and the next time this if loop will not execute. This way the relay will no be turned on time after time.
obi-1:
if (distance <= 10 && state = 0) Here I wanted to create an if loop that will turn the relay on only when the reading is less then 10cm and in the var "state" is 0 (that means that the relay is off) after running this loop the var "state" will be set to 1 and the next time this if loop will not execute. This way the relay will no be turned on time after time.
Ifs don't loop, and as Paul said, if you're trying to compare state to 0, then you need the equality operator (==) not the assignment operator (=).