Correct use of if and boolean statement?

Hi all just a quick question.

I am driving a servomotor but on the condition of two things.

I want to drive my continuous rotation servomotor when a temperature reaches a certain setpoint but I only want to drive it until a condition changes on one of my digital inputs e.g. goes low. My plan was this:

if (temp > 25 && digin == HIGH) windowservo.write(90);

This should drive my servo until either the digital input goes low or the temperature is below 25 degrees?

Any help would be grealy appreciated.

Well, if you want to keep doing something, until some criterion is no longer true, then you would need a "while" statement, not an "if".

But since the whole loop( ) function of the arduino runs over and over, it is effectively within a while loop outside the loop( ) function, so your "if" statement here should be OK in that context.

No. This drives your servo as long as the temperature is higher than 25 degrees and the digin signal is HIGH.

You want it to measure change? In what way? For example, the change from higher than 25 degrees and a HIGH digin signal to lower than or equal to 25 degrees and a LOW digin signal, and vice versa? What are your boundaries? What counts as "change"? Does a small fluctuation of the temperature count as "change"? Does the "change" need to be persistent to some extent?

These are things you need to take into consideration.

sampazzer:
This should drive my servo until either the digital input goes low or the temperature is below 25 degrees?

ArtificialUser:
No. This drives your servo as long as the temperature is higher than 25 degrees and the digin signal is HIGH.

What you have said is the same thing I have said, just in a different way. I am driving my servo when the temperature goes above my set-point. It will drive untill it hits a limit switch which will set a digital pin from high to low which will break the boolean if statement and stop driving my servo.

I will do some testing with it. Just waiting on my limit switches to arrive!

I think that your condition is OK.

whether you use an explicit while or rely on the repetition of loop( ), you need to actually read the temperature and the status of the digital input pin on each iteration.

For example, this code example won't work, because it only reads the temperature once

int temperature = analogRead(  temp_pin );
while ( temperature > 25 )
{
   //  do something
}