Hi guys,
What i want to do is, from 2 temp sensors get the average value and according to that value move a servo motor.At the maximum temperature i also open a relay (fan).
However sometimes my serial print shows -52.40 celcius more or less making my servo move back a position, but the reading from the temp sensors is fine!
Serial print :
Requesting temperatures...DONE
Temperature for Device 1 is: 21.94
Temperature for Device 2 is: 22.37
Average temp is...-52.31
Requesting temperatures...DONE
Temperature for Device 1 is: 22.00
Temperature for Device 2 is: 22.37
Average temp is...-52.28
etc .. etc...
My code is as follows :
#include <Servo.h>
#include <OneWire.h>
#include <DallasTemperature.h>
int DS18S20_Pin = 3; // Set temperature sensor in pin 0
int thresholdcold = 21; // Threshold for cold temperature
int thresholdhot = 23; // 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;
Servo servo1;
float Temp1=0;
float Temp2=0;
float AverageTemp=0;
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 3
// 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);
void setup(void)
{
Serial.begin(9600); // Begin communicating with computer
Serial.println("Dallas Temperature IC Control Library Demo");
servo1.attach(9); // Attaches servo to specified pin
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
}
void loop(void) {
// 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");
Serial.print("Temperature for Device 1 is: ");
Serial.println(sensors.getTempCByIndex(1)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
Serial.print("Temperature for Device 2 is: ");
Serial.println(sensors.getTempCByIndex(0));
delay(1000);
Temp1=sensors.getTempCByIndex(1); //Store sensor 1 value
Temp2=sensors.getTempCByIndex(0); //Store sensor 2 value
AverageTemp= (Temp1 + Temp2) / 2; //Average value of sensor 1 & sensor 2
Serial.print("Average temp is...");
Serial.println (AverageTemp);
// if (Temp1 > 23 || Temp2 > 23 ){
// digitalWrite(led, HIGH); // Testing spikes
// Serial.println ("ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
// }
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);
}
}
}
So... any help would be greatly appreciated!! thanks!