I'm looking to tweak some code for a temperature control. Right now I have an issue with my set temperature. the temperature reading sometimes fluctuates over the trigger set point causing my output to rapidly cycle on off on off on off as the reading goes from 34.99 to 40. i'll be switching a high current load with a secondary relay and I'll burn out the contacts in no time if it's rapidly switching like this.
i'm thinking i have to reorganize everything and do a counted for loop to insert a delay or something.
even if I can manage to have it, say turn on until 35c...turn off... but not turn back on until 34.
I'm new at programming this code, and don't really know how to go about getting what I need. Thanks in advance for any ideas or solutions!
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
#define THERMISTORPIN A0
#define THERMISTORNOMINAL 10000
#define TEMPERATURENOMINAL 25
#define NUMSAMPLES 20
#define BCOEFFICIENT 3950
#define SERIESRESISTOR 10000
#define TEMPMIN 30
#define TEMPMAX 42
int TEMPSET = 39;
int VALDOWN = 0;
int VALUP = 0;
int LASTUP = 0;
int LASTDOWN = 0;
int L = 0;
const int relay =11;
uint16_t samples[NUMSAMPLES];
void setup()
{
pinMode(11, OUTPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
lcd.begin(16,2);
lcd.backlight();
lcd.setCursor(0,0);
lcd.clear();
}
void loop(void){
uint8_t i;
float average;
for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(THERMISTORPIN);
delay(10);
}
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
float celcius;
celcius = average / THERMISTORNOMINAL; // (R/Ro)
celcius = log(celcius); // ln(R/Ro)
celcius /= BCOEFFICIENT; // 1/B * ln(R/Ro)
celcius += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
celcius = 1.0 / celcius; // Invert
celcius -= 273.15; // convert to C
VALDOWN = digitalRead(4);
if (VALDOWN != LASTDOWN) {
LASTDOWN = VALDOWN;
if (VALDOWN == LOW) {TEMPSET = TEMPSET-1;}
delay(20);
}
VALUP = digitalRead(5);
if (VALUP != LASTUP) {
LASTUP = VALUP ;
if (VALUP == LOW) {TEMPSET = TEMPSET +1;}
delay(20);
}
lcd.setCursor(0,0);
lcd.print("Temp ");
lcd.print (celcius);
lcd.print((char)223);
lcd.print("C ");
lcd.setCursor(0,1);
lcd.print("Set ");
lcd.print (TEMPSET);
lcd.print((char)223);
lcd.print("C");
if(TEMPSET >= TEMPMAX)
{
(TEMPSET = TEMPMAX);
}
if(TEMPSET <= TEMPMIN)
{
(TEMPSET = TEMPMIN);
}
if (celcius < TEMPSET){
digitalWrite(11, HIGH);
} else {
}
if (celcius > TEMPSET){
digitalWrite(11, LOW);
} else{
}
}
/* --(end main loop )-- */
/* ( THE END ) */