Skip to content

Instantly share code, notes, and snippets.

@RyoSaitoOp3
Created October 18, 2015 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyoSaitoOp3/128108922943fd5366f2 to your computer and use it in GitHub Desktop.
Save RyoSaitoOp3/128108922943fd5366f2 to your computer and use it in GitHub Desktop.
#include <PID_v1.h>
#include <LiquidCrystal.h>
#define RelayPin 13
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
float a_in, b_in, temp1_c, temp2_c ;
float timePoints[6] = {2000,0,0,0,0,0};//Max num of timePoints is 6, milli sec
int tempTargets[6] = {74,0,0,0,0,0};//Degrees celcius
float startTime, now;
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//double Setpoint =73;//Degrees Celcius
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,80,0,0, DIRECT);
int WindowSize = 1000;
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);
//シリアル通信で何か受け取るまで待つ
while(Serial.available()<=0){
Serial.println("0,0,0");
delay(500);
}
//画面一旦消去
lcd.clear();
//tell the PID to range between 0 and the full window size
myPID.SetOutputLimits(0, WindowSize);
//turn the PID on
myPID.SetMode(AUTOMATIC);
startTime = millis();
}
void loop() {
//Processingにデータを送るためのコード?
if(Serial.available()>0){
Serial.read();
}
//Reset target temperature
now = millis();
if(timePoints[0]-now+startTime>-2000 & timePoints[0]-now+startTime<=0){
Setpoint = tempTargets[0];
}else if(timePoints[1]-now+startTime>-2000 & timePoints[1]-now+startTime<=0){
Setpoint = tempTargets[1];
}else if(timePoints[2]-now+startTime>-2000 & timePoints[2]-now+startTime<=0){
Setpoint = tempTargets[2];
}else if(timePoints[3]-now+startTime>-2000 & timePoints[3]-now+startTime<=0){
Setpoint = tempTargets[3];
}else if(timePoints[4]-now+startTime>-2000 & timePoints[4]-now+startTime<=0){
Setpoint = tempTargets[4];
}else if(timePoints[5]-now+startTime>-2000 & timePoints[5]-now+startTime<=0){
Setpoint = tempTargets[5];
}
a_in = analogRead(0) ; // アナログ0番ピンからセンサー値を読込む
temp1_c = (a_in * 0.52) + 1.5;
char t1[6]; // 温度表示の文字列の内容は 0.0 ~ 100.0 の可能性があるため、
// 最低でも 5 文字+文字列終端記号の 1 文字分を確保しておきます。
dtostrf( temp1_c, 3, 1, t1 ); // 温度の数値を文字列に変換します。
Serial.print( t1 ); // シリアルモニタに出力
Serial.print(",");
//目標温度のLCDモニタへの表示
lcd.setCursor(0, 0);
lcd.print("Target temp:");
lcd.print(Setpoint);
b_in = analogRead(1) ; // アナログ1番ピンからセンサー値を読込む
temp2_c = (b_in * 0.52) + 1.5;
char t2[6]; // 温度表示の文字列の内容は 0.0 ~ 100.0 の可能性があるため、
// 最低でも 5 文字+文字列終端記号の 1 文字分を確保しておきます。
dtostrf( temp2_c, 3, 1, t2 ); // 温度の数値を文字列に変換します。
Serial.print( t2 ); // シリアルモニタに出力
Serial.print(",");
//温度のLCDモニタへの表示
lcd.setCursor(0, 1);
//lcd.print("In liquid :");
lcd.print(temp1_c);
lcd.print(" ");
lcd.print(temp2_c);
Input = temp2_c;
myPID.Compute();
char t3[6];
dtostrf( Output, 3, 1, t3 ); // Outputの数値を文字列に変換します。
Serial.println(t3);
//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