Variable changed without code

Hi

This simple code bellow displays difference in variables A and B on serial output (IDE 1.0.5, deumillanove).
Variables should be equal and are not changed within code, so I expect that serial output should always write OK.
This is not the case, it writes "not equal ".
Any ideas why ?

regards

Dino

/*
Encoder test
Created by Dino Mrvalj 2014
*/

#include <Wire.h>

int A = 0;
int B = 0;

void setup() {
A = 0;
B = 0;
Wire.begin();
Serial.begin(57600);
Serial.println (" Start ");
}

void loop() {
/***********************************************************************************

  • printout not as expected
    ***********************************************************************************/
    A = B;
    B = A;

if (A=!B) {
Serial.print (“A: “); Serial.print (A); Serial.print (” - B: “); Serial.print (B); Serial.println (” Not equal !?”);
}
else {
Serial.print ("A: “); Serial.print (A); Serial.print (” - B: “); Serial.print (B); Serial.println (” OK ");
}

}

Hi, try

!= instead of =!

Tom… :slight_smile:

No comparison!

You are assigning 'not B' to A

A=!B

B == 0 == FALSE
!B == TRUE

A is assigned TRUE and the if always succeeds.

Try

A == !B

Or like tom said, the more intuitive version:

A != B