Arduino Temperature test code

I am creating an automatic window Opener that uses 2 DS1820 digital temperature sensors. I have them hooked up to the Arduino so that when the difference in temperatures is at a certain level, it will open or close a window depending on the state of the window. My problem is that when the indoor temperature is below 70 and the outdoor is above 70, the motor opens and then closes immediately, continuously. It's an endless cycle of open and close. If the indoor temperature is above 70 and the outdoor is below, then it opens the window properly like it's supposed to. Here is my temperature test function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  void testTemp(){
 // while(selection == true){
  float in = getTempin();
  float out = getTempout();
  if ((in >= 70) && (out < 70) && (OPEN == false) && (CLOSE == true))
  {
    digitalWrite(motorPinO, HIGH);
    delay(5000);
    digitalWrite(motorPinO, LOW);
    OPEN = true;
    CLOSE = false;
  }
  else if((in <= 70) && (out > 70) && (OPEN == true) && (CLOSE == false))
  {
    digitalWrite(motorPinC, HIGH);
    delay(5000); 
    digitalWrite(motorPinC, LOW);
    OPEN = false;
    CLOSE = true;
  }

The issue is that when the else if statement is true, it keeps opening and closing a window over and over again. MotorPinC stands for the motor closing the window. MotorPinO stands for Opening the window.
The Open and Close are to signify if the window is already opened or closed.
I think one thing that might help is have a wider threshold, not unlike a furnace's thermostat. Therefore, don't attempt to actuate window until temps are < 68 and > 72 indoors. It seems as though the problem exists when temp is exactly 70 wherein in >= 70 and out <= 70 both qualify.
Topic archived. No new replies allowed.