temperature controlled servo

i have been working on this project for some time, the idea here is to sense the temperature from the temperature sensor DS18B20 and after a preset temperature is crossed the board should send the signals to a servo to go from 0 degrees to 180 degrees and hold the position.The program compiles but the servo doesnt get triggered by temp limit crossing.

here is my code
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Servo.h>

#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);
Servo myservo;
int val;
int pos = 0; // variable to store the servo position;

void setup(void)
{
Serial.begin(9600);
sensors.begin();
myservo.attach(9);
}

void loop(void)
{
static float tempPrec=40;
float temp;
sensors.requestTemperatures();

temp=sensors.getTempCByIndex(0);

Serial.print("Temperature : ");
Serial.println(sensors.getTempCByIndex(0));
if (temp == tempPrec)
{for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(750);
}
{
val = map(temp,50,80,100,180);
myservo.write(val);
tempPrec=temp;
delay(150);

}
}
}

venkyD.ino (892 Bytes)

You compare if a float variable is equal to another float variable. That is very unlikely to happen.

The tempPrec is 40.000000.
The temp could perhaps be 49.999998 or 40.000005. There is no reason for it to be exactly 40.000000.

Koepel:
You compare if a float variable is equal to another float variable. That is very unlikely to happen.

The tempPrec is 40.000000.
The temp could perhaps be 49.999998 or 40.000005. There is no reason for it to be exactly 40.000000.

yes i understand it but the even after reaching temperatures as high as 98 degrees celcius the servo doesnt get triggered but instead it starts to move first itself

That's because 98 is not equal to 40. Try >= instead of ==.

Steve

Did you make a test sketch for the DS18B20 ? and another test sketch for the servo motor ?
Can you make them work ? Can you set the servo at a certain position ?
I don't know what is going on, so you have to do a few tests yourself. Don't try to make the final project at once, do it step by step.

When you do the "myservo.attach(9);", the servo signal starts at pin 9. Since it is not known at what position the servo motor is, it is going somewhere to the middle position.

The 5V pin of an Arduino board can not be used for a servo motor. Well, it can, with some luck. If you have a bad quality USB cable, the Arduino resets due to a voltage drop as soon as the servo motor starts moving.

That means your Arduino board could reset over and over again. You could print a message to the serial monitor to tell if the board was reset:

void setup(void)
{
  Serial.begin(9600);
  Serial.println("The sketch has started");