Paso 4: Lógica de control de temperatura
El código consta de 3 partes:
-lectura de temperatura del sensor y de la escritura en pantalla, comprobamos la temperatura cada 800ms
-botones de lectura y escritura la temperatura deseada en la pantalla
-decidir entre calefacción, refrigeración y apagado. Se incluyeron histéresis de temperatura para evitar la conmutación entre dos Estados que a menudo, cuando estamos cerca de la temperatura deseada.
PROGRAMA:
#include <OneWire.h> #include <DallasTemperature.h> #include <LiquidCrystal.h> unsigned long lasttime; int comand; float oldTc; float Td = 21; //desired T, controlled with buttonI and buttonD float Th = 1.0; //allowed deviation float Tc; //chamber temperature int buttonD = 9; //button which decrease desired T int buttonI = 8; //button which increase desired T int relay1 = 5; int relay2 = 4; //FUNCTION WHICH READS CHAMBER TEMPERATURE #define ONE_WIRE_BUS 6 // Temperature sensors data wire is plugged into pin 6 on the Arduino. OneWire oneWire(ONE_WIRE_BUS); //Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&oneWire); LiquidCrystal lcd(A3,A2,A1,A0,13,12); void setup(void){ lcd.begin(16, 2); delay(200); // Start up the library sensors.begin(); sensors.setWaitForConversion(false); pinMode(relay1, OUTPUT); pinMode(relay2, OUTPUT); lasttime = millis(); } void loop(void) { int button_state; if((millis()-lasttime)>800){ Tc = sensors.getTempCByIndex(0); sensors.requestTemperatures(); //Send the command to get temperatures. lasttime = millis(); } lcd.setCursor(0, 0); lcd.print("T="); lcd.print(Tc,1); lcd.print("C "); lcd.setCursor(0, 1); lcd.print("Desired T="); lcd.print(Td,1); lcd.print("C "); int down = 0; button_state=digitalRead(buttonD); if(button_state==0) { Td = Td - 0.1; down = 1; } button_state=digitalRead(buttonI); if(button_state==0) { Td = Td + 0.1; down = 1; } if (down) { //for faster/better button response delay(200); } //SELECTING BETWEEN HEATING, COOLING AND OFF if(Tc<=(Td-Th/2)){ comand = 1; } if(Tc>=(Td+Th/2)){ comand = -1; } if(Tc>Td && oldTc<=Td) { //hysteresis comand = 0; } if(Tc=Td) { //hysteresis comand = 0; } oldTc = Tc; //commands for heating, cooling and off lcd.setCursor(9, 0); if(comand==-1) { //cooling digitalWrite(4, HIGH); digitalWrite(5, LOW); lcd.print("COOLING"); } if(comand==1){ //heating digitalWrite(5, HIGH); digitalWrite(4, LOW); lcd.print("HEATING"); } if(comand==0) { //off digitalWrite(4, HIGH); digitalWrite(5, HIGH); lcd.print("OFF "); } }