Created
October 18, 2015 08:23
-
-
Save RyoSaitoOp3/48476c3dd470d0725af6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <PID_v1.h> //PID制御用のライブラリを読み込む | |
#include <LiquidCrystal.h> //LCD表示用のライブラリを読み込む | |
#define RelayPin 13 //SSRに接続するピンがpin13であると指定する | |
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2); | |
float a_in, temp_c ; | |
double Setpoint = 70; //目標温度(摂氏) | |
double Input, Output; //PIDライブラリが利用する変数を宣言 | |
//PIDライブラリを使用する | |
//4, 5, 6番目の変数は、それぞれ制御用の係数。 | |
//4番目の変数を大きくすると、温度差が小さいときでも出力は強い。 | |
//4番目の変数を小さくすると、温度差が大きいときでも出力は弱い。通常は80のままでいいはず | |
PID myPID(&Input, &Output, &Setpoint,80,0,0, DIRECT); | |
int WindowSize = 1000; //時間枠(ミリ秒)。この時間枠の中で1回ON/OFFを行う。 | |
void setup() | |
{ | |
Serial.begin(9600) ; // パソコン(ArduinoIDE)とシリアル通信の準備を行う | |
//LCDの準備、起動メッセージの表示 | |
lcd.begin(16, 2); | |
lcd.clear(); | |
lcd.setCursor(0, 0); | |
lcd.print("hello, world!"); | |
//Relay PINを出力に | |
pinMode(RelayPin, OUTPUT); | |
lcd.clear(); | |
myPID.SetOutputLimits(0, WindowSize); //PIDのOutputの最大値をWindowSizeの値にする。 | |
myPID.SetMode(AUTOMATIC); //PID制御の処理を開始する | |
} | |
void loop() { | |
//目標温度のLCDモニタへの表示 | |
lcd.setCursor(0, 0); | |
lcd.print(" TargetTemp"); | |
lcd.print(Setpoint); | |
a_in = analogRead(0) ; // アナログ0番ピンからセンサー値を読込む | |
temp_c = (a_in * 0.58388) - 1.42141; | |
char t1[6]; // 温度表示の文字列の内容は 0.0 ~ 100.0 の可能性があるため、 | |
// 最低でも 5 文字+文字列終端記号の 1 文字分を確保しておく | |
dtostrf( temp_c, 3, 1, t1 ); // 温度の数値を文字列に変換します。 | |
//現在温度のLCDへの表示 | |
lcd.setCursor(0, 1); | |
lcd.print("CurrentTemp"); | |
lcd.print(t1); | |
//現在温度を入力して、出力を計算する | |
Input = temp_c; | |
myPID.Compute(); | |
//SSRの制御 | |
digitalWrite(RelayPin, HIGH); | |
delay(Output); | |
digitalWrite(RelayPin, LOW); | |
delay(WindowSize-Output); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment