I've had my share of syntax and logical errors, but things have been going well. However, this behavior has me stumped.
The following array contains values (the binomial representation of the positions of 8 levers) for entry into other arrays for which I want to validate the data. Not all combinations are valid and levers will be physically locked to prevent undesired combinations so many values are missing.
//These big arrays run from 0 to 59
unsigned int Lever_Position[] = {0,1,2,3,128,130,4,5,6,7,132,134,8,10,136,12,14,140,16,17,18,19, ....};
The following is test code to run through all possible lever positions to verify the data. It tests the value of i against the array above and should return the index of any match it finds.
unsigned int readLevers () { //Returns lever positions, Address 4 byte 0
//Coal Creek interlocking only has 12 inputs so they are hard wired to the MEGA board.
//No addressing is necessary
static unsigned int i = 0; //Test value of Lever_Positions - this would be read from the input pins
static int k = 0; //Keeps track of total found so I don't read outside the array
int j; //This is the array index
//debug routine to check data
Serial.println ("Begin readLevers");
if (k>=60) {
Serial.println ("All matches found. End program/reset.");
pressToContinue;
}
for (j=0; j<60; j++) { //Search all lever position data for a match
Serial.print ("Test Value i = ");
Serial.println (i);
Serial.print ("Lever_Position[");
Serial.print (j);
Serial.print ("] = ");
Serial.println (Lever_Position[j]);
if (i = Lever_Position[j]) { //Found a match - return value and exit
Serial.print ("readLevers - Found match at Lever_Position ");
Serial.println (j);
i++; //Incriment i for next test
k++; //Incriment when match found
return (j); //Index of match value
}
else { //Not a match
Serial.print ("Lever position ");
Serial.print (i);
Serial.print (" binary ");
Serial.print (i,BIN);
Serial.println (" INVALID.");
i++; //Incriment i for next test
delay(1000);
}
}
}
but here's what I get on the serial monitor:
Enter any character to continue.
Begin SetUp
Call readLevers from setup
Begin readLevers
Test Value i = 0 i is 0
Lever_Position[0] = 0 Lever_Position is 0
Lever position 0 binary 0 INVALID. equivalency test failed
Test Value i = 1
Lever_Position[1] = 1
readLevers - Found match at Lever_Position 1
In setup received from readLevers: 1
Lever position 1 binary 1 invalid.
Enter any character to continue.
On the first run through the loop (j = 0), the test value i = 0 and Lever_Position[j] = 0, but the if statement doesn't see the match and declares the value as invalid. On the second pass (j = 1) it does find a match in 1 = 1 and returns the correct value - but setup is expecting 0 so it has a fit.
Any suggestion as to why the if statement doesn't see a match between two unsigned int 0 values?