Help with relays and proximity sensor.

I am stuck on this project where I have a proximity sensor (ping) which controls multiple relays. For example I at 100cm>0cm to activate relay1 and at 50cm>0cm activate relay 2. I sort of have it working but I want to know if there are functions that allow you hold the current state of an object for a specified amount of time. I want it to be able to still have the program running while the first sensor is activated instead of it holding and the program delay is 1second so if something interferes within 50cm it activates the relay immediately and not waiting for the delay time to end. This is the part of code I have got working but not perfectly how I want it:

if (cm < 100)
  {  digitalWrite (relay1, HIGH);
    delay(1000);}  
  if (cm < 50)
  {  digitalWrite (relay2, HIGH);
    delay(1000);}
  if (cm > 100)
    digitalWrite (relay1, LOW);
  if (cm > 50)
    digitalWrite (relay2, LOW);

The trick is not to use delay, but to make a note of the time you turn something on and so you know when to turn it off. See the blonk without delay example in the examples menu of the arduino software.

Grumpy_Mike: The trick is not to use delay, but to make a note of the time you turn something on and so you know when to turn it off. See the blonk without delay example in the examples menu of the arduino software.

yes but wouldnt that just keep on turning the relays on and off not only on when something is within the distance? And can you please put the code together so I can see how that function works with what I need?

yes but wouldnt that just keep on turning the relays on and off not only on when something is within the distance?

No, not if you write it correctly.

Emphasis on you

No, not if you write it correctly.

Ok I think I might know how to do it but not too sure. Like I said previously could you please show me?

Ok I think I might know how to do it but not too sure.

The great thing about software, unlike just about any physical technology, is that it is relatively easy, at this level, to build as many prototypes as you need to get it right. Read the blink without delay example and really understand what it is doing.

Simplify the problem whilst you are working the technique, like one range/relay - after that, it should be relatively simple to expand. Post your code AND your results if you get stuck.

Post your code AND your results if you get stuck.

Ok I didnt quite understand how the blink without delay worked but I tried this before the void setup() along with my other pins:

long previousMillis = 0; 
long interval = 1000;

and then in the void loop() I had:

unsigned long currentMillis = millis();
    
  if(currentMillis - previousMillis > interval) {
  previousMillis = currentMillis;   
    
  if (cm < 100)
  (relay1 = HIGH);
  else relay1 = LOW;
  if (cm < 50)
  relay2 = HIGH;
  else relay2 = LOW;

It all compiled fine but it didnt activate the relays but the led on the proximity sensor didnt stop when something came into the distance but the brightness of it did showing that it has found something within its distance so im not sure what to do and if someone could guide me along with this.

Have another look at reply #1

Yeh I looked at it and I didn't use the delay function and I didn't 100% understand the blink without delay sketch either. Once again if you could assist me on how to do it and/or link me to somewhere where it explains it in detail as the arduino.cc website on the millis function doesn't explain enough for me.

The important point made in reply #1 was to note the time that the event took place, not simply that you should not use delay. If you don't understand the blink without delay example (there are only about three important lines in it), then work at until you do understand it. Ask questions if there are bits you don't get.

Can you please assist me in telling me which part I have done correctly and what is wrong at the moment so I know what parts to try and fix? And what is it by noting the time the event took place?

Imagine I ask you to turn a light on in five minutes. You can use a five minute egg-timer, or you can use the clock on the wall, a pencil and a sheet of paper. [u]Egg-timer method:[/u] Turn the egg-timer over and sit watching it until the sand runs through. Switch on the light. This is the same as using "delay" - while you're watching the sand run through, you can't do anything else (aka blocking)

[u]Wall clock method[/u] Write down the time now. Look at the clock - is the time greater than five minutes after the time you wrote down? No, put the kettle on Look at the clock - is the time greater than five minutes after the time you wrote down? No, get a cup out Look at the clock - is the time greater than five minutes after the time you wrote down? Get the coffee out Look at the clock - is the time greater than five minutes after the time you wrote down? ..and so on.

"millis()" is the wall clock.

Ok I tried to do it and had to search around for things other people did and tweaked them to try and get them to work but still havent got it working…
I put this at the start along with everything else:

unsigned long timeOn = 5; 
unsigned long timeOfSensor = 0;

I put this in the void setup()digitalWrite (relay1, LOW);
and this in the void loop()

if (cm < 100)
   {
  timeOfSensor = millis();
  digitalWrite(relay1, HIGH);
   }
  else if (((timeOn * 1000 * 60) + timeOfSensor) < millis())
 {
 digitalWrite(relay1, LOW);

It doesnt work perfectly as when I interfere within 100cm it activates the relay and latches but doesnt let go. I think I am getting there but I am quite stuck. After I get one working I will need it to be working on 3 different relays at 3 different distances along with 4-5 proximity sensors. So I just need to get 1 working then ill go to 2/3 then ill start adding in the other sensors and come back here if I need help.

if (cm < 100)
  else if (((timeOn * 1000 * 60) + timeOfSensor) < millis())

These are not exclusive situations. Turning the relay on at the appropriate distance is not directly related to turning it off later.

My friend wrote the code for me and it works 100% and is sort of more simple than using the millis function. I understand almost all of what he wrote compared to this. But if someone could still write the code for me using millis it would still help me in the future. Thankyou

Actually the code I posted before did work but instead of multiplying it by 6 I left it timeOn * 1000 which changes the value I had into seconds.
So at the start I had unsigned long timeOn = 5; (5ms) so after multiplying it by 1000 it made it 5 seconds but before it was 30 seconds.

if (cm < 100)
   {
  digitalWrite(relay1, HIGH);
    timeOfSensor = millis();
   }
  else if (((timeOn * 1000) + timeOfSensor) < millis()) //timeOn (5) * 1000 = 5000 (5seconds)
 {
 digitalWrite(relay1, LOW);

In your first post, you said that you wanted to open a series of relays as the distance changed, AND you wanted to turn those relays off a specific amount of time later,

The code you have now does not do that. The way to do that would be to take the original code, and remove all the delays. Replace them with calls to millis, to record when the relays were activated.

After that series of if statements, add some more code to test the length of time each relay has been active, and turn them off if enough time has passed.

It isn't clear whether the relay should be turned off if whatever is being sensed is still in range, or whether the relay should be turned off only if whatever is being sensed is no longer in range.

As far as deciding whether it is time to turn them off, it makes no difference.

It matters whether you set the time the relay was turned on every time the sensor sees the object at that range, or only when the sensor sees the object at that range and the relay is not already on.

actually it does work believe it or now but I may need help with connecting multiple sensors at different distances

actually it does work believe it or now but I may need help with connecting multiple sensors at different distances

I don’t.

What help do you need? Simply repeat that code (in a function would be best) for each new sensor.

Though why you need to this escapes me, since you were using one sensor to test different distances before.

It works iv got it 100% working if you dont believe me test it yourself if you cant then you cant keep believing it doesnt. But post how you think it would work