Ecco il codice.
Può essere un problema che ogni loop dura quasi 1 secondo?
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <avr/wdt.h>
LiquidCrystal_I2C lcd(0x20,16,2);
unsigned long time;
unsigned long loop_time;
int tempoloop;
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 6
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#include <PID_v1.h>
#define RelayPin 7
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
int WindowSize = 5000;
unsigned long windowStartTime;
void setup(){
lcd.init();
lcd.backlight();
wdt_enable(WDTO_8S);
Serial.begin(9600);
time=millis();
loop_time=millis();
windowStartTime = millis();
//initialize the variables we're linked to
Setpoint = 37.7;
//tell the PID to range between 0 and the full window size
myPID.SetOutputLimits(0, WindowSize);
//turn the PID on
myPID.SetMode(AUTOMATIC);
sensors.begin();
}
void loop(){
time=millis();
sensors.requestTemperatures();
Input = sensors.getTempCByIndex(0);
Serial.print(" temperatura ");
Serial.print(Input);
lcd.setCursor(7,0);
lcd.print(" TE ");
lcd.setCursor(11,0);
lcd.print(Input);
myPID.Compute();
/************************************************
* turn the output pin on/off based on pid output
************************************************/
if(millis() - windowStartTime>WindowSize)
{ //time to shift the Relay Window
windowStartTime += WindowSize;
}
if(Output < millis() - windowStartTime) digitalWrite(RelayPin,HIGH), lcd.setCursor(5,1), lcd.print("T 1");
else digitalWrite(RelayPin,LOW), lcd.setCursor(5,1), lcd.print("T 0");;
Serial.print(" output ");
Serial.print(Output);
wdt_reset();
loop_time=millis();
tempoloop=loop_time-time;
Serial.print(" ");
Serial.println(tempoloop);
}