Boolean operators mixed

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.

Thank you.

Post a sketch where your example has happened.

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 setup(){
Serial.begin(9600);
pinMode(4,OUTPUT);
pinMode(6,INPUT);
}

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);
}

else
{
digitalWrite(4,0);
delay(5000);
}
Serial.println(light);
Serial.println(eye);
delay(100);
}

rutar7:

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);
}

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.

...R

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.

Hi rutar7

Could you add some more print statements like this and post the serial monitor output.

void setup(){
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  pinMode(6,INPUT);
  Serial.println("Light : Eye : Pin 4");
}

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);
    Serial.println("true);
  }
  else
  {
    digitalWrite(4,0);
    Serial.println("false");
    delay(5000);
  }
  Serial.print(light);
  Serial.print("   ");
  Serial.println(eye);
  Serial.print("   ");
  Serial.println(digitalRead(4));
  delay(100);
}

What do you have connected to pin 4?

Regards

Ray

Test:

void setup () 
{
 Serial.begin (115200);
}

void loop () 
{
Serial.println (1 && 1);
Serial.println (0 && 1);
Serial.println (1 && 0);
Serial.println (0 && 0);

Serial.println (1 || 1);
Serial.println (0 || 1);
Serial.println (1 || 0);
Serial.println (0 || 0);
  
int x = 5;

Serial.println (x < 20);
Serial.println (x > 20);

while (true) {}
 
}

Output:

1
0
0
0
1
1
1
0
1
0

The output is as expected. You need to display debugging in your loop to show the values that you are testing against. They may surprise you.

else

{
digitalWrite(4,0);
delay(5000);
}
Serial.println(light);
Serial.println(eye);
delay(100);

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.

Your figures are still 5 seconds out of date. Post your new sketch please.

Tens of thousands of programs would fail if > was really < and if AND was really OR. So the problem is your code.

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.

So you decided to disregard my sketch above which proves that it does in fact recognize the correct operators?

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 :slight_smile:

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.

truth table.jpg

So this sketch should work just fine?

void setup(){
Serial.begin(9600);
pinMode(4,OUTPUT);
pinMode(6,INPUT);
}

void loop()
{
int sensor=analogRead(5);
int light=map(sensor,0,1023,0,100);
int eye=digitalRead(6);
Serial.println(light);
Serial.println(eye);

if (eye == HIGH && light < 15) {
digitalWrite(4,1);
delay(5000);
}

else
{
digitalWrite(4,0);
delay(100);
}

}

I don't have access to an Arduino at the moment to test it, but is it a case of operator precedence?

if (eye == HIGH && light < 15) {

should be

if ((eye == HIGH) && (light < 15)) {

when light value falls below 15 and digital movement sensor (''eye'') detects movement (sensor HIGH) output number 4 should go HIGH and delay 5000ms.

From your latest program:

if (eye == HIGH && light < 15) {
   digitalWrite(4,1);
   delay(5000);
}
else
{
  digitalWrite(4,0);
  delay(100);
}

I'm suggesting you try inverting the digitalWrite values, like this, and see how that affects the relay.

if (eye == HIGH && light < 15) {
   digitalWrite(4,0);
   delay(5000);
}
else
{
  digitalWrite(4,1);
  delay(100);
}

rutar7:
So this sketch should work just fine?

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.

...R

@dannable: Thank you very much...those brackets fixed the problem...

Could you please post the final working code?

Thanks