"If" statement issue

Hello;

I have a program that measures resistance. Everything works except the “if” statement at the end of the following code:

long Resistance_Array = 0; // sum of resistance measurements taken
int Measurement_Count = 0; // current measurement number
float Resistance_Reading = 0; // calculated resistance value

float Resistance_Min = 100; // Minimum Resistance
float Resistance_Max = 150; // Maximum Resistance
int Pass_Fail = 0;

//**************************************************************************************************************************************************************

void setup() {
Serial.begin(9600); // set the rate for data transfer
}

//**************************************************************************************************************************************************************

void loop(){

// MEASURE THE RESISTANCE
Measurement_Count = 0; // reset the measurement count
Resistance_Array = 0; // reset the sum of resistance measurements taken
Resistance_Reading = 0; // reset the calculated resistance value

while (Measurement_Count < 200) { // take a set number of resistance readings and add them up
Resistance_Array += analogRead(A0);
Measurement_Count++;
delay(1);// Delay to allow Arduino Input to refresh
}
Resistance_Reading = (((Resistance_Array / 200)*.004882813)/.004); // Average the 200 resistance measurements together, multiply by 4.88,mv / bit, and divide by 4ma to get resistance

// CHECK IF READING IS IN SPEC

if (Resistance_Reading > Resistance_Min & Resistance_Reading < Resistance_Max) {Pass_Fail = 1;} // Pass if in spec
else {Pass_Fail = 0;} // Fail if not in spec

//**** THIS IS WHERE THINGS GO WRONG========
if (Pass_Fail = 1) {
Serial.println(“Pass”);
}
else if (Pass_Fail = 0) {
Serial.println(“Fail”);
}
delay(5000);
}

It seems like no mater what variable type I set Pass_Fail to, my serial output is always “Pass” or both “Pass” and “Fail”. It’s like the if statement runs just the first cause, or both. As an FYI, eventually the “If” statement will not serial print but rather do some other routines, but I tried to simplify it as much as possible to try to get it to work. I know I am most likely missing some minor detail, so any help would be greatly appreciated. Thank you.

long Resistance_Array = 0; // sum of resistance measurements taken

This variable is NOT an array. The phrase Array does not belong in it’s name.

//**** THIS IS WHERE THINGS GO WRONG========
   if (Pass_Fail = 1) {

Yep. = is not the same as ==. One is appropriate. One is not.

 if (Resistance_Reading >  Resistance_Min & Resistance_Reading <  Resistance_Max) {Pass_Fail = 1;} // Pass if in spec

Actually, it started to go wrong here. & and && are completely different operators. Pay attention to use the correct one.

PaulS;

Thank you. Can't believe I made a rookie mistake lol. I guess that's what happens when you program in Visual Basic some days and Arduino on others lol.

there is on more thing:
(Resistance_Array / 200) results in an integer. change to 200.0 (match your number of decimals elswhere)

if (Pass_Fail = 1)

you don't do a comparision here but rather an assignment! The 1 will be assigned to Pass_Fail an the if-statements evauates alway true :wink: