#include //START OF TEMP CONTROL INITIALISE //#define DBG_SWITCH // Remove remark slashes to enable extensive debugging int FanRelais = 9; // Define cooling fan relais pin float Tlow = 1.0; // Temperature the fan should be switched off float Thigh = 5.0; // Temperature the fan should be switched on boolean pwm = true; // True for Pulse Width Modulation, false for on/off control boolean FanEnabled = false; // Flag fan to be off float ST = 0.0; int PW = 0; // START OF SETUP void setup() { Serial.begin (19200); // Setup the serial port and define communication speed pinMode(FanRelais, OUTPUT); // Define fan relais pin as output Serial.println("Fan Off!"); if (!pwm) { digitalWrite(FanRelais, LOW); // Disable Fan relais FanEnabled = false; // Flag fan to be off } else { analogWrite(FanRelais, 0); // Disable Fan relais } } // END OF SETUP float Vin = 5; // Voltage fed at the temperature sensor float Rlow = 12513; // Resistance for lowest temperature sensor float TRlow = 20; // Temerature lowest temperature sensor float Rhigh = 10000; // Resistance for highest temperature sensor float TRhigh = 25; // Temerature highest temperature sensor float Tsteps = TRhigh - TRlow; float st = (Rlow - Rhigh) / Tsteps; // Sensor resistor increase per degree float Rr = 23500; // Value of resistor positioned in serial with the temp sensor int Sensor; // Sensor valua measured at analoge port float Vr; // Variable for measured voltage on resistor float I; // Current variable float Rtot; // Total resistor value measured float Rt; // Resistance temperature sensor variable float T; // Calculated temperature variable void loop() { // Wait a second delay(3000); // Measure value at analog input Sensor = analogRead(A0); //Serial.print("Sensor value: "); //Serial.println(Sensor); // Calculate the voltage on the resistor Vr = Sensor * (5.0 / 1023.0); //Serial.print("Voltage 23.5k ohm resistor: "); //Serial.println(Vr); // Calculate current running throug the resistor I = Vr / Rr; //Serial.print("Current: "); //Serial.println(I); // Calculate total resistor (temp resistor + serial resistor) Rtot = Vin / I; //Serial.print("Rtotaal: "); //Serial.println(Rtot); // Calculate value of temp. sensor resistor Rt = Rtot - Rr; //Serial.print("Rt: "); //Serial.println(Rt); // Calcualte temperature measured T = TRhigh - ((Rt - Rhigh) / st); Serial.print("Temperature: "); Serial.println(T); if (!pwm) { if (T > Thigh and !FanEnabled) { Serial.println("Fan On!"); digitalWrite(FanRelais, HIGH); // Enable Fan FanEnabled = true; } if (T < Tlow and FanEnabled) { Serial.println("Fan Off!"); digitalWrite(FanRelais, LOW); // Disable Fan FanEnabled = false; } } else { ST = 255 / (Thigh - Tlow); PW = ST * (T - Tlow); if (PW < 0) PW = 0; else if (PW > 255) PW = 255; Serial.print("PWM value: "); Serial.println(PW); analogWrite(FanRelais, PW); } Serial.println(""); }