When does -1 != -1 ?

I must be missing something entirely fundamental here, and posting this will probably make me feel stupid, but for the life of me I cannot determine why the sketch below turns the LED on.

I've tried several values other than -1 (e.g. 1, 0, -42, etc.) and none turn the LED on.

I've duplicated this behavior on two different PCs, two different microcontrollers, with Arduino 0022 and 1.0. Attempting to use Serial to debug makes the problem go away :stuck_out_tongue_closed_eyes:

#define TEST_VALUE -1
int foo = TEST_VALUE;

void setup(void)
{
    pinMode(13, OUTPUT);
    if (foo != TEST_VALUE) digitalWrite(13, HIGH);
}

void loop(void)
{
}

x != x is always going to return false because x == x is true

What are you doing to give foo a different value than TEST_VALUE? Looks to me that both are = to -1 when you hit void setup.

foo != TEST_VALUE is always false.

CrossRoads: What are you doing to give foo a different value than TEST_VALUE? Looks to me that both are = to -1 when you hit void setup.

I'm not doing anything to give foo a different value. That is the sketch, in its entirety.

foo != TEST_VALUE is always false.

One would certainly think so, but for TEST_VALUE = -1, it seems to be true...

JasonK: x != x is always going to return false because x == x is true

Agree with that statement, but that's not exactly what is in the sketch...

and none turn the LED on.

That sketch should not turn on the LED because the two things you are comparing are the same and the LED will only turn on if they are different.

If it's convenient, and if I could prevail on one of you for a few minutes, I wonder if someone would try the sketch. I'd really like to know whether it's a personal problem, or if I should call Mulder and Scully!

The sketch you posted does not turn on the LED, where as this one does:-

#define TEST_VALUE -1
int foo = TEST_VALUE;

void setup(void)
{
    pinMode(13, OUTPUT);
    if (foo == TEST_VALUE) digitalWrite(13, HIGH);
}

void loop(void)
{
}

On the Mega 1280 using arduino 1.0

[quote author=Jack Christensen link=topic=84243.msg631146#msg631146 date=1324571225] If it's convenient, and if I could prevail on one of you for a few minutes, I wonder if someone would try the sketch. I'd really like to know whether it's a personal problem, or if I should call Mulder and Scully! [/quote]

Well I loaded and ran it on my Seeeduino mega board and the pin 13 led remains off. If I hit reset, led blinks on once and then stays off. I'm using IDE 22.

Lefty

Much appreciated, guys. I'm definitely seeing different behavior, so will continue to look into it. Using Unos here, not sure what bearing that might have. There is one more PC I can try it on here.

Quote from: JasonK on Today at 03:59:43 PM x != x is always going to return false because x == x is true

Agree with that statement, but that's not exactly what is in the sketch...

In effect it is what is in your sketch.

Grumpy_Mike:

Quote from: JasonK on Today at 03:59:43 PM x != x is always going to return false because x == x is true

Agree with that statement, but that's not exactly what is in the sketch...

In effect it is what is in your sketch.

Because of compiler optimization? My assumption has been that the sketch compares a variable to a constant.

My assumption has been that the sketch compares a variable to a constant.

That is as maybe but the result is the same and correct.

So:-

if (foo != TEST_VALUE) digitalWrite(13, HIGH);
if (foo != -1) digitalWrite(13, HIGH);
if (foo != foo) digitalWrite(13, HIGH);

Are all the same thing and none of then results in the LED being turned on. Do you think it should?

Grumpy_Mike:

My assumption has been that the sketch compares a variable to a constant.

That is as maybe but the result is the same and correct.

So:-

if (foo != TEST_VALUE) digitalWrite(13, HIGH);
if (foo != -1) digitalWrite(13, HIGH);
if (foo != foo) digitalWrite(13, HIGH);

Are all the same thing and none of then results in the LED being turned on. Do you think it should?

No, no, believe me, I get all that completely. The thing is, when I run the sketch that I originally posted, it [u]does[/u] turn on the LED. So something is wrong but I haven't discovered what.

ok, I just ran that code n my Uno (IDE 1.0) and the LED turned on and stayed on.

let me see what I can find.

JasonK: ok, I just ran that code n my Uno (IDE 1.0) and the LED turned on and stayed on.

let me see what I can find.

Jason, many thanks! I really thought I was losing it there!

BTW, I just got confirmation from a local friend that he also reproduced the behavior, again on an Uno.

OK Jack you are not going mad. Just pulled out my Uno from a project and it does light the LED.

if (foo != foo) digitalWrite(13, HIGH);

Will not turn it on, but

if (foo != TEST_VALUE) digitalWrite(13, HIGH);
if (foo != -1) digitalWrite(13, HIGH);

Will, looks like there is an effect here. I did try a

digitalWrite(13,LOW);

before the if, in case the output register had a 1 set and it was being made visible by changing the mode but it made no difference. I will have another play.

Just tried:-

if (foo == -1) digitalWrite(13, HIGH);

and the LED does not turn on. Looks like there is some error with the comparison operation.

Time for the old diode from reset to +5vdc test I suspect, the code may not be actually running on the Uno but hung in a semi reset state? Don't have a Uno so can't play with that.

Lefty

Two things that come to my mind: 1) How is the LED wired? Is the LED on when the pin is high or when it is low? 2) Use avr-objdump on the elf file and have a look at the generated machine code

@lefty, I'm not familiar with the diode trick.

@udo, it's the "pin 13 LED" .. I'm using a standard, official Arduino Uno board. Have reproduced the behavior with a breadboarded ATmega328P.

Was playing with avr-objdump earlier, don't know AVR assembler though. I'll post the output.

I found that this works around the issue:

#define TEST_VALUE -1
long int foo = TEST_VALUE;

void setup(void)
{
    foo = -1;
    pinMode(13, OUTPUT);
    if (foo != TEST_VALUE) digitalWrite(13, HIGH);
}

void loop(void)
{
}