Pada kesempatan kali ini saya akan menjelaskan mengenai sebuah alat yang berfungsi untuk mengendalikan pompa motor dc dan monitor suhu menggunakan visual basic 6.0 dan Arduino. alat ini menggunakan sensor suhu DS18B20 dan juga sebagai kendali pompa menggunakan relay modul.. interface yang digunakan menggunakan VB 6 dan database Ms. Access. untuk lebih jelasnya berikut adalah program dan komponennya.
a. Arduino Mega
b. Relay Modul
c. LCD Keypad Shield
d. Sensor Suhu DS18B20
e. Driver Motor L298
f. Program Arduino IDE
#include<Wire.h>
#include<LiquidCrystal.h>
#include <OneWire.h>
LiquidCrystal lcd(15, 14, 19, 18, 17, 16);
OneWire ds(8);
int dataadc;
int btset = 724;
int btsave = 483;
int btup = 136;
int btdown = 312;
int btok = 1;
int datain;
int rpmx;
int jam;
int menit;
float detiks;
int detik;
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
int suhu;
int rpm;
int timer;
int heater = 5;
int pompa = 2;
int motor1 = 6;
int motor2 = 7;
int timerku = 0;
void setup(){
lcd.begin(16,2);
lcd.clear();
lcd.noCursor();
pinMode(13,OUTPUT);
Serial.begin(9600);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(heater,OUTPUT);
pinMode(pompa,OUTPUT);
pinMode(motor1,OUTPUT);
pinMode(motor2,OUTPUT);
digitalWrite(heater,HIGH);
digitalWrite(motor1,HIGH);
digitalWrite(motor2,HIGH);
digitalWrite(pompa,HIGH);
}
void loop(){
if (Serial.available() > 0)
{
datain = Serial.parseInt();
if(datain == 2){
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
analogWrite(9,rpmx);
}
if(datain == 3){
timerku = 1;
}
if(datain == 5){
Serial.println(suhu);
Serial.println(rpm);
Serial.println(timer);
}
}
else{
if(timerku == 0){
lcd.setCursor(0,1);
lcd.print("Set=");
lcd.print(suhu);
lcd.print("/");
lcd.print(rpm);
lcd.print("/");
lcd.print(timer);
}
if(timerku == 1){
detiks = detiks - 0.5;
detik = detiks;
if(detik < 1){
detiks = 60;
menit--;
}
lcd.setCursor(0,1);
lcd.print(jam);
lcd.print(":");
lcd.print(menit);
lcd.print(":");
lcd.print(detik);
lcd.print(" ");
if((menit < 0)&&(detik < 1)){
lcd.clear();
selesai();
}
}
if ( !ds.search(addr)) {
//Serial.println("No more addresses.");
//Serial.println();
ds.reset_search();
delay(250);
return;
}
//Serial.print("ROM =");
for( i = 0; i < 8; i++) {
//Serial.write(' ');
//Serial.print(addr[i], HEX);
}
if (OneWire::crc8(addr, 7) != addr[7]) {
//Serial.println("CRC is not valid!");
return;
}
//Serial.println();
//the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
//Serial.println(" Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
case 0x28:
//Serial.println(" Chip = DS18B20");
type_s = 0;
break;
case 0x22:
//Serial.println(" Chip = DS1822");
type_s = 0;
break;
default:
//Serial.println("Device is not a DS18x20 family device.");
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
//Serial.print(" Data = ");
//Serial.print(present, HEX);
//Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
//Serial.print(data[i], HEX);
//Serial.print(" ");
}
//Serial.print(" CRC=");
//Serial.print(OneWire::crc8(data, 8), HEX);
//Serial.println();
// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
//untuk lihat data suhu
//Serial.println(celsius);
dataadc = analogRead(A0);
if((celsius > suhu)&&(suhu > 0)) {
digitalWrite(heater,HIGH);
}
if((celsius < suhu)&&(suhu > 0)) {
digitalWrite(heater,LOW);
}
if(dataadc == btset){
delay(200);
lcd.clear();
suhu = 36;
rpm = 50;
timer = 10;
setting1();
setting2();
setting3();
}
lcd.setCursor(0,0);
lcd.print("Suhu= ");
lcd.print(celsius);
lcd.print(" C ");
}
}
void setting1(){
dataadc = analogRead(A0);
if(dataadc == btup){
delay(200);
suhu++;
}
if(dataadc == btdown){
delay(200);
suhu--;
}
if(suhu < 36){
suhu = 36;
}
if(suhu > 38){
suhu = 38;
}
if(dataadc == btok){
delay(200);
lcd.clear();
return;
}
lcd.setCursor(0,0);
lcd.print("SETTING ");
//lcd.print(dataadc);
//lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Suhu = ");
lcd.print(suhu);
lcd.print(" C ");
//lcd.print(dataadc);
setting1();
}
void setting2(){
dataadc = analogRead(A0);
if(dataadc == btup){
delay(200);
rpm = rpm + 25;
}
if(dataadc == btdown){
delay(200);
rpm = rpm - 25;
}
if(rpm < 50){
rpm = 50;
}
if(rpm > 100){
rpm = 100;
}
if(dataadc == btok){
delay(200);
lcd.clear();
if(rpm == 50){
rpmx = 70;
}
if(rpm == 75){
rpmx = 100;
}
if(rpm == 100){
rpmx = 200;
}
return;
}
lcd.setCursor(0,0);
lcd.print("SETTING ");
lcd.setCursor(0,1);
lcd.print("RPM = ");
lcd.print(rpm);
lcd.print(" ");
setting2();
}
void setting3(){
dataadc = analogRead(A0);
if(dataadc == btup){
delay(200);
timer = timer + 5;
}
if(dataadc == btdown){
delay(200);
timer = timer - 5;
}
if(timer < 5){
timer = 5;
}
if(timer > 60){
timer = 60;
}
if(dataadc == btok){
menit = timer;
delay(200);
lcd.clear();
return;
}
lcd.setCursor(0,0);
lcd.print("SETTING ");
lcd.setCursor(0,1);
lcd.print("TIMER = ");
lcd.print(timer);
lcd.print(" ");
setting3();
}
void selesai(){
rpmx = 0;
lcd.setCursor(0,0);
lcd.print("selesai.. ");
digitalWrite(10,LOW);
digitalWrite(11,LOW);
analogWrite(9,rpmx);
digitalWrite(heater,HIGH);
digitalWrite(motor1,HIGH);
digitalWrite(motor2,HIGH);
if (Serial.available() > 0)
{
datain = Serial.parseInt();
if(datain == 4){
digitalWrite(pompa,LOW);
}
if(datain == 6){
digitalWrite(pompa,HIGH);
}
}
selesai();
}
g. Program VB 6
1. Form1
Option Explicit
Dim arrdata()
Dim TotalBaca As Integer
Dim BMI As Single
Dim Keterangan As String
Const MAKSBACA = 18
Dim Conn1 As ADODB.Connection
Dim Cmd1 As ADODB.Command
Dim Param1 As ADODB.Parameter
Dim Rs1 As ADODB.Recordset
Private Sub berhenti_Click()
start.Enabled = True
berhenti.Enabled = False
TimerBaca.Enabled = False
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Command3_Click()
MSComm2.Output = "2" + Chr(13)
End Sub
Private Sub Command4_Click()
MSComm2.Output = "3" + Chr(13)
End Sub
Private Sub Command5_Click()
MSComm2.Output = "4" + Chr(13)
End Sub
Private Sub Command6_Click()
Dim SQl As String
Call dbConnect
SQl = "INSERT INTO tbsuhu VALUES('" & Text4.Text & "','" & Text5.Text & "','" & Text6.Text & "','" & Text2(0).Text & "','" & Text2(1).Text & "','" & Text2(2).Text & "')"
Dim Rs As Recordset
Set Rs = New ADODB.Recordset
Rs.Open SQl, Conn, adOpenDynamic
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
End Sub
Private Sub Command7_Click()
MSComm2.Output = "5" + Chr(13)
End Sub
Private Sub Command8_Click()
MSComm2.Output = "6" + Chr(13)
End Sub
Private Sub Form_Load()
Dim i As Byte
For i = 1 To 16
ComboCOM.AddItem (i)
Next i
End Sub
Private Sub MSComm2_OnComm()
Dim buffer As String
Dim temp As String
Dim pisah() As String
Dim i As Integer
If MSComm2.CommEvent = comEvReceive Then
buffer = MSComm2.Input
pisah = Split(buffer, Chr$(13))
On Error Resume Next
If buffer <> " " Then
With Text1
.SelStart = Len(.Text)
.SelText = buffer
End With
For i = 0 To 2
If 0 < 3 Then
Text2(i).Text = CStr(pisah(i))
End If
Next i
Else
buffer = " "
End If
End If
End Sub
Private Sub start_Click()
Dim u As Integer
On Error GoTo ada_eror
MSComm2.CommPort = ComboCOM.Text
MSComm2.Settings = "9600,N,8,1"
MSComm2.RThreshold = 28
MSComm2.InputLen = 28
MSComm2.InputMode = comInputModeText
MSComm2.PortOpen = True ' buka port
ReDim arrdata(1 To MAKSBACA + 1)
For u = 1 To MAKSBACA
arrdata(u) = 0
Next
TotalBaca = 0
start.Enabled = False
berhenti.Enabled = True
TimerBaca.Enabled = True
Timer2.Enabled = True
Timer3.Enabled = True
ada_eror:
If ComboCOM.ListIndex < 1 Then
MsgBox "COM berada di Nomor : " & Err.Number & vbCrLf & Err.Description, vbCritical + vbOKOnly, "ERROR"
start.Enabled = True
berhenti.Enabled = True
ComboCOM.Text = "Pilih COM"
End If
End Sub
Private Sub Timer1_Timer()
End Sub
Private Sub Timer2_Timer()
Adodc1.Refresh
Dim ok As Integer
Dim mode As Integer
End Sub
Private Sub Timer3_Timer()
MSComm2_OnComm
End Sub
Private Sub TimerBaca_Timer()
Dim strInput As String
Dim strPotong As String
Dim singleInput As Single
Dim u As Integer
strInput = MSComm2.Input
strPotong = strInput
If TotalBaca >= MAKSBACA Then
TimerBaca.Enabled = False
If MSComm2.PortOpen = True Then MSComm2.PortOpen = False
Call berhenti_Click
End If
End Sub
2. Module
Public Conn As New ADODB.Connection
Public Rs As New ADODB.Recordset
Public SQl As String
Public i As Byte
Public Sub dbConnect()
Set Conn = New ADODB.Connection
Conn.ConnectionString = strConn
Conn.Open
End Sub
Public Function strConn() As String
Set Conn = New ADODB.Connection
Set Rs = New ADODB.Recordset
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Acer\Desktop\VB Interface SErial ARRAY 6\database\dbsuhu.mdb;Persist Security Info=False"
End Function
h. VIDEO HASLNYA
No comments:
Post a Comment