Kali ini saya akan menjelaskan mengenai bagaimana cara membuat software delphi sebagai interface untuk menampilkan data yang dikirimkan oleh Arduino, jadi aplikasi ini hanyalah untuk menampilkan data saja berupa nilai dan grafik real time, selain itu data yang masuk ke grafik akan disimpan ke database Ms. Access dengan time sampling tiap 1 detik. yang perlu diperhatikan disini yaitu bagaimana cara mengkonfigurasikan Ms. Access dan Delphi agar terkoneksi dengan baik dan juga bagaimana install comport agar data dari Arduino bisa tampil di Delphi 7. untuk program dan skemanya dapat dilihat dipenjelasan dibawah ini.
a. Arduino UNO
b. Program Arduino IDE
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:
int sensorValue = analogRead(A0);
int sensorValue1 = analogRead(A1);
// print out the value you read:
Serial.print(sensorValue),Serial.println ('a');
Serial.print(sensorValue1),Serial.println ('b');
delay(100); // delay in between reads for stability
}
// 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:
int sensorValue = analogRead(A0);
int sensorValue1 = analogRead(A1);
// print out the value you read:
Serial.print(sensorValue),Serial.println ('a');
Serial.print(sensorValue1),Serial.println ('b');
delay(100); // delay in between reads for stability
}
c. Program Delphi 7
unit TA;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls,strutils, TeeProcs, TeEngine, Chart, CPort, mmsystem,
Series, Buttons, DB, ADODB, Grids, DBGrids, DBCtrls;
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;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
ADOConnection1: TADOConnection;
ADOTable1: TADOTable;
Timer2: TTimer;
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);
procedure Timer2Timer(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;
Timer2.Enabled := True;
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:=strtoint(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:=strtoint(edit2.text);
Series2.Add(kulit,'',clgreen) ;
shape2.Brush.Color:=clred;
end;
end;
procedure TForm1.Edit1Change(Sender: TObject);
var ruang:double;
begin
ruang:=strtoint(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:=strtoint(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;
procedure TForm1.Timer2Timer(Sender: TObject);
begin
adotable1.Append;
adotable1.Fields.FieldByName ('Data').Text:=edit1.Text;
adotable1.Fields.FieldByName ('Duty').Text:=edit2.Text;
adotable1.Post;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls,strutils, TeeProcs, TeEngine, Chart, CPort, mmsystem,
Series, Buttons, DB, ADODB, Grids, DBGrids, DBCtrls;
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;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
ADOConnection1: TADOConnection;
ADOTable1: TADOTable;
Timer2: TTimer;
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);
procedure Timer2Timer(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;
Timer2.Enabled := True;
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:=strtoint(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:=strtoint(edit2.text);
Series2.Add(kulit,'',clgreen) ;
shape2.Brush.Color:=clred;
end;
end;
procedure TForm1.Edit1Change(Sender: TObject);
var ruang:double;
begin
ruang:=strtoint(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:=strtoint(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;
procedure TForm1.Timer2Timer(Sender: TObject);
begin
adotable1.Append;
adotable1.Fields.FieldByName ('Data').Text:=edit1.Text;
adotable1.Fields.FieldByName ('Duty').Text:=edit2.Text;
adotable1.Post;
end;
end.
d. Database Ms. Access
e. Cara Penggunaan Alat
Untuk menggunakan alat ini pertama yaitu aktifkan program Arduino dan Upload program diatas. Buat Interface delphi 7 seperti diatas dan masukan programnya, jangan lupa install comport yang sesuai agar bisa berkomunikasi dengan Arduino. jika berhasil maka akan tampil data yang diinginkan ke interface Delphi 7 dan juga data akan tersimpan rapi dalam database.
No comments:
Post a Comment