#include <Arduino.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <PID_v1.h>
#define ONE_WIRE_BUS 2
#define LedPIN 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensor(&oneWire);
DeviceAddress tempDeviceAddress;
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
int target = 10;
//Specify the links and initial tuning parameters
byte Kp=2, Ki=5, Kd=1;
//double Kp=2, Ki=5, Kd=1;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
//int WindowSize = 1000;
//unsigned long windowStartTime;
void setup()
{
Serial.begin(9600);
pinMode(LedPIN, OUTPUT);
digitalWrite(LedPIN, LOW);
sensor.begin();
//windowStartTime = millis();
//initialize the variables we're linked to
//Setpoint = 100;
Setpoint = target;
//tell the PID to range between 0 and the full window size
//myPID.SetOutputLimits(0, WindowSize);
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensor.getTempC(deviceAddress);
if (tempC == DEVICE_DISCONNECTED_C)
{
Serial.println("Error: Could not read temperature data");
return;
}
Input = tempC;
//Serial.print("Temp C: ");
//Serial.print(tempC);
}
void loop()
{
sensor.requestTemperatures(); // Send the command to get temperatures
sensor.getAddress(tempDeviceAddress, 0);
// It responds almost immediately. Let's print out the data
printTemperature(tempDeviceAddress); // Use
//-----------------------------------------------------------------------
if (Input <= 0) {
myPID.SetMode(MANUAL);
Output = 0;
}
if ((Input > 0) && (Input < 4)) {
myPID.SetMode(MANUAL);
Output = 100;
}
if (Input >= 5) myPID.SetMode(AUTOMATIC);
myPID.Compute();
analogWrite(LedPIN, Output);
Serial.println(Output);
}