首页 文章

带有ds1820的Arduino PID继电器输出表现不尽如人意

提问于
浏览
0

我正在尝试将带有PID库的ds1820温度探头用于继电器输出 . 我从探头得到的温度很好,但我希望继电器输出,在这种情况下只是打开/关闭的打印语句,在温度高于设定值时改变 . 他们不 . 也许我已经错过了一些东西,但我认为另外一套眼睛是有用的,因为它几乎是逐字逐句的PID库示例代码,修改从ds1820获取输入,可能非常有用给别人在这个例子中,设定值是60.我希望打印语句在温度低于60时打开,在温度高于时关闭 . 如果温度低于60,我得到的是“开”,如果温度高于60,我仍然“开启” .

/*-----( Import needed libraries )-----*/
// Get 1-wire Library here: http://www.pjrc.com/teensy/td_libs_OneWire.html
#include <OneWire.h>
//Get DallasTemperature Library here:  http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
#include <DallasTemperature.h>
#include <PID_v1.h>
#define ONE_WIRE_BUS_PIN 3

OneWire oneWire(ONE_WIRE_BUS_PIN);
DallasTemperature sensors(&oneWire);
DeviceAddress Probe01 = { 0x28, 0x92, 0xBA, 0xAF, 0x06, 0x00, 0x00, 0xD1 }; //x1

//Define Variables we'll be connecting to
double pidInputTemp;
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
double Kp=2, Ki=5, Kd=1;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

int WindowSize = 5000;
unsigned long windowStartTime;
void setup()
{
  Serial.begin(19200);
  sensors.begin();
  // set the resolution of the ds1820s to 12 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Probe01, 12);
  windowStartTime = millis();

  //initialize the variables we're linked to
  Setpoint = 60;

  //tell the PID to range between 0 and the full window size
  myPID.SetOutputLimits(0, WindowSize);

  //turn the PID on
  myPID.SetMode(AUTOMATIC);
}

void loop()
{
  sensors.requestTemperatures(); 
  theTemperature(Probe01);
  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) Serial.println("On");
  else Serial.println("Off");

}
void theTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
   if (tempC == -127.00) 
   {
   Serial.println("Error getting temperature  ");
   } 
   else
   {
   float tempF = (DallasTemperature::toFahrenheit(tempC));
   Serial.println("Setpoint: ");
   Serial.println(Setpoint);
   Serial.println("Now: ");
   Serial.println(tempF);
   Input = tempF;
   }
}

1 回答

相关问题