If Statement Problems

I am somewhat new to the forum so I hope that I am following the proper etiquette. If not, please forgive me. I am posting some code that is giving me problems. I am using an 'if' statement in the main loop and it seems to compile and upload to my Arduino Nano (Mega328) just fine. But when it executes it seems to hang up on the first if statement. In a previous version of the code the second if statement started working after I got rid of the brackets and put the conditional statement on the same line. Is there something wrong with the way I have coded this? Any help would be appreciated.

Thanks

Spin_temp_rpm3.ino (4.88 KB)

Printing to the LCD is a slow process. You should not be doing that in the ISR.

    sensorState = 1;
	 if(sensorState ==1)

I see no purpose in testing the value of sensorState immediately after you assigned it the value you are testing for.

Why don't you add a Serial.print() statement in the body of the if statements, to see where the hangup happens?

Not sure why I did the assign statement for the sensor. It seems to work fine in spite of that, probably because it doesn't get there unless the sensor has triggered an interrupt and therefore the assign statement is just redundant.

The problem I am having is in the if statements in the main loop:

void loop()
{
//Print the first line
lcd.setCursor(0, 0);
ambient = mlx.readAmbientTempF();
current_temp = mlx.readObjectTempF();
delta_temp = current_temp - test_temp;
if (abs(current_temp - test_temp) < 2)
{
cum_temp = cum_temp + delta_temp;
test_temp = current_temp; // filter out reading errors
}
if (cum_temp < 0) cum_temp = 0; // WRK sometimes accumulates negative numbers before wheel starts warming up
lcd.print("WRK ");lcd.print(cum_temp,1); lcd.print(" TMP ");lcd.print(current_temp,0);
delay(5000);

}

It just seems to hang up after the first 'if' statement.

It just seems to hang up after the first 'if' statement.

It can't. An assignment is a non-blocking operation, guaranteed to succeed (although not guaranteed to produce the expected results).

Add a Serial.print() statement in the body of the if statement, and after the if statement. What do you see printed?

I don't think that a serial.print() statement will do anything with my system. Code is uploaded to a Nano and the only display is the lcd. I tried an lcd.print statement and it is still hanging up. When I regress to a previous version of the code with a different if statement structure it still works so I believe that the hardware is working. Also, I note that when I comment out the sensor state =1 statement the ISR never gets triggered. I think I need to re-look at that code completely.

Also, I note that when I comment out the sensor state =1 statement the ISR never gets triggered.

Why do you think that? You initialize sensorState to -1. Then, you assign it a value of 1, and test that the value is 1. If you comment out the assignment statement, the value will never be 1, because you assign sensorState a value of 0 in the if statement body. So, with the assignment in the ISR commented out, the only values that sensorState can have are -1 and 0, neither of which is 1. And, it can only have a value of 0 if the value was 1. Which -1 isn't.

I think I need to re-look at that code completely.

Now, there is something I can agree with.

This is getting kind of humorous. I did this all over a year ago so I don't remember why I did it the way I did but I think I just cobbled it together from some examples I found rather than thinking it through. It does work however, because the ISR is never entered unless the sensor is triggered, then setting the state to 1 and testing for it works but it isn't very clean. Thanks for your comments - I will go back and understand it before I rewrite it.

So I cleaned up the ISR and then found the underlying problem. It probably won't mean anything or be helpful to anyone but just for closure I thought I should mention it. I was attempting to read the temp sensor as part of the initialization of the test_temp variable and this was before the begin mlx statement so the program hung up. It seems to be behaving now and it apparently had nothing to do with the if statements.

Thanks

Don't print to the LCD in the ISR. The ISR should (usually) at most update some variables, which the loop() inspects in a protected block. These variables need to be marked 'volatile'.

The pattern is:

volatile int isrCounter;

void ISR() {
  isrCounter ++;
}

void loop() {
  // protected block
  noInterrupts();
  int counter = isrCounter;
  isrCounter = 0;
  interrupts();

  // ok! Now the isr can resume doing it's thing. 

  if(counter != 0) {
    // hey! there has been an interrupt since I last looped!
    // maybe I need to recalculate and update the LCD
  }
}

AlphaThor:
So I cleaned up the ISR and then found the underlying problem. It probably won't mean anything or be helpful to anyone but just for closure I thought I should mention it. I was attempting to read the temp sensor as part of the initialization of the test_temp variable and this was before the begin mlx statement so the program hung up. It seems to be behaving now and it apparently had nothing to do with the if statements.

Thanks

Thank you for following up with the solution.