Translate

Monitoring Tegangan Dengan Arduino dan Interface DELPHI 7

Monitoring Tegangan Dengan Arduino dan Interface DELPHI 7


             Pada kesempatan emas ini saya akan menjelaskan mengenai bagaimana cara membuat interface Delphi 7 dari mikrokontroller Arduino, jadi prinsip kerjanya begini, Arduino mengirimkan data berupa data adc, yang berjumlah 2 buah yaitu adc A dan adc B. kemudian didalam delphi ada dua buah program yaitu menampilkan kedua data tersebut kedalam satu grafik dan yang kedua yaitu menampilkan kedua data tersebut ke grafik yang terpisah atau sendiri-sendiri. yang harus diperhatikan yaitu pertama install dulu comport untuk delphinya, disini saya menggunakan cport 3.10. untuk lebih jelasnya berikut program dan skemanya.



a. Arduino Uno




b. Program Arduino

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  float sensorValue = analogRead(A0);
  float sensorValue1 = analogRead(A1);
 
  // print out the value you read:
  Serial.print(sensorValue),Serial.println ('a');
  Serial.print(sensorValue1),Serial.println ('b');
 
  delay(50);        // delay in between reads for stability
}



c. Program Interface Grafik Delphi 7 ( Satu Grafik isi 2 Data )

 unit TA;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls,strutils, TeeProcs, TeEngine, Chart, CPort, mmsystem,
  Series, Buttons;

type
  TForm1 = class(TForm)
    ComPort1: TComPort;
    GroupBox1: TGroupBox;
    Edit1: TEdit;
    Edit2: TEdit;
    Chart1: TChart;
    Shape1: TShape;
    Shape2: TShape;
    GroupBox2: TGroupBox;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Memo1: TMemo;
    Label1: TLabel;
    Label2: TLabel;
    Timer1: TTimer;
    Label7: TLabel;
    Label8: TLabel;
    GroupBox4: TGroupBox;
    Edit3: TEdit;
    Edit4: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    BitBtn1: TBitBtn;
    Series1: TFastLineSeries;
    Series2: TFastLineSeries;
    procedure ComPort1RxChar(Sender: TObject; Count: Integer);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Memo1Change(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
var buff:string;
begin
comport1.ReadStr(buff,count);
memo1.Text:=memo1.Text+buff;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
comport1.ShowSetupDialog;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if button2.Caption='Connect' then
   begin
   button2.Caption:='Disconnect';
   comport1.Open;
   end
else if button2.Caption='Disconnect' then
   begin
   button2.Caption:='Connect';
   comport1.Close;
   end;

end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Edit1.Color:=clwhite;
Edit2.Color:=clwhite;
edit1.text:='';
edit2.text:='';
memo1.Text:='';
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
close;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ThousandSeparator:=',';
DecimalSeparator:='.';
end;

procedure TForm1.Memo1Change(Sender: TObject);
var ruang,kulit:double;
    rin,kin:string;
    jumlahdata:integer;

begin
  shape1.Brush.Color:=clgreen;
  shape2.Brush.Color:=clgreen;
  jumlahdata:=chart1.GetMaxValuesCount;
  form1.Caption:=inttostr(jumlahdata);
  if jumlahdata =10000 then
     begin
     chart1.Series[0].Clear;
     chart1.Series[1].Clear;
     end;

  rin:=memo1.Lines[memo1.Lines.count-2];
  kin:=memo1.Lines[memo1.Lines.count-1];

  if rightstr(rin,1)= 'a' then
     begin
     edit1.Text:=leftstr(rin,length(rin)-1);
     ruang:=strtofloat(edit1.text);
     chart1.Series[0].Add(ruang,'',clred) ;
     shape1.Brush.Color:=clred;
     end;
  if rightstr(kin,1)='b' then
     begin
     edit2.Text:=leftstr(kin,length(kin)-1);
     kulit:=strtofloat(edit2.text);
     chart1.Series[1].Add(kulit,'',clgreen) ;
     shape2.Brush.Color:=clred;
     end;


end;


procedure TForm1.Edit1Change(Sender: TObject);
var ruang:double;
begin
ruang:=strtofloat(edit1.text);
if ruang<30 then
   begin
   edit1.Color:=clblue;
   edit3.text:= 'Lembab';
   end;
if ruang>31 then
   begin
   edit1.Color:=clred;
   edit3.text:= 'Kering';
   end;
if (ruang>=31) and (ruang<=30) then
   begin
   edit1.Color:=clwhite;
   edit3.text:= 'Normal';
   end;
end;


procedure TForm1.Edit2Change(Sender: TObject);
var kulit:double;
begin
kulit:=strtofloat(edit2.text);
if kulit<60 then
   begin
   edit2.Color:=clblue;
   edit4.text:= 'Kering';
   end;
if kulit>61 then
   begin
   edit2.Color:=clred;
   edit4.text:= 'Lembab';
   end;
if (kulit>=61) and (kulit<=60) then
   begin
   edit2.Color:=clwhite;
   edit4.text:= 'Normal';
   end;
end;



procedure TForm1.Timer1Timer(Sender: TObject);
begin
label7.Caption:='Date  '+ FormatdateTime('d/mm/yyyy',Date);
label8.Caption:='Time  '+ FormatDateTime('hh:nn:ss',Time);
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
close;
end;

end.




 d. Program Interface Grafik Delphi 7 ( Dua Grafik isi 2 Data )


 unit TA;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls,strutils, TeeProcs, TeEngine, Chart, CPort, mmsystem,
  Series, Buttons;

type
  TForm1 = class(TForm)
    ComPort1: TComPort;
    GroupBox1: TGroupBox;
    Edit1: TEdit;
    Edit2: TEdit;
    Chart1: TChart;
    Shape1: TShape;
    Shape2: TShape;
    GroupBox2: TGroupBox;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Memo1: TMemo;
    Label1: TLabel;
    Label2: TLabel;
    Timer1: TTimer;
    Label7: TLabel;
    Label8: TLabel;
    GroupBox4: TGroupBox;
    Edit3: TEdit;
    Edit4: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    BitBtn1: TBitBtn;
    Chart2: TChart;
    Series1: TFastLineSeries;
    Series2: TFastLineSeries;
    procedure ComPort1RxChar(Sender: TObject; Count: Integer);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Memo1Change(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
var buff:string;
begin
comport1.ReadStr(buff,count);
memo1.Text:=memo1.Text+buff;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
comport1.ShowSetupDialog;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if button2.Caption='Connect' then
   begin
   button2.Caption:='Disconnect';
   comport1.Open;
   end
else if button2.Caption='Disconnect' then
   begin
   button2.Caption:='Connect';
   comport1.Close;
   end;

end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Edit1.Color:=clwhite;
Edit2.Color:=clwhite;
edit1.text:='';
edit2.text:='';
memo1.Text:='';
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
close;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ThousandSeparator:=',';
DecimalSeparator:='.';
end;

procedure TForm1.Memo1Change(Sender: TObject);
var ruang,kulit:double;
    rin,kin:string;
    jumlahdata:integer;

begin
  shape1.Brush.Color:=clgreen;
  shape2.Brush.Color:=clgreen;
  jumlahdata:=chart1.GetMaxValuesCount;
  form1.Caption:=inttostr(jumlahdata);
  if jumlahdata =10000 then
     begin
     Series1.Clear;
     Series2.Clear;
     end;

  rin:=memo1.Lines[memo1.Lines.count-2];
  kin:=memo1.Lines[memo1.Lines.count-1];

  if rightstr(rin,1)= 'a' then
     begin
     edit1.Text:=leftstr(rin,length(rin)-1);
     ruang:=strtofloat(edit1.text);
     Series1.Add(ruang,'',clred) ;
     shape1.Brush.Color:=clred;
     end;
  if rightstr(kin,1)='b' then
     begin
     edit2.Text:=leftstr(kin,length(kin)-1);
     kulit:=strtofloat(edit2.text);
     Series2.Add(kulit,'',clgreen) ;
     shape2.Brush.Color:=clred;
     end;


end;


procedure TForm1.Edit1Change(Sender: TObject);
var ruang:double;
begin
ruang:=strtofloat(edit1.text);
if ruang<30 then
   begin
   edit1.Color:=clblue;
   edit3.text:= 'Lembab';
   end;
if ruang>31 then
   begin
   edit1.Color:=clred;
   edit3.text:= 'Kering';
   end;
if (ruang>=31) and (ruang<=30) then
   begin
   edit1.Color:=clwhite;
   edit3.text:= 'Normal';
   end;
end;


procedure TForm1.Edit2Change(Sender: TObject);
var kulit:double;
begin
kulit:=strtofloat(edit2.text);
if kulit<60 then
   begin
   edit2.Color:=clblue;
   edit4.text:= 'Kering';
   end;
if kulit>61 then
   begin
   edit2.Color:=clred;
   edit4.text:= 'Lembab';
   end;
if (kulit>=61) and (kulit<=60) then
   begin
   edit2.Color:=clwhite;
   edit4.text:= 'Normal';
   end;
end;



procedure TForm1.Timer1Timer(Sender: TObject);
begin
label7.Caption:='Date  '+ FormatdateTime('d/mm/yyyy',Date);
label8.Caption:='Time  '+ FormatDateTime('hh:nn:ss',Time);
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
close;
end;

end.



e. Cara Penggunaan Alat

            Untuk menggunakan alat ini yaitu pertama download / upload program Arduino terlebih dahulu, kemudian pastikan Arduino mengirimkan data secara serial dengan menggunakan fitur serial monitor pada software Arduino. jika semua berhasil maka buat interface Delphi 7 dan pastikan tidak error saat RUN. langkah terakhir yaitu lihat COM port pada Arduino lalu pilih COM tersebut. jika berhasil maka tampilan grafik akan muncul pada interface Delphi 7. jika mau download program diatas bisa melalui link dibawah ini.











No comments:

Post a Comment