Ok guys i've been very busy to understand the solution to my problem... I used a code which seems to work... Dont know yet, i have to test it for a few days.
#include <Servo.h>
#include <OneWire.h>
#include <DallasTemperature.h>
int DS18S20_Pin = 2; // Set temperature sensor in pin 0
int thresholdcold = 21; // Threshold for cold temperature
int thresholdhot = 24; // Threshold for hot temperature
int thresholdnormal =22; // Threshold for normal temperature
int servocold = 174; // Angle in which servo will go to
int servohot = 0; // Angle in which servo will go to
int servonormal = 80; // Angle in which servo will go to
int previousPosition = 80;
int ServoDelay = 30; // Servo delay
int led = 13;
int Relay_Pin = 12;
OneWire ds(DS18S20_Pin);
unsigned long Time = (millis()/1000);
Servo servo1;
#define ONEWIREPIN 2 /* OneWire bus on digital pin 7 */
#define MAXSENSORS 2 /* Maximum number of sensors on OneWire bus */
// Model IDs
#define DS18S20 0x10
#define DS18B20 0x28
#define DS1822 0x22
// OneWire commands
#define CONVERT_T 0x44 // Tells device to take a temperature reading and put it on the scratchpad
#define COPYSCRATCH 0x48 // Copy EEPROM
#define READSCRATCH 0xBE // Read EEPROM
#define WRITESCRATCH 0x4E // Write to EEPROM
#define RECALLSCRATCH 0xB8 // Reload from last known
#define READPOWERSUPPLY 0xB4 // Determine if device needs parasite power
#define ALARMSEARCH 0xEC // Query bus for devices with an alarm condition
class Sensor /* hold info for a DS18 class digital temperature sensor */
{
public:
byte addr[8];
boolean parasite;
float temp;
};
int HighByte, LowByte, TReading, SignBit, Tc_100;
byte i, j, sensors;
byte present = 0;
boolean ready;
int dt;
byte data[12];
byte addr[8];
//OneWire ds(ONEWIREPIN); // DS18S20 Temperature chip i/o
Sensor DS[MAXSENSORS]; /* array of digital sensors */
void setup(void) {
// initialize inputs/outputs
// start serial port
Serial.begin(9600);
servo1.attach(9); // Attaches servo to specified pin
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
sensors = 0;
Serial.println("Searching for sensors...");
while ( ds.search(addr)) {
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.print("CRC is not valid!\n");
break;
}
delay(1000);
ds.write(READPOWERSUPPLY);
boolean parasite = !ds.read_bit();
present = ds.reset();
Serial.print("temp");
Serial.print(sensors,DEC);
Serial.print(": ");
DS[sensors].parasite = parasite;
for( i = 0; i < 8; i++) {
DS[sensors].addr[i] = addr[i];
Serial.print(addr[i], HEX);
Serial.print(" ");
}
//Serial.print(addr,HEX);
if ( addr[0] == DS18S20) {
Serial.print(" DS18S20");
}
else if ( addr[0] == DS18B20) {
Serial.print(" DS18B20");
}
else {
Serial.print(" unknown");
}
if (DS[sensors].parasite) {Serial.print(" parasite");} else {Serial.print(" powered");}
Serial.println();
sensors++;
}
Serial.print(sensors,DEC);
Serial.print(" sensors found");
Serial.println();
for (i=0; i<sensors; i++) {
Serial.print("temp");
Serial.print(i,DEC);
if (i<sensors-1) {
Serial.print(",");
}
}
Serial.println();
}
void get_ds(int sensors) { /* read sensor data */
for (i=0; i<sensors; i++) {
ds.reset();
ds.select(DS[i].addr);
ds.write(CONVERT_T,DS[i].parasite); // start conversion, with parasite power off at the end
if (DS[i].parasite) {
dt = 75;
delay(750); /* no way to test if ready, so wait max time */
} else {
ready = false;
dt = 0;
delay(10);
while (!ready && dt<75) { /* wait for ready signal */
delay(10);
ready = ds.read_bit();
dt++;
}
}
present = ds.reset();
ds.select(DS[i].addr);
ds.write(READSCRATCH); // Read Scratchpad
for ( j = 0; j < 9; j++) { // we need 9 bytes
data[j] = ds.read();
}
/* check for valid data */
if ( (data[7] == 0x10) || (OneWire::crc8( addr, 8) != addr[8]) ) {
LowByte = data[0];
HighByte = data[1];
TReading = (HighByte << 8) + LowByte;
SignBit = TReading & 0x8000; // test most sig bit
if (SignBit) // negative
{
TReading = (TReading ^ 0xffff) + 1; // 2's comp
}
if (DS[i].addr[0] == DS18B20) { /* DS18B20 0.0625 deg resolution */
Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25
}
else if ( DS[i].addr[0] == DS18S20) { /* DS18S20 0.5 deg resolution */
Tc_100 = (TReading*100/2);
}
if (SignBit) {
DS[i].temp = - (float) Tc_100 / 100;
} else {
DS[i].temp = (float) Tc_100 / 100;
}
} else { /* invalid data (e.g. disconnected sensor) */
DS[i].temp = NAN;
}
}
}
void loop(void) {
float temp;
get_ds(sensors);
for (i=0; i<sensors; i++) {
if (isnan(DS[i].temp)) {
Serial.print("NaN");
} else {
Serial.print(DS[i].temp,2);
}
if (i<sensors-1) {
Serial.print(",");
}
}
Serial.println();
float AverageTemp = ((DS[0].temp) + (DS[1].temp)) / 2.0f ;
Serial.print("Average Temp is : ");
Serial.println ( AverageTemp);
delay(5000);
if(AverageTemp <= thresholdcold) // If temperature is above the threshold, activate sequence
{
if (servocold != previousPosition){
for(previousPosition != 0; previousPosition < servocold; previousPosition += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo1.write(previousPosition); // tell servo to go to position in variable 'pos'
delay(ServoDelay); // waits 15ms for the servo to reach the position
}
servo1.write(servocold);
previousPosition = servocold;
}
}
else if(AverageTemp >= thresholdhot) // If temperature is above the threshold, activate sequence
{
if (servohot != previousPosition){
for(previousPosition != thresholdhot; previousPosition -= 1;) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo1.write(previousPosition); // tell servo to go to position in variable 'pos'
delay(ServoDelay); // waits 15ms for the servo to reach the position
}
servo1.write(servohot);
previousPosition = servohot;
}
digitalWrite(led, HIGH);
digitalWrite(Relay_Pin, HIGH);
}
else if(AverageTemp >= thresholdnormal) // If Fract is above the threshold, activate sequence
{
while (previousPosition != servonormal){
if (previousPosition < servonormal) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo1.write(previousPosition++) ; // tell servo to go to position in variable 'pos'
delay(ServoDelay); // waits 15ms for the servo to reach the position
}
else // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo1.write(previousPosition--); // tell servo to go to position in variable 'pos'
delay(ServoDelay); // waits 15ms for the servo to reach the position
}
servo1.write(servonormal);
//previousPosition = servonormal;
digitalWrite(led, LOW);
digitalWrite(Relay_Pin, LOW);
}
}
}
Cause this is overly complex for me, when i test the program and i disconnect one sensor my serial print says -0.06:
23.18,22.81
Average Temp is : 22.99
23.25,22.81
Average Temp is : 23.03
[b]-0.06[/b],22.87
Average Temp is : 11.41
28.37,23.25
Average Temp is : 25.81
25.93,23.06
Average Temp is : 24.49
Is that correct? Shouldnt it say Invalid CRC or something? If this is normal what can i do to print out to me that a sensor is disconnected?