IF statement gone awry

if ((avg >= -40) && (avg <= 20) && (change == 1)) {
digitalWrite(PAD, HIGH);
timer0 = -45;
change = 2; }

Hey guys, my question is regarding the last part of that if statement, (change == 1).

It seems to me that this part of the code should only be able to execute once and not again until INT change is back to 1. I am serial.print-ing CHANGE, and even when it equals 2, the code still executes and PAD gets written high. What am I doing wrong?

Thank you!

zakelectric: What am I doing wrong?

You have not posted your complete program so we have no means to see the scope of the variables you are using.

...R

It seems to me that this part of the code should only be able to execute once and not again until INT change is back to 1.

Why would it execute once? It would execute whenever change is 1.

I am serial.print-ing CHANGE

Not in the code you posted you aren't.

CHANGE and change are two different variables.

How do you know it is being executed more than once?

You are setting PAD to HIGH once inside that if() statement, if you are not setting it to LOW elsewhere it will remain HIGH.

From a logic point of view, you are correct, it should only get in there when change is 1 and avg falls between that range, so either you have an error elsewhere in your code, or your assumption that its being called multiple times is incorrect.

Hi,

Can you please post a complete copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom… :slight_smile:

tammytam: How do you know it is being executed more than once?

You are setting PAD to HIGH once inside that if() statement, if you are not setting it to LOW elsewhere it will remain HIGH.

From a logic point of view, you are correct, it should only get in there when change is 1 and avg falls between that range, so either you have an error elsewhere in your code, or your assumption that its being called multiple times is incorrect.

Tammytam, you were right, I was assuming, which makes an ass out of u n me! mostly me. It is only being called once, but nothing is setting PAD to LOW. Thanks!

And sorry everyone else, will use the code tags and post my whole code next time. Thanks all!