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:
When LDRa goes high and LDRb and LDR1 need to be low, then Relay goes high
When LDR1 is high and LDRa and LDRb are low and if Relay is high, then Relay goes low
When LDRa is low, and LDR1 is low and LDRb is high, then Relay needs to be sent low (again)
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() {
int LDRa = digitalRead(0);
int LDRb = digitalRead(1);
int LDR1 = digitalRead(2);
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?
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.
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?
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.
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.
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?
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() {
int LDRa = digitalRead(0);
int LDRb = digitalRead(1);
int LDR1 = digitalRead(2);
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);
}
}
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?