Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana membuat sebuah alat yang bisa digunakan untuk memonitor kemiringan dataran menggunakan wireless modul HC-12. alat ini menggunakan sensor accelero IMU yaitu GY-521 atau MPU6050 yang mana terdapat tidak hanya accelero saja namun ada gyro dan lainnya. untuk lebih jelasnya berikut adalah komponen dan programnya.
a. Arduino Uno
b. Modul Wireless HC-12
c. Sensor IMU accelero - gyro GY-521 / MPU6050
d. Program Arduino IDE
#include<Wire.h>
const int MPU_addr=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
void setup(){
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.begin(9600); // 9600
}
void loop(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
//AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
//Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
//GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
//GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
//GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
Serial.print(AcX); Serial.println('a');
Serial.print(AcY); Serial.println('b');
//Serial.print("AcX = "); Serial.print(AcX);
//Serial.print(" | AcY = "); Serial.print(AcY);
//Serial.print(" | AcZ = "); Serial.print(AcZ);
//Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53); //equation for temperature in degrees C from datasheet
//Serial.print(" | GyX = "); Serial.print(GyX);
//Serial.print(" | GyY = "); Serial.print(GyY);
//Serial.print(" | GyZ = "); Serial.println(GyZ);
delay(1000);
}
const int MPU_addr=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
void setup(){
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.begin(9600); // 9600
}
void loop(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
//AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
//Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
//GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
//GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
//GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
Serial.print(AcX); Serial.println('a');
Serial.print(AcY); Serial.println('b');
//Serial.print("AcX = "); Serial.print(AcX);
//Serial.print(" | AcY = "); Serial.print(AcY);
//Serial.print(" | AcZ = "); Serial.print(AcZ);
//Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53); //equation for temperature in degrees C from datasheet
//Serial.print(" | GyX = "); Serial.print(GyX);
//Serial.print(" | GyY = "); Serial.print(GyY);
//Serial.print(" | GyZ = "); Serial.println(GyZ);
delay(1000);
}
e. 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;
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;
Label5: TLabel;
Label6: TLabel;
Timer2: TTimer;
Timer3: 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);
procedure Timer3Timer(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
Timer2.Enabled := True;
Timer3.Enabled := True;
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;
procedure TForm1.Timer2Timer(Sender: TObject);
var x:double;
y:double;
begin
x:=strtofloat(edit1.text);
y:=strtofloat(edit2.text);
if(x>y) then
begin
Label6.Caption := 'BAHAYA';
end;
if(x<=y) then
begin
Label6.Caption := 'AMAN';
end;
end;
procedure TForm1.Timer3Timer(Sender: TObject);
var
F: TextFile;
begin
AssignFile(F, 'D:\result2.txt');
Append(F);
Write(F,'SumbuX=',Edit1.Text);
Write(F,' ');
WriteLn(F,'SumbuY=',Edit2.Text);
CloseFile(F);
end;
end.
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;
Label5: TLabel;
Label6: TLabel;
Timer2: TTimer;
Timer3: 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);
procedure Timer3Timer(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
Timer2.Enabled := True;
Timer3.Enabled := True;
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;
procedure TForm1.Timer2Timer(Sender: TObject);
var x:double;
y:double;
begin
x:=strtofloat(edit1.text);
y:=strtofloat(edit2.text);
if(x>y) then
begin
Label6.Caption := 'BAHAYA';
end;
if(x<=y) then
begin
Label6.Caption := 'AMAN';
end;
end;
procedure TForm1.Timer3Timer(Sender: TObject);
var
F: TextFile;
begin
AssignFile(F, 'D:\result2.txt');
Append(F);
Write(F,'SumbuX=',Edit1.Text);
Write(F,' ');
WriteLn(F,'SumbuY=',Edit2.Text);
CloseFile(F);
end;
end.
f. VIDEO HASILNYA
No comments:
Post a Comment