Wednesday, February 23, 2011

Interfacing Arduino and PC using VB 6




One of the conveniences that exist on the Arduino is a serial communication function that has been bundled in the Arduino software. With the serial function, then the Arduino can communicate two ways with PC using a program created with Visual Basic 6.
In this project, we will combining the Digital Data and Analog Data. How to receive and transmit Digital and Analog Data  from Arduino to PC and vice versa.

Arduino Conditions:
  1. Digital Input Signal from Push Button
  2. Analog Digital Input Signal from LDR
  3. Digital Output Signal at LED (on-off)
  4. Analog Outpuy Signal at LED (brightness).

PC Conditions with VB 6:

  1. Digital Input Data showing by Command Button Object
  2. Analog Input Data showing by Slider Object
  3. Digital Output Data showing by Shape Object
  4. Analog Output Data showing by Picture Box Object


Procedures:
  1. Connect Arduino to the computer through a USB port.
  2. Check the COM channel used (open Device Manager to find out).
  3. Arrange a series of Input Output Arduino using buttons, LDR and 2 pieces LEDs, where the key to connect with feet D2, A0 LDR connect with your legs, and first and second LEDs in succession to connect with feet D9 and D10.
  4. Perform programming on the Arduino, then Compile and upload the program.
  5. Perform Visual Basic, run the program and complete.
Arduino Source Code:


int kakitombol=2;
int kakiled=9;
int pinled=10;

void setup() {
Serial.begin(9600);
pinMode(kakitombol,INPUT);
pinMode(kakiled,OUTPUT);
pinMode(pinled,OUTPUT);
}

void loop() {  
if(Serial.available())
{
int terimadata=Serial.read();
if (terimadata=='a')
{digitalWrite(kakiled,HIGH);}
else if (terimadata=='b')
{digitalWrite(kakiled,LOW);}
else
{analogWrite(pinled,terimadata);}
}
int statustombol=digitalRead(kakitombol);
if(statustombol==HIGH)
{Serial.print('a');}
else
{Serial.print('b');}
int analogValue = analogRead(0);   
int datamsb=highByte(analogValue);
int datalsb=lowByte(analogValue);
Serial.print(datamsb,BYTE);
Serial.print(datalsb,BYTE);
delay(100);
}

Visual Basic 6 Source Code:


Private Sub drawscale(var As Long)
Picture1.Cls
Picture1.Line (0, 0)-(var, Picture1.ScaleHeight), vb3shadow, BF
End Sub

Private Sub Command1_Click()
Dim statusled As String
If Option1 = True Then
statusled = "b"
Else
statusled = "a"
End If
MSComm1.Output = statusled
End Sub

Private Sub Form_Load()
Slider1.Max = 255
Slider1.Min = 0
Slider1.TickFrequency = 10
Slider1.LargeChange = 10
MSComm1.RThreshold = 3
MSComm1.InputLen = 3
MSComm1.Settings = "9600,n,8,1"
MSComm1.CommPort = 6
MSComm1.PortOpen = True
MSComm1.DTREnable = False
Picture1.ScaleWidth = 1000
Picture1.AutoRedraw = True
End Sub

Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
End Sub

Private Sub Slider1_Change()
MSComm1.Output = Chr$(Slider1.Value)
Label3.Caption = Slider1.Value
End Sub

Private Sub MSComm1_OnComm()
Dim sData As String
Dim highbyte As Long
Dim lowbyte As Long
Dim word As Long
Dim tombol As String
If MSComm1.CommEvent = comEvReceive Then
    sData = MSComm1.Input
    tombol = Mid$(sData, 1, 1)
    highbyte = Asc(Mid$(sData, 2, 1))
    lowbyte = Asc(Mid$(sData, 3, 1))
    word = (highbyte * &H100) Or lowbyte
    Label1.Caption = CStr(word)
    drawscale word
    Label2.Caption = tombol
    If tombol = "a" Then
    Shape1.FillColor = vbYellow
    Else
    Shape1.FillColor = vbRed
    End If
End If
End Sub


Using Objects Informations:
  • 1 Form
  • 1 Mscomm (diperoleh dari Project / Components / Controls / Microsoft Comm Control 6.0)
  • 1 Shape
  • 3 Label
  • 1 Picture Box
  • 1 Slider (diperoleh dari Project / Components / Controls / Microsoft Windows Common Controls 6.0)
  • 2 Option Button
  • 1 Command Button  




Source: http://arduinodanvb.blogspot.com/2010/10/interface-3-menggabungkan-digital-dan.html

No comments:

Post a Comment