Pages: [1]   Go Down
Author Topic: Temperature Sensing Function  (Read 420 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
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 Open and Close are to signify if the window is already opened or closed.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 48
Posts: 3417
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I can't see the bug. I would suggest though that you only need the OPEN boolean. No need to keep two variables to tell you if the window is open or closed. Also, you might want to vary the temperatures a bit - maybe open if in is greater than 71 and close if it's less than 69 - q.v. hysteresis.
Logged

Offline Offline
Edison Member
*
Karma: 57
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I agree with windbill.

You need hysteresis, for example 1 or 2 degrees.
http://en.wikipedia.org/wiki/Hysteresis

The OPEN and CLOSE are very confusing.
You try to fix everything in two "if" statements. I think the code will read better if you expand that.

Code:
// Code not tested

// OPEN = true, CLOSE = false;
// Initial state unknown ?
boolean windowOpen = false;    

void testTemp(){
 // while(selection == true){
  float in = getTempin();
  float out = getTempout();

  if( windowOpen)
  {
    // The window is open at the moment.
    // Test if it needs to be closed.
    // A difference of 1 should be between the temperatures.
    if(in < 69.5 && out > 70.5)
    {
      // Close the window.
      digitalWrite(motorPinC, HIGH);
      delay(5000);
      digitalWrite(motorPinC, LOW);
      
      // Remember the window state
      windowOpen = false;
    }
  }
  else
  {
    // The window is closed at the moment.
    // Test if it needs to be opened.
    // A difference of 1 should be between the temperatures.
    if (in > 70.5 && out < 69.5)
    {
      // Open the Window
      digitalWrite(motorPinO, HIGH);
      delay(5000);
      digitalWrite(motorPinO, LOW);

      // Remember the window state
      windowOpen = true;
    }    
  }
}

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Alright, thank you guys. I did change the range of temperatures. The issue wasn't the for loop, but was a problem with html returning a value after every refresh and that made one of the if statements true every time. I fixed it though. Thanks for the help!
Logged

Pages: [1]   Go Up
Jump to: