arduarn:
AWOL's hints seem to be too subtle, so more directly:
Here, you give your pins nice names. (I recommend marking them const)Here, you ignore the nice names and just hard code the pin numbers?
Additionally, these new variables eclipse the ones you previously defined?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?
Thanks to AWOL (whose hints were too subtle for me, sorry AWOL), arduarn, sterretje, Delta_G:
const int Relay = 3;
const int LDRa = 0;
const int LDRb = 1;
const int LDR1 = 2;
boolean A = LOW;
void setup() {
pinMode(Relay,OUTPUT); //setting the pin mode to Output
pinMode(LDRa,INPUT);
pinMode(LDRb, INPUT);
pinMode(LDR1, INPUT);
digitalWrite(Relay, LOW);
}
void loop() {
int stateLDRa = digitalRead(LDRa);
int stateLDRb = digitalRead(LDRb);
int stateLDR1 = digitalRead(LDR1);
int stateRelay = digitalRead(Relay);
if (stateLDRa == HIGH && stateLDR1 == LOW && stateLDRb == LOW) //condition 1; clockwise
{digitalWrite(Relay, HIGH);
A = HIGH;
}
if (stateLDRa == LOW && stateLDR1 == HIGH && stateLDRb == LOW && A == HIGH) //condition 2; clockwise
{digitalWrite(Relay, LOW);
}
if (stateLDRa == LOW && stateLDR1 == LOW && stateLDRb == HIGH) //condition 3; clockwise and counterclockwise: set relay low
{digitalWrite(Relay, LOW);
A = LOW;
}
if (stateLDRa == LOW && stateLDR1 == HIGH && stateLDRb == LOW && A == LOW) //condition 4; counterclockwise: set relay high
{digitalWrite(Relay, HIGH);
}
}
Now, maybe some suggestion to clean this up? This solution now works, since just now, but I am sure the code can be optimised.
A hint sure will do now (I hope).
Delta_G:
2 hints:
const int Relay = 3;
Why waste a byte here? 3 will fit just fine in one byte, you don't need two. Unless you one day expect to have to use pin 256 on your Arduino.
- Hit Autoformat on your code (Control - T). It will make it a lot more readable to get the braces and lines lined up right.
Thanks!!
Here is cleaned up code (I also remove the declarations of the 3 pins 0, 1 and 2 as being inputs in setup).
const byte Relay = 3;
const byte LDRa = 0;
const byte LDRb = 1;
const byte LDR1 = 2;
boolean A = LOW;
void setup() {
pinMode(Relay, OUTPUT); //setting the pin mode to Output
digitalWrite(Relay, LOW);
}
void loop() {
int stateLDRa = digitalRead(LDRa);
int stateLDRb = digitalRead(LDRb);
int stateLDR1 = digitalRead(LDR1);
int stateRelay = digitalRead(Relay);
if (stateLDRa == HIGH && stateLDR1 == LOW && stateLDRb == LOW) //condition 1; clockwise
{ digitalWrite(Relay, HIGH);
A = HIGH;
}
if (stateLDRa == LOW && stateLDR1 == HIGH && stateLDRb == LOW && A == HIGH) //condition 2; clockwise
{ digitalWrite(Relay, LOW);
}
if (stateLDRa == LOW && stateLDR1 == LOW && stateLDRb == HIGH) //condition 3; clockwise and counterclockwise: set relay low
{ digitalWrite(Relay, LOW);
A = LOW;
}
if (stateLDRa == LOW && stateLDR1 == HIGH && stateLDRb == LOW && A == LOW) //condition 4; counterclockwise: set relay high
{ digitalWrite(Relay, HIGH);
}
}
Is this an assignment you are working on?
I get that feeling, because you did not really understand the problem description.
brice3010:
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
There is a difference between "is high" and "goes high" (I assume "need to be" is equal to "is").
It involves keeping the previous state to be able to detect changes (state change detection).
Whandall:
Is this an assignment you are working on?
I get that feeling, because you did not really understand the problem description.
There is a difference between "is high" and "goes high" (I assume "need to be" is equal to "is").
It involves keeping the previous state to be able to detect changes (state change detection).
Hi, thks for your reply. This is my little project, blame the improper descriptions on my rather unlogical english. I will try and rewrite the conditions in a truth table:
LDRa LDRb LDR1 Relay
0 0 0 not relevant
1 0 0 1
0 1 0 0
1 1 0 This situation does not occur, Relay position not relevant
0 0 1 0 (if previously LDRa was 1) or 1 (if previously LDRb was 1)
1 0 1 1
0 1 1 0
1 1 1 This situation does not occur, Relay position not relevant