Equal (?) Values fail in if statement

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?

I thought static made it so a variable is fixed?

static int k = 0;

Anyway, I am parsing your code for others:

//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); 
    }
  }
}
  if (i = Lever_Position[j])

Oops.

AWOL:

  if (i = Lever_Position[j])

Oops.

Yeah, as per. AWOL got it in one :P.

Comparison is ==, not =.

Johnny010:
I thought static made it so a variable is fixed?

static int k = 0;

You're thinking of const.

A static variable is a locally scoped value that retains it's value between calls to the function.

ie:

void test() {
static int i=0;
Serial.println(i++);
}

will print 0 the first time it's called, 1 the second time, and so on, but i is only defined inside of the test() function.

DrAzzy:
Comparison is ==, not =.

You're thinking of const.

A static variable is a locally scoped value that retains it's value between calls to the function.

ie:

void test() {
static int i=0;
Serial.println(i++);
}

will print 0 the first time it's called, 1 the second time, and so on, but i is only defined inside of the test() function.

Ah got ya. Thanks.

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 definition / initialization of Level_Position (array) is incorrect - the size of the array cannot be determined by using ellipsis.

Following sample code returned multiple errors to indicate that.

I am not sure what you met "These big arrays run from 0 to 59", perhaps you will use multidimensional array of variable size?

Irregardless, if you need multidimensional array with variable size you be better off using pointers- they are more "memory friendly".

void setup() {
  // put your setup code here, to run once:
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, ....};
}

void loop() {
  // put your main code here, to run repeatedly:

}

Compiler errors

sketch_dec29c.ino: In function 'void setup()':
sketch_dec29c.ino:3:98: error: expected primary-expression before '...' token
sketch_dec29c.ino:3:101: error: expected '}' before '.' token
sketch_dec29c.ino:3:101: error: expected ',' or ';' before '.' token
sketch_dec29c.ino: At global scope:
sketch_dec29c.ino:4:1: error: expected declaration before '}' token
expected primary-expression before '...' token

Actually you did not use ellipsis (...) , but if used , the error output is reduced to

sketch_dec29c.ino: In function 'void setup()':
sketch_dec29c.ino:3:98: error: expected primary-expression before '...' token
expected primary-expression before '...' token

Delta_G:
What are the .... at the end of the array supposed to mean?

Something like "Well, you get the idea, I'm omitting the rest as it doesn't really matter."

@Vaclav: If you're going to quote stuff, do you think you could start using quote tags, please?

I know you don't like being reminded that you've been here a long time, but you have.

  if (k>=60) {
    Serial.println ("All matches found.  End program/reset.");
    pressToContinue;
  }

What is the pressToContinue; statement supposed to be doing? It is not calling a function.