Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

LEARNING JOURNAL

Mata Kuliah : Interface dan Peripheral


Topik : Membuat HMI dengan Visual Studio
Nama : Julio Abiyu Putra
NIM : 40040620650026

TUGAS BESAR : MEMBUAT HMI DENGAN VISUAL STUDIO


Hasil pembuatan HMI-GUI
1. Kode Program
Imports System.L nq
Imports System.Data.SqlCl ent
Imports System.Data.OleDb
Imports M crosoft.Off ce.Core
Imports Excel = M crosoft.Off ce.Interop.Excel
Imports ExcelAutoFormat = M crosoft.Off ce.Interop.Excel.XlRangeAutoFormat
Imports M crosoft.Off ce.Interop
Imports System.IO
Imports System.Xml.Xflath
Imports System.Data
Imports System.Xml
flubl c Class Form1
D m Strflarse, LDR_1, LDR_2, flot As Str ng
D m LDR_1L, LDR_2L, flotL As Integer
D m L m t As Integer = 10
D m F leflathAndName As Str ng
flr vate Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
ButtonD sconnect.Enabled = False
ButtonConnect.Enabled = False
ButtonStartRecord ng.Enabled = False
ButtonStopRecord ng.Enabled = False
ComboBoxBaudRate.SelectedIndex = 3
ButtonSaveToExcel.He ght = 50
For = 0 To 30 Step 1

Chart1.Ser es("Temperature").flo nts.AddXY(DateT me.Now.ToLongT meStr ng, 0)


If Chart1.Ser es(0).flo nts.Count = L m t Then
Chart1.Ser es(0).flo nts.RemoveAt(0)
End If
Chart1.ChartAreas(0).Ax sY.Max mum = 110
Next
Chart1.ChartAreas(0).Ax sY.Max mum = 120
End Sub
flr vate Sub ButtonScanflort_Cl ck(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButtonScanflort.Cl ck
ComboBoxflort.Items.Clear()
D m myflort As Array
D m As Integer
myflort = IO.florts.Ser alflort.GetflortNames()
ComboBoxflort.Items.AddRange(myflort)
= ComboBoxflort.Items.Count
= -
Try
ComboBoxflort.SelectedIndex =
ButtonConnect.Enabled = True
Catch ex As Except on
MsgBox("Com port not detected", MsgBoxStyle.Cr t cal, "Warn ng !!!")
ComboBoxflort.Text = ""
ComboBoxflort.Items.Clear()
ButtonConnect.Enabled = False
ButtonStartRecord ng.Enabled = False
Return
End Try ComboBoxflort.DroppedDown
= True
End Sub
flr vate Sub ButtonConnect_Cl ck(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButtonConnect.Cl ck
Ser alflort1.BaudRate = ComboBoxBaudRate.SelectedItem
Ser alflort1.flortName = ComboBoxflort.SelectedItem
Ser alflort1.Open()
T merSer al.Start()
ComboBoxflort.Enabled = False
Label1.Enabled = False
ComboBoxBaudRate.Enabled = False
ButtonScanflort.Enabled = False
ButtonConnect.Enabled = False
ButtonD sconnect.Enabled = True
ButtonStartRecord ng.Enabled = True
fl ctureBox1.Image = My.Resources.green
LabelStatus.Text = "Status : Connected"
End Sub
flr vate Sub ButtonD sconnect_Cl ck(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButtonD sconnect.Cl ck
fl ctureBox1.Image = My.Resources.red
fl ctureBox1.V s ble = True
Label2.Text = "Status : D sconnect"
ComboBoxflort.Enabled = True
Label1.Enabled = True
ComboBoxBaudRate.Enabled = True
ButtonScanflort.Enabled = True
ButtonConnect.Enabled = True
ButtonD sconnect.Enabled = False
T merSer al.Stop()
T merDataLogRecord.Stop()
ButtonSaveToExcel.Enabled = True
ButtonStopRecord ng.Enabled = False
Ser alflort1.Close()
End Sub
flr vate Sub ButtonStartRecord ng_Cl ck(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles ButtonStartRecord ng.Cl ck
ButtonStartRecord ng.Enabled = False
ButtonStopRecord ng.Enabled = True
ButtonSaveToExcel.Enabled = False
T merDataLogRecord.Start()
End Sub
flr vate Sub ButtonStopRecord ng_Cl ck(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles ButtonStopRecord ng.Cl ck
ButtonStartRecord ng.Enabled = True
ButtonStopRecord ng.Enabled = False
ButtonSaveToExcel.Enabled = True
T merDataLogRecord.Stop()
fl ctureBox1.V s ble = True
End Sub
flr vate Sub Label2_Cl ck(sender As Object, e As EventArgs) Handles
Label2.Cl ck
End Sub
flr vate Sub GroupBox2_Enter(sender As Object, e As EventArgs) Handles
GroupBox2.Enter
End Sub

flr vate Sub LabelStatus_Cl ck(sender As Object, e As EventArgs) Handles


LabelStatus.Cl ck

End Sub

flr vate Sub ButtonClear_Cl ck(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles ButtonClear.Cl ck
For = 0 To 30 Step 1

Chart1.Ser es("Temperature").flo nts.AddXY(DateT me.Now.ToLongT meStr ng, 0)


If Chart1.Ser es(0).flo nts.Count = L m t Then
Chart1.Ser es(0).flo nts.RemoveAt(0)
End If
Chart1.ChartAreas(0).Ax sY.Max mum = 110
Next
DataGr dV ew1.Rows.Clear()
End Sub
flr vate Sub ButtonSaveToExcel_Cl ck(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButtonSaveToExcel.Cl ck
ButtonSaveToExcel.He ght = 37
ButtonSaveToExcel.Text = "fllease Wa t..."
ButtonSaveToExcel.Enabled = False
ButtonStartRecord ng.Enabled = False
flrogressBarflrocess.V s ble = True
flrogressBarflrocess.Value = 1
D m xlApp As M crosoft.Off ce.Interop.Excel.Appl cat on
D m xlWorkBook As M crosoft.Off ce.Interop.Excel.Workbook
D m xlWorkSheet As M crosoft.Off ce.Interop.Excel.Worksheet
D m m sValue As Object = System.Reflect on.M ss ng.Value
D m As Integer
D m j As Integer
flrogressBarflrocess.Value = 3
xlApp = New M crosoft.Off ce.Interop.Excel.Appl cat on
xlWorkBook = xlApp.Workbooks.Add(m sValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
flrogressBarflrocess.Value = 5
For = 0 To DataGr dV ew1.RowCount - 2
For j = 0 To DataGr dV ew1.ColumnCount - 1
For k As Integer = 1 To DataGr dV ew1.Columns.Count
xlWorkSheet.Cells(1, k) = DataGr dV ew1.Columns(k -
1).HeaderText
xlWorkSheet.Cells( + 2, j + 1) = DataGr dV ew1(j,
).Value.ToStr ng()

Next
Next
Next
flrogressBarflrocess.Value = 8
F leflathAndName = Appl cat on.Startupflath & "\" & Now.Day & "-" &
Now.Month & "-" & Now.Year & ".xlsx"
If F le.Ex sts(F leflathAndName) Then F le.Delete(F leflathAndName)
xlWorkSheet.SaveAs(F leflathAndName)
xlWorkBook.Close()
xlApp.Ǫu t()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
flrogressBarflrocess.Value = 10
MsgBox("Successfully saved" & vbCrLf & "F le are saved at : " &
F leflathAndName, MsgBoxStyle.Informat on, "Informat on")
flrogressBarflrocess.V s ble = False
flrocess.Start(F leflathAndName)
ButtonSaveToExcel.He ght = 50
ButtonSaveToExcel.Text = "Save To MS Excel"
ButtonSaveToExcel.Enabled = True
ButtonStartRecord ng.Enabled = True
End Sub
flr vate Sub releaseObject(ByVal obj As Object)
Try
System.Runt me.InteropServ ces.Marshal.ReleaseComObject(obj)
obj = Noth ng
Catch ex As Except on
obj = Noth ng
F nally
GC.Collect()
End Try
End Sub
flr vate Sub T merDataLogRecord_T ck(ByVal sender As Object, ByVal e As
EventArgs) Handles T merDataLogRecord.T ck
D m LDR_1Log As Str ng
D m DT As DateT me = Now
LDR_1Log = M d(LDR_1, 3, LDR_1L)
DataGr dV ew1.Rows.Add(New Str ng() {DataGr dV ew1.RowCount, LDR_1Log,
DT.ToLongT meStr ng, DT.ToStr ng("dd-MM-yyyy")})
Me.DataGr dV ew1.F rstD splayedScroll ngRowIndex =
Me.DataGr dV ew1.RowCount - 1
Chart1.Ser es("Temperature").flo nts.AddXY(DateT me.Now.ToLongT meStr ng,
LDR_1Log)
If Chart1.Ser es(0).flo nts.Count = L m t Then
Chart1.Ser es(0).flo nts.RemoveAt(0)
End If
If fl ctureBox1.V s ble = True Then
fl ctureBox1.V s ble = False
ElseIf fl ctureBox1.V s ble = False Then
fl ctureBox1.V s ble = True
End If
End Sub
flr vate Sub T merSer al_T ck(ByVal sender As Object, ByVal e As EventArgs)
Handles T merSer al.T ck
Try
D m StrSer alIn As Str ng = Ser alflort1.ReadEx st ng
D m StrSer alInRam As Str ng
D m TB As New TextBox
TB.Mult l ne = True
TB.Text = StrSer alIn
StrSer alInRam = TB.L nes(0).Substr ng(0, 2)
If StrSer alInRam = "L1" Then
LDR_1 = TB.L nes(0)
LDR_1L = LDR_1.Length
Else
LDR_1 = LDR_1
End If
If fl ctureBox1.V s ble = True Then
fl ctureBox1.V s ble = False
ElseIf fl ctureBox1.V s ble = False Then
fl ctureBox1.V s ble = True
End If
LabelSuhu.Text = "Temp (C) = " & M d(LDR_1, 3, LDR_1L)
Catch ex As Except on
End Try
End Sub
End Class

2. Screenshot Tampilan Desain


3. Screenshot Source Code
4. Screenshot Tampilan Start Debugging

5. Screenshot Tampilan Scan Port


6. Screenshot Tampilan Save To MS. Excel

7. Screenshot Tampilan di Excel


8. Kode Program Arduino

#include <Keypad.h>
#include <Wire.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x27, 16, 2);
char customKey, mulaiKey;
//char stringAngka[2];
int indexKeypad = 0;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {11,10,9,8};
byte colPins[COLS] = {7,6,5,4};
int x = 0;
int ssr = 3; // alamati pin SSR
float sp;
float diff, temp_now;
float T_cutoff, T_cuton, nilaicd, nilaisp, nilaidiff;
Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void(* resetFunc) (void) = 0; //declare reset function @ address 0
void setup()
{
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(ssr,OUTPUT);
sp = 0;
diff = 0;
nilaisp = sp;
nilaidiff = diff;
T_cutoff = sp + (diff/2);
T_cuton = sp - (diff/2);
delay(50);
lcd.clear();
}
void loop()
{
customKey = customKeypad.getKey();
if(x == 0){
//lcd.clear();
lcd.setCursor(0,0);
lcd.print("1.SET ON-OFF");
}
if(x == 1){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("2.MULAI");
}
if(x == 2){
//lcd.clear();
lcd.setCursor(0,0);
lcd.print("3.CEK ON-OFF");
}
switch(customKey)
{
case '0' ... '9':
break;

case '#':
break;
case '*':
break;
case 'A':
x++;
break;

case 'B':
x--;
break;

case 'C':
break;

case 'D':
if(x == 0){
lcd.clear();
//analogWrite(ssr,0);
setsp();
setdiff();
}
if(x == 1){
lcd.clear();
mulai();
}
if(x == 2){
lcd.clear();
//analogWrite(ssr,0);
cekonoff();
}
break;
}
if(x > 2){
x = 0;
}
if(x < 0){
x = 2;
}
}
void setdiff(){
lcd.setCursor(0,0);
lcd.print("SET DIFF ");
customKey = customKeypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
diff = diff * 10 + (customKey - '0');
lcd.setCursor(0,1);
lcd.print(diff);
T_cutoff = sp + (diff/2);
T_cuton = sp - (diff/2);
}

if(customKey == '*'){
lcd.clear();
delay(1000);
nilaidiff = diff;
return;
}
setdiff();
}
void setsp(){
lcd.setCursor(0,0);
lcd.print("SET SP ");
customKey = customKeypad.getKey();
if(customKey >= '0' && customKey <= '9')
{
sp = sp * 10 + (customKey - '0');
lcd.setCursor(0,1);
lcd.print(sp);
//lcd.print(customKey);
//sp = customKey;
}
if(customKey == '*'){
lcd.clear();
delay(1000);
nilaisp = sp;
return;
}
setsp();
}
void cekonoff(){
lcd.setCursor(0,0);
lcd.print("SP = ");
lcd.print(nilaisp);

lcd.setCursor(0,1);
lcd.print("Control diff = ");
lcd.print(nilaidiff);

customKey = customKeypad.getKey();

if(customKey == '*'){
lcd.clear();
delay(1000);
return;
}
cekonoff();
}
void mulai(){
if (sp==0) {
lcd.setCursor(0, 0);
lcd.print("Mohon Set SP");
lcd.setCursor(0, 1);
lcd.print("dan Diff Dahulu");
delay(2000);
return;
}
sensors.requestTemperatures();
T_cutoff = sp + (diff/2);
T_cuton = sp - (diff/2);
temp_now = sensors.getTempCByIndex(0);
if (temp_now >= T_cutoff) {
analogWrite(ssr,0);
}
else if (temp_now <= T_cuton) {
analogWrite(ssr,255);
}
//Serial.print("DATA,TIME,");
Serial.print("L1");
Serial.println(temp_now);
lcd.setCursor(0, 0);
lcd.print("SP= ");
lcd.print(sp);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Y ");
lcd.print(temp_now);
lcd.print(" ");
delay(200);
customKey = customKeypad.getKey();
if(customKey == '*'){
lcd.clear();
analogWrite(ssr,0);
resetFunc(); //call reset
delay(800);
return;
}

mulai();
}
9. Screenshot Tampilan Kode Program Arduino IDE

You might also like