if (0 != 0) is TRUE !?!? WHY!!!!

I'm frustrated and stumped please help! Here is shortened version of program;

int sensorValue[24];
int sensorNoData[24];

int pulse_value;
int pulse_value2;

void setup()
{
sensorNoData[0] = 0;
sensorNoData[1] = 0;
sensorNoData[2] = 0;
sensorNoData[3] = 0;
sensorNoData[4] = 0;
sensorNoData[5] = 0;
sensorNoData[6] = 1;
sensorNoData[7] = 0;
sensorNoData[8] = 1;
sensorNoData[9] = 0;
sensorNoData[10] = 1;
sensorNoData[11] = 0;
sensorNoData[12] = 1;
sensorNoData[13] = 1;
sensorNoData[14] = 0;
sensorNoData[15] = 0;
sensorNoData[16] = 0;
sensorNoData[17] = 0;
sensorNoData[18] = 0;
sensorNoData[19] = 0;
}

for (int iValue = 0; i <= iPulses; i++) {
pulse_length = pulseIn(inputPin, LOW);
if (pulse_length > 80 && pulse_length < 1800)
pulse_value = 0;
else
pulse_value = 1;

sensorValue[iValue] = pulse_value;
}

for (int iValue = 0; iValue <= iPulses; iValue++) {
if (sensorValue[iValue] != sensorNoData[iValue]);
{
Serial.print(iValue); Serial.print("'"); Serial.print(sensorValue[iValue] ); Serial.print(sensorNoData[iValue]); Serial.print("'");
printData();
iValue = 20;
}

So for the line:
if (sensorValue[iValue] != sensorNoData[iValue]);

returns TRUE even know that sensorValue[iValue] = 0, and sensorNoData[iValue])=0.. I was under the impression if 0 !=0, should be false. Can someone help me figure out WHY please?


sensorValue and sensorNoData are arrays. You need to compare their values element by element.
(sensorValue != sensorNoData) is true because you are comparing the memory addresses of the first element of each array, and those arrays do not occupy the same space in memory.

Sorry, mystype = there were BRACKETS around i caused a screen translation issue on the forum.

I modified the original posting so it matches what I'm working with, please re-look at it again.

You seem to have an undefined value iPulses.

DrStein99:
Sorry, mystype = there were BRACKETS around i caused a screen translation issue on the forum.

I modified the original posting so it matches what I'm working with, please re-look at it again.

You know, the problem with typing code in, instead of doing an Auto-format, and cut/paste of the ENTIRE code from the IDE, is that so much can go wrong, and that it becomes more difficult for those who want to help, to compile and test the code.

So, what all is wrong here.

  1. Your first for loop uses iValue as a counter, but uses i in the other two parts of the conditional.

  2. Your if statement works perfectly. It checks to see if (sensorValue[iValue] != sensorNoData[iValue]), finds that it's false, and executes the next statement in the if block.

  3. (really part of 2). you execute the Serial.print statements. Unconditionally. Regardless of the result of the if conditional.

Can you see what's happening?

Check the if statement. Anything wrong with it? If your answer is NO, check it again. Repeat until the answer is YES.

iValue = 20;

This line also seems quite stupid.

You are going through your loop, and you get to , say iValue=22, and the condition is met, and you set iValue to 20, and then 21, and then 22, and the condition is met, and you set iValue to 20, and then 21, and then 22, ..... and your for loop never finishes.

There is also a very stupid semicolon which should not be there.

         if (sensorValue[iValue] != sensorNoData[iValue]);

How to use this forum

Please use code tags.

Read this before posting a programming question

michinyon;

I found the semi colon was the actual nature of the problem after many long hours at reviewing lengthy code then becomming frustrated, I oversaw the semi-colon at the end of the IF statement.

Thanks for the replies.