You are on page 1of 3

LAMPIRAN PROGRAM

#include "Wire.h"

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
int pwmout = 3;
int btup = 8;

int btdown = 9;
int btok = 10;

int btupx;
int btdownx;
int btokx;
float setpoint = 35.0;

int kipas = 2 ;
int adclm;
float kp = 3.52;
float ki = 0.24;

float kd = 0.97;
float p,i,d,suhu,pid;

float error,errorx,sumerr;
void setup() {
Serial.begin(9600);
lcd.init();

lcd.init(); // Print a message to the LCD.


lcd.backlight();

lcd.clear();
pinMode(kipas, OUTPUT);
pinMode(pwmout, OUTPUT);
pinMode(btup, INPUT);
pinMode(btdown, INPUT);
pinMode(btok, INPUT);

}
void loop() {

;
btokx = digitalRead(btok);

lcd.setCursor(0,0);
lcd.print("TEKAN SET");

if(btokx == HIGH){
lcd.clear();
delay(1000);
setsuhu();

mulai();
}
}

void setsuhu(){
btokx = digitalRead(btok);
btupx = digitalRead(btup);
btdownx = digitalRead(btdown);

lcd.setCursor(0,0);
lcd.print("SET SUHU: ");

lcd.print(setpoint,1);
if(btupx == HIGH){
delay(200);
setpoint = setpoint + 0.1;

}
if(btdownx == HIGH){
delay(200);
setpoint = setpoint - 0.1;

}
if(setpoint > 42){

setpoint = 35;
}
if(setpoint < 35){
setpoint = 42;

}
if(btokx == HIGH){

delay(200);
lcd.clear();
return;
}
setsuhu();
}

void mulai(){
analogWrite(pwmout,pid);

error = setpoint - suhu;


p = error * kp;

sumerr = error + errorx;


i = ki * sumerr;

d = kd * (error - errorx) ;
pid = p + i + d;
if(pid < 1){
pid = 0;

}
if(pid > 255){
pid = 255;

}
if(suhu < setpoint){
digitalWrite(kipas,LOW);
}

if(suhu > setpoint){


digitalWrite(kipas,HIGH);

}
adclm = analogRead(A0);
suhu = (adclm * (5.0 / 1023.0) * 100) + 3;
lcd.setCursor(0,0);

lcd.print("SP:");
lcd.print(setpoint,1);
lcd.print(" P:");
lcd.print(pid,2);

lcd.print(" ");
lcd.setCursor(0,1);

lcd.print("T:");
lcd.print(suhu,2);
if(error <1 )
{

lcd.setCursor(11,1);
lcd.print(" stop ");

}
else if (error>1)
{
lcd.setCursor(8,1);
lcd.print("Er:");
lcd.print(error,1);

}
errorx = error;

delay(1000);
mulai();

You might also like