Issue with reading value from bs18b20 sensor.

Hi hope everyone would enjoying good health. I am new to arduino and just used the programming for the first time to develop my semester project. The programming is as follow. When I declare Tc=constant then program works well but when I put it equivalent to variable which is valued by the sensor, my motor starts working ridiculously.Can any one help me.

I want to get temperature(Tc) value from the sensor when program starts, save it and use it in my programming. Sensor should provide one value at any instant that is saved in Tc.

#include <Servo.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

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

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
Servo myservo;
const float Treq=35;
float Tc;
const float Cpc=4195.5;
const float Cph=4185.1;
const float Mc=0.0035824;
const float Th=60; //hot water at 60 degrees Celcuis
const float Slope=0.0000030619;
const float Speed=0.248275862;

void setup()
{
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");

// Start up the library
sensors.begin();
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
//Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
//Serial.println("DONE");
// After we got the temperatures, we can print them here.
// We use the function ByIndex, and as an example get the temperature from the first sensor only.
//Serial.print("Temperature for the device 1 (index 0) is: ");
Tc=sensors.getTempCByIndex(0);
Serial.print("cold water Temp =");
Serial.print(Tc);
Serial.print(" C ");
float Qreq = McCpc(Treq-Tc);
float Mh = (Qreq)/(Cph*(Th-Treq));
float Rotation = Mh/Slope;
float Turntime= Rotation/Speed;
myservo.attach(9);
myservo.write(0);
delay(Turntime);
myservo.write(90);
delay(20000);
myservo.write(180);
delay(Turntime);
myservo.detach();
}

void loop() {

}

 Serial.print("cold water Temp =");
  Serial.print(Tc);

What does this print?

Pete

When I declare Tc=constant then program works well but when I put it equivalent to variable which is valued by the sensor, my motor starts working ridiculously.Can any one help me.

When you say my motor starts working ridiculously, you are required to clearly mention the meaning of it. Is the motor just turning forward and backward or no turning at all?

Your motor is not turning at all? You have put all the codes in the setup. There are three positions (00, 900, and 1800) that you have mentioned in your program. When I have moved your codes into loop() function, the motor is working as you have wanted in your program. Look at the following codes of your original post with a little bit formatting in the serial display.

#include <Servo.h>
#include <OneWire.h>
#include <DallasTemperature.h>
      // Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

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

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
Servo myservo;
const float Treq = 35;
float Tc;
const float Cpc = 4195.5;
const float Cph = 4185.1;
const float Mc = 0.0035824;
const float Th = 60; //hot water at 60 degrees Celcuis
const float Slope = 0.0000030619;
const float Speed = 0.248275862;

void setup()
{
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  //Serial.print("Requesting temperatures...");
}

void loop()
{
  sensors.requestTemperatures(); // Send the command to get temperatures
  //Serial.println("DONE");
  // After we got the temperatures, we can print them here.
  // We use the function ByIndex, and as an example get the temperature from the first sensor only.
  //Serial.print("Temperature for the device 1 (index 0) is: ");
  Tc = sensors.getTempCByIndex(0);
  Serial.print("cold water Temp =");
  Serial.print(Tc);
  Serial.println(" C ");
  float Qreq = Mc * Cpc * (Treq - Tc);
  float Mh = (Qreq) / (Cph * (Th - Treq));
  float Rotation = Mh / Slope;
  float Turntime = Rotation / Speed;
  myservo.attach(9);
  myservo.write(0);
  delay(Turntime);
  myservo.write(90);
  delay(20000);
  myservo.write(180);
  delay(Turntime);
  myservo.detach();
}