# conditionals not performed as required

Three digital inputs need to be set in a specific sequence to set a digital output:

LDRa, LDRb, LDR1 are low in rest; Relay is low in rest. Following conditions can occur:

1. When LDRa goes high and LDRb and LDR1 need to be low, then Relay goes high

2. When LDR1 is high and LDRa and LDRb are low and if Relay is high, then Relay goes low

3. When LDRa is low, and LDR1 is low and LDRb is high, then Relay needs to be sent low (again)

4. When LDRa is low, LDRb is low and LDR1 goes high and if Relay is low, then Relay goes high

``````int Relay = 3;
int LDRa = 0;
int LDRb = 1;
int LDR1 = 2;

void setup() {
pinMode(Relay,OUTPUT); //setting the pin mode to Output
digitalWrite(Relay, LOW);
}

void loop() {
if (LDRa == true && LDR1 == false && LDRb == false)
{digitalWrite(Relay, HIGH);
}
if (LDRa == false && LDR1 == true && LDRb == false && Relay == HIGH)
{digitalWrite(Relay, LOW);
}
if (LDRa == false && LDR1 == false && LDRb == true)
{digitalWrite(Relay, LOW);
}
if (LDRa == false && LDR1 == true && LDRb == false && Relay == LOW)
{digitalWrite(Relay, HIGH);
}
}
``````

What happens is that sequence 1 and 2 (and 3) are being executed, but not sequence 4

The purpose of this little routine is for a reversing loop for model trains. When the loco goes clockwise it hits LDRa first and a relay is sent high to get the reversing loop in the same phase as the track under LDRa.

When the loco continues over the loop the relay is sent low to reverse the track phases back to the one for the outgoing track under LDRb. These are sequence 1 to 3.

When the loco enters the reversing loop counterclockwise it hits LDRb first, the Relay is set low and the loop tracks are in phase with the track under LDRb. When the loco continues over LDR1 and has keft LDRb the relay must be set high so that the loop track phase is identical with the track under LDRa. This is sequence 4.

What is wrong in this routine that sequence 4 is not executed?

What chance that Relay (value 3) ever equals LOW (value zero) ?
Or HIGH (value one), for that matter.

At the moment Relay is always LOW except when LDRa goes high. As soon as LDRa is low again and LDR1 is high Relay goes back low.

No, at the moment, Relay is always 3, because that's what you set it to,

Relay is supposed to be allocated to pin 3 (ATtiny85), I guess I defined it wrong?

brice3010:
Relay is supposed to be allocated to pin 3 (ATtiny85), I guess I defined it wrong?

No, you defined it correctly, as far as I can see.

I have a problem: as soon as sequence 4 is executed it gets contradicted in the next loop by sequence 2!

When the Relay is LOW, and LDR1 goes HIGH, the sequence 4 makes the Relay go HIGH. However, when this happens then sequence 2 immediately sends Relay back LOW.

How can I solve this?

You could try separating your relay pin number from the logical condition on that pin.

You may also want to try clicking on the link in your personal text - you may be surprised.

AWOL:
You could try separating your relay pin number from the logical condition on that pin.

You may also want to try clicking on the link in your personal text - you may be surprised.

I see: introduce a separate variabel, for example a boolean A, which could be 1 for a loco that goes clockwise, and be 0 for a loco that goes counterclockwise?

Or, for example, a boolean "relayState", true for relay ON and false for relay OFF.

AWOL:
Or, for example, a boolean "relayState", true for relay ON and false for relay OFF.

I am lost; I tried several options but I feel like hitting in the dark. Can you give another hint maybe? Thanks a lot!

Maybe nesting the conditions? One condition for clockwise running with relay actions nested, and one condition for counterclockwise running with relay actions also nested?

I'm sorry, I've already pointed out that a pin number does not tell you anything about the state of that pin (but it does give you the means to find out . . . )

You've given the variable (should be a constant, IMO) "Relay" the value 3.
That is correct, as far as I can tell from what you have told me.

Now, I know nothing about locomotives, or clockwise or anticlockwise, but I do know about relays, and they're in one state or another, but that state has nothing at all to do with the number of of the pin they're connected to.

(still not fixed the link, I see)

AWOL:
I'm sorry, I've already pointed out that a pin number does not tell you anything about the state of that pin (but it does give you the means to find out . . . )

You've given the variable (should be a constant, IMO) "Relay" the value 3.
That is correct, as far as I can tell from what you have told me.

ATtiny pin PB3 is defined as Relay (or so I hope it is correct with my code?)

AWOL:
Now, I know nothing about locomotives, or clockwise or anticlockwise, but I do know about relays, and they're in one state or another, but that state has nothing at all to do with the number of of the pin they're connected to.

(still not fixed the link, I see)

What link do you mean? Fixed what?

The link, in your personal text - the one that says “www.mysafety.be”, but which doesn’t work - it’s not very good advertising when you’re trying to project a professional image.

AWOL:
The link, in your personal text - the one that says "www.mysafety.be", but which doesn't work - it's not very good advertising when you're trying to project a professional image.

ah ok; I just changed it, and noticed the personal text had to be reduced, probably a change in forum rules.
Edit: website link still does not work, I have to look into it, but first the issue of my thread question: how do I solve the condtional sequences?

brice3010:
ah ok; I just changed it

Still doesn’t work

With this code I still have the clockwise conditions (1 and 2) right, but counterclockwise (condition 3) goes wrong: as soon as LDRb goes high, Relay goes HIGH too, and it should stay LOW???

``````int Relay = 3;
int LDRa = 0;
int LDRb = 1;
int LDR1 = 2;
boolean A;

void setup() {
pinMode(Relay,OUTPUT); //setting the pin mode to Output
digitalWrite(Relay, LOW);
}

void loop() {
if (LDRa == HIGH && LDR1 == LOW && LDRb == LOW)  //condition 1; clockwise
{digitalWrite(Relay, HIGH);
A = HIGH;
}
if (LDRa == LOW && LDR1 == HIGH && LDRb == LOW && A == HIGH) //condition 2; clockwise
{digitalWrite(Relay, LOW);
}
if (LDRa == LOW && LDR1 == LOW && LDRb == HIGH) //condition 3; clockwise and counterclockwise: set relay low
{digitalWrite(Relay, LOW);
A = LOW;
}
if (LDRa == LOW && LDR1 == HIGH && LDRb == LOW && A == LOW)  //condition 4; counterclockwise: set relay high
{digitalWrite(Relay, HIGH);

}

}
``````

AWOL's hints seem to be too subtle, so more directly:

brice3010:

``````int Relay = 3;
``````

int LDRa = 0;
int LDRb = 1;
int LDR1 = 2;

Here, you give your pins nice names. (I recommend marking them const)

brice3010:

``````void loop() {
``````

Here, you ignore the nice names and just hard code the pin numbers?
Additionally, these new variables eclipse the ones you previously defined?

brice3010:

``````    if (LDRa == false && LDR1 == true && LDRb == false && Relay == HIGH)
``````

{digitalWrite(Relay, LOW);
}

LDRa, LDRb,LDR1 are either LOW/HIGH so why compare with true/false?
And most importantly, Relay always has the value 3, so why are you comparing it with LOW/HIGH?

To cut it short, you fail to detect the state of the relay in your code. digitalRead can do the trick (even for an output pin).