Hi!
I've been programing arduino UNO board for few months now and till today I had no problems or if I did, I was able to solve them. But today occured something really wierd... As I started using boolean operators, I noticed they are all mixed. What I mean is that if I type "AND" operator or "&&" sign in if sentence, my arduino recognizes it as "OR" (||) operator and if I type "||" sign it somehow recognizes it as "AND" (&&) operator. I noticed that also more or less signes are mixed, so if I type x**<20, my arduino understands x>**20. If I type if(digitalRead(6)==HIGH), arduino understands if(digitalRead(6)==LOW). Other functions work fine, I only have problems using boolean operators in ''if'' sentence.
So I guess you understand what I mean. Hope you can help me.
Here is my sketch. So what this sketch is supposed to do is when light value falls below 15 and digital movement sensor (''eye'') detects movement (sensor HIGH) output number 4 should go HIGH and delay 5000ms. The sketch I attached seems to be wrong (as the boolean operators are mixed) but on my arduino it works as it is supposed to. I also noticed, that if I put delay(5000) in the place where if sentence is true (this is how it shuld be) it doesn't work correctly. When I tried to put it in the false part of if sentence, arduino stops the program for 5s after digitalWrite(4,1).
void loop()
{
int sensor=analogRead(5);
int light=map(sensor,0,1023,0,100);
int eye=digitalRead(6);
if (eye == LOW || light > 15) {
digitalWrite(4,1);
}
{
int sensor=analogRead(5);
int light=map(sensor,0,1023,0,100);
int eye=digitalRead(6);
if (eye == LOW || light > 15) {
digitalWrite(4,1);
}
else
{
digitalWrite(4,0);
delay(5000);
}
This is a poor example for exploring how && and || work because the values going into the test are unpredictable.
Try it like this
void loop()
{
int light=20;
int eye= HIGH;
if (eye == LOW || light > 15) {
digitalWrite(4,1);
}
else
{
digitalWrite(4,0);
delay(5000);
}
And then change around the values for light and eye and see what happens.
I have a suspicion you are mixed up in your mind about exactly what AND and OR do. OR will return TRUE if either part is true and AND will return TRUE only if both parts are true.
I'm watching those ''light'' and ''eye'' values on serial monitor so I know when digital output should be high or low... I'm also pretty sure I know what those logical operators mean, as I'm not new in programing. The point is, that if I insert sign && (true only if both parameters are true) arduino will understand it as || (true as soon as one parameter is true). As I stated above, also < and > signs are mixed... So if I write x<20 arduino will understand it as x>20. If I write x==HIGH arduino will understand x==LOW.
When I found out that those operators are mixed, I wrote the whole sketch (the attached one) in the opposite operators (HIGH -> LOW; < -> >, && -> ||) and it works just fine. I've already used a lot of IF sentences in past and never had this problem. So I really think the problem is not in my understanding.
The way you are doing it you are showing the values potentially 5 seconds after changing the LED relay. So your display of light and eye is 5 seconds out of date.
On pin 4 there's a relay module connected (with transistor).
Here's a picture of serial monitor with different variatons of ''eye'' and ''light'' values.
@Nick Gammon: Please read carefully my previous post as I explained why there's a delay in false part of if sentence. You really should focus only on why those operators are mixed. I know exactly how sketch should look like, the one I posted is already corrected so it works fine on my arduino.
I know that most sketches are based on if sentences...so were my older sketches. But they worked just fine. I really really understand the code, I'm aware of the meaning of those operators, and I'm sure I can write correct sketch as this is not my first sketch I'm doing. So maybe we should really focus on why arduino is not recognizing correct operators anymore.
Looking at the output screen shot, the true / false outcomes and the value written to pin 4 are correct with respect to the code. Which is expected
Going back to your original problem statement, it sounds like the symptoms are consistent with the relay being operated in the reverse to what you think it is.
Your requirement was relay on (let's say) when (light value < 15) AND (eye == HIGH).
You have had to change the code so that relay "goes on" when (light value is not < 15) OR (eye is not HIGH).
Try reversing the values you use to turn the relay off and on.
EDIT Added truth table in attached file to clarify what I am suggesting.
Why do you insist on trying to learn something with a sketch that includes code that is irrelevant to the learning. I proposed a very simple piece of test code earlier in Reply #3 which you seem to have ignored.