Ventilation Robot Servo

Hi, All.
So, I've built a miniature green house. Whenever the sun hits it get too warm so I need to ventilate it.
I've got an Arduino One, a Temperature sensor and a Servo (RDS 3115 MG).

As of now that I'm just trying this out as my first ever Arduino project I've got constant power to the board, but that also need to change to a 9V battery... My main concern now is the Servo - I thought it was broken because it behaved erratic so I bought another one... it behaves exactly the same so It's all my fault (of course).

Ideally the servo would react (and open a vent-hatch) whenever the temperature his a certain temperature like 27C... Depending on outside temperature It'll take a few seconds up to hours to decrease the temperature but eventually I want it to return/close whenever the temperature is below 27C.

Here is my code:


#include <OneWire.h>
#include <DallasTemperature.h>
#include <Servo.h>

#define ONE_WIRE_BUS 2 // Data wire is plugged into pin 2 on the Arduino

OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)

DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.

int tempDegree = 0; //declare variable that can contain the temperature 
int servoBracket = 0;
int ventHatch_open = 100; // declare variable that can contain the number of degrees of servo when OPEN
int ventHatch_closed = 50; // declare variable that can contain the number of degrees of servo when CLOSED
Servo myservo; // create a new servoobejct called myservo

void setup(void)
{
  Serial.begin(9600); // start serial port
  // Start up the library
  sensors.begin();
  myservo.attach(8); // put the servo on pin 8
}
  
void loop(void)
{
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println(sensors.getTempCByIndex(0)); // 0 refers to the first IC on the wire 

  tempDegree = sensors.getTempCByIndex(0);

  while((tempDegree > 27) && (servoBracket < ventHatch_open)){      
      servoBracket = servoBracket + 1;
      myservo.write(servoBracket);             
      delay(50);
  }
  while((tempDegree < 27) && (servoBracket > ventHatch_closed)){
      myservo.write(servoBracket);             
      delay(50);
      servoBracket = servoBracket - 1;
  }
 }

Any suggestions?

Hi,
Where does your servo get it's power from? Please don't tell me it's the Arduino!! You never power servos or motors from the arduino it can't deliver the current these need. Servos are usually 4.5-6v, and are you using one of these tiny square battery's that still can't provide enough power!! use 4-6 x AA's mush better.

Power the arduino via the Vin pin (7v+) from the batteries, also to servo. Are the servo and arduino grounds connected?

Let us see your schematic or a drawing of it!! We need more info to help you.
Regards

Mel.

  while((tempDegree > 27) && (servoBracket < ventHatch_open)){     
      servoBracket = servoBracket + 1;
      myservo.write(servoBracket);             
      delay(50);
  }

It is pointless to check the temperature in the while statement, since it never changes.

Separate the process of opening the window from the need to open the window. Create a function, openWindow() to actually open the window. Create another one to actually close the window. Do NOT put anything in those functions that is not DIRECTLY concerned with the process (NOT the need for) of opening or closing the window. Those functions will use for loops, not while loops, to open and close the window.

Then, you'll see that a while statement is inappropriate in loop().

You probably want to open the window when one temperature is reached, and close it at a different temperature. Opening and closing the window as the temperature fluctuates around 27 isn't a good idea. Opening at 27 and closing at 25 is.

I agree with PaulS, is more easy if you create a functions to open and close the window, and replace the "while for a "if", i did a similar project but instead of temperature it was light

Here the code i that i used, i guess it can be easy modified to work with temperature, and controlling a servo

// Pins
int sensorPin = 0;
int lightPin = 3;

// Variables
int lightState = 0;
int lowThreshold = 250;
int highTreshold = 35;

void setup() {
  
  // Start Serial & set pin to output  
  Serial.begin(9600);
  pinMode(lightPin,OUTPUT);
  
}

void loop() {

  // Read the sensor pin
  int sensorValue = analogRead(sensorPin);

  // If light level is low is detected, switch light on
  if (sensorValue < lowThreshold){
    digitalWrite(lightPin, HIGH);
  }
  
  // If light level goes up again, switch the lights off
  if (sensorValue > highTreshold){ 
    digitalWrite(lightPin, LOW);
  }
}

Thanx a lot. I'll try to seperate the open/close sections into functions - good idea (havent a clue on how to do it but I'll figure it out - can´t be too hard)

Also - Great input on the temperature gap. My mistake... if I dont have a gap the hatch will open and close contiuously... :slight_smile: No need för that.

The part with powering the servo with an alternate power source is a little bit over my head... I think. I really happy that I'm able to do a servo sweep at all :slight_smile: I'll try to do a schematic or just take a picture of it... :slight_smile: