Author Topic: Arduino with a relay and two obstacle sensors.  (Read 2345 times)

0 Members and 1 Guest are viewing this topic.

Offline geobeeTopic starter

  • Contributor
  • Posts: 47
  • Country: au
Arduino with a relay and two obstacle sensors.
« on: July 04, 2021, 10:41:54 pm »
Hi all, trying to get my head around this. I need to trigger a relay from Arduino, from two IR sensors so that the first trigger latches the relay, then the second trigger from the other sensor, unlatches the relay. In other words, a toggle function. I have searched the web, and there are plenty of ''momentary'' relay drivers, but cannot find what suits. I am a total noob to Arduino coding. I can do simple sketch mods, but am still learning at a very low pace. Am into model trains, and have quite a few Arduino items that I would like to get going. Any help or sketch ideas would be a great help.
 

Offline themadhippy

  • Super Contributor
  • ***
  • Posts: 2569
  • Country: gb
Re: Arduino with a relay and two obstacle sensors.
« Reply #1 on: July 04, 2021, 11:18:52 pm »
I'd use something like

assign  a variable,
if   first trigger is high make  the variable  =1
if  second trigger from the other sensor is high  make  the variable 0
else variable = variable
set the output pin to the value of the variable
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12855
Re: Arduino with a relay and two obstacle sensors.
« Reply #2 on: July 04, 2021, 11:57:10 pm »
If the sensors have a simple logic or dry contact output, and *ALL* you need to do is latch/unlatch the relay, you don't even need an Arduino.  Its a job for a SR flipflop which can be implemented with a single CMOS 4000 series logic IC + a driver transistor for the relay, or even by the BJT incarnation of the classic Eccles Jordan Bistable circuit, probably with an extra transistor to get strong relay drive.  See: https://en.wikipedia.org/wiki/Flip-flop_(electronics)

However if you *MUST* do it in code, it will probably look something like this:
Code: [Select]
void loop(){
   while(!digitalRead(SENSOR1)){}; // wait for sensor 1 to activate
   digitalWrite(RELAY, ON);
   while(digitalRead(SENSOR1)){}; // wait till sensor 1 is inactive

   while(!digitalRead(SENSOR2)){}; // wait for sensor 2 to activate
   digitalWrite(RELAY, OFF);
   while(digitalRead(SENSOR2)){}; // wait till sensor 2 is inactive
}
+ a bunch of #defines for the pin numbers for RELAY, SENSOR1, SENSOR2, and the logic levels for ON and OFF. It will also need some stuff in setup() to set the pin directions and whether or not to use input pullups.  Note that the above assumes active high sensor outputs. If they are active low, swap the ! in the while() expressions to the other pair of while() statements.
   
Themadhippy's idea of using a variable to store the relay state is necessary if you want a single Arduino to read multiple pairs of sensors to control multiple relays independently, looping through updating them each in turn fast enough you cant perceive the delay*, but it over-complicates the code if handling two sensors and a single relay is the Arduino's *ONLY* task. 

* You'd need to grok state machines and superloops which can be quite an adventure for less experienced programmers to make the multi-task version work well.
« Last Edit: July 05, 2021, 12:06:01 am by Ian.M »
 

Offline geobeeTopic starter

  • Contributor
  • Posts: 47
  • Country: au
Re: Arduino with a relay and two obstacle sensors.
« Reply #3 on: July 05, 2021, 02:44:37 am »
Thanks to both, I should be able to get something happening from that. Cheers
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4028
  • Country: nz
Re: Arduino with a relay and two obstacle sensors.
« Reply #4 on: July 05, 2021, 04:21:17 am »
Hi all, trying to get my head around this. I need to trigger a relay from Arduino, from two IR sensors so that the first trigger latches the relay, then the second trigger from the other sensor, unlatches the relay. In other words, a toggle function. I have searched the web, and there are plenty of ''momentary'' relay drivers, but cannot find what suits. I am a total noob to Arduino coding. I can do simple sketch mods, but am still learning at a very low pace. Am into model trains, and have quite a few Arduino items that I would like to get going. Any help or sketch ideas would be a great help.

The good thing about doing things in code is you can implement arbitrary complex logic very easily.

Maybe the bad thing about it is you should specify what you want more precisely.

- is it possible for the IR sensors to give a continuous "object detected" output, or is it inherently momentary?

- if it is possible to be continuous, what do you want to happen if the second signal is received while the first is still active?

- what do you want to happen if the first IR detector triggers multiple times before the second one triggers? If the first one triggers three times (say) should a single trigger of the second one turn the relay off, or only after three triggers?

- should the relay change state at the positive edge of both detectors, or at the negative edge of one (or both) of them?

- if a detector can trigger several times rapidly, is there a time period within which multiple triggers should be considered to be just one?

- is there a minimum time the relay should be on or off for?
« Last Edit: July 05, 2021, 04:24:32 am by brucehoult »
 

Offline geobeeTopic starter

  • Contributor
  • Posts: 47
  • Country: au
Re: Arduino with a relay and two obstacle sensors.
« Reply #5 on: July 05, 2021, 09:59:36 am »
Hi, it needs to latch the relay when the first sensor is triggered, then unlatch it when the second one is triggered, and so forth.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4028
  • Country: nz
Re: Arduino with a relay and two obstacle sensors.
« Reply #6 on: July 05, 2021, 11:55:38 am »
Yes, you said that in the first message.
 
The following users thanked this post: Ian.M

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12855
Re: Arduino with a relay and two obstacle sensors.
« Reply #7 on: July 05, 2021, 12:22:32 pm »
Hi, it needs to latch the relay when the first sensor is triggered, then unlatch it when the second one is triggered, and so forth.
@geobee,
Perhaps if I explain the assumptions I made (for the code in reply #2) you can see how they differ from what you just stated.

My assumptions: 

   The sensors output an active high pulse when triggered long enough for the Arduino to easily respond to.  The maximum pulse duration is unknown.

   The initial (power-up or after reset) state of the relay is off.

   The relay should turn on when the first sensor becomes (or is) active, remaining on while the first sensor is still active.

   Once the first sensor goes inactive, the relay should turn off when the second sensor becomes (or is) active, remaining off while the second sensor is still active. 

   Once the second sensor goes inactive, repeat ad nauseam.

That implies that if both sensors are active at the same time (i.e. their pulses overlap), whichever came first initially 'wins' then as they become inactive, whichever is last to become inactive 'wins'.

I haven't even covered any requirement for debouncing.  If you were dealing with mechanical switches or interrupted beam IR sensors you might choose to specify *either* 'Any change of sensor state shall be ignored until it has been stable for 10 ms.' *or* 'A change of sensor state shall be acted on immediately then further changes for the same sensor ignored until it has been stable for 10 ms, then acted on if required' or similar with a different debouncing period.

As you can see, specifications for code in human language can get rather complex if you need to nail down all ambiguities.
« Last Edit: July 05, 2021, 12:26:46 pm by Ian.M »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf