# Need help with multiple proximity sensors and relays.

Hey guys this is an addition to the previous post about a week ago but I am struggling to find a way to have multiple proximity sensors activating multiple relays at different distances please read below. I have tried to have 2 sensors activating the same relays at different distances but I cant really find a way to as the relays are depending on the "cm" value which is being read from the all the sensors not individual ones. So if I do try and set the relays off at different distances they go off and on no matter which sensor. So what I am trying to do is: Sensor 1: Sensor 2: At 100cm>0cm activate relay1 At 150cm>0cm activate relay1 and 50cm>0cm activate relay2 and 75cm>0cm activate relay2

and then this will be expanded up to 4-5 sensors and 3-4 relays. So right not I want to get the basics working with the minimum to test which will then allow me to expand onto multiple sensors and relays.

This is the code my friend wrote: http://pastebay.com/146241 and this is the code I wrote: http://pastebay.com/146242 I would prefer to use my friends code as it works slightly different to mine and mine uses the millis function which does count the time which overlaps.

Thanks to anyone who can find a way to get this to work.

It'd be better if you simply included your code in the body of your message.

You need to figure out the logic before you start programming. It looks to me like there are four cases: sensor1 TRUE and sensor2 TRUE, sensor1 TRUE and sensor2 FALSE, sensor1 FALSE, and sensor 2 FALSE, and sensor1 FALSE and sensor2 TRUE. What do you want to do in EACH of those cases? Then, after saving the two states of the sensors, put in logic to do what you want (whenever you say OR replace with || and whenever you say AND replace with &&)

``````if (cm < 100)
{
digitalWrite(relay1, HIGH);
timeOfSensor = millis();
}

else if (((timeOn * 1000) + timeOfSensor) < millis())  //  ,<-----
{
digitalWrite(relay1, LOW);
}
``````

It means you need to turn relay off only if cm > 100? Do you really need else?