Pages: 1 [2]   Go Down
Author Topic: Help with relays and proximity sensor.  (Read 1358 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 64
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
if (cm < 100)
   {
  digitalWrite(relay1, HIGH);
    timeOfSensor = millis();
   }
  else if (((timeOn * 1000) + timeOfSensor) < millis()) //timeOn (5) * 1000 = 5000 (5seconds)
 {
 digitalWrite(relay1, LOW);
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 64
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 64
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: 1 [2]   Go Up
Jump to: