PROGRAM PADA VB
'Imports System.IO
'Imports System.IO.Ports
'Imports System.Threading
Imports Microsoft.DirectX ' Required for joystick (Remember to add as project referance)
Imports Microsoft.DirectX.DirectInput ' Required for Joystick (Remember to add as project referance)
Public Class frmMain
Dim myPort As Array 'COM Port yang terdeteksi pada sistem akan disimpan disini
Dim re1 As Byte
Dim re2 As Byte
Dim re3 As Byte
Dim re4 As Byte
Dim re5 As Byte
Dim re6 As Byte
Dim re7 As Byte
Dim re8 As Byte
Private WithEvents Joystick As New Joystick ' Needed to incoorporate Joystick.vb as well as its events
#Region "frmMain Events"
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
re1 = 0
re2 = 0
re3 = 0
re4 = 0
re5 = 0
re6 = 0
re7 = 0
re8 = 0
RefreshDeviceList() ' Refresh the Game Controller Device List
'Cek semua com port yang memungkinkan
myPort = IO.Ports.SerialPort.GetPortNames()
ComboBox2.Items.Add(9600)
'Nilai Baud Rate yang bisa digunakan
ComboBox2.Items.Add(19200)
ComboBox2.Items.Add(38400)
ComboBox2.Items.Add(57600)
ComboBox2.Items.Add(115200)
For i = 0 To UBound(myPort)
ComboBox1.Items.Add(myPort(i))
Next
'Atur Com Port pada Port yang pertama terdeteksi
ComboBox1.Text = ComboBox1.Items.Item(0)
'Atur Baud Rate pada Baud yang pertama terdeteksi
ComboBox2.Text = ComboBox2.Items.Item(0)
End Sub
Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Joystick.Dispose() ' Be nice to your PC and dispose of the Joystick Controlls.
Sp1.Close()
End Sub
#End Region
#Region "Select Game Control Device"
Private Sub RefreshDeviceList()
cmbGameControllerList.Items.Clear() ' First Clear all the items in the combobox
If Joystick.GameControllerList.Count > 0 Then ' Determine if there are any devices
For Each GameController As DeviceInstance In Joystick.GameControllerList ' For each device found
Dim strList As String = GameController.InstanceName & _
" | Guid = " & GameController.InstanceGuid.ToString ' Build an identification string of the device Name and Guid
cmbGameControllerList.Items.Add(strList) ' Add each device identification string to the combobox
Next GameController
Else ' If there are no devices let the user know there are no devices on the system
MsgBox("There are no Game Controllers connected to the system, " & _
"connect a Game Controller and relaunch the application, " & _
"refresh the device list")
End If
End Sub
Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
RefreshDeviceList() ' Populate a combobox of all the available Game Controllers.
End Sub
Private Sub cmbGameControllerList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbGameControllerList.SelectedIndexChanged
If cmbGameControllerList.SelectedIndex <> -1 Then ' Has something been selected on the ComboBox?
btnInitialize.Enabled = True ' Something is selected so Enable initialize button
Sp1.PortName = ComboBox1.Text
Sp1.BaudRate = ComboBox2.Text
Sp1.Open()
Else
btnInitialize.Enabled = False ' Nothing is selected so Disable initialize button
Sp1.Close()
End If
End Sub
Private Sub btnInitialize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInitialize.Click
Dim strItem As String = cmbGameControllerList.SelectedItem.ToString ' Read in the selected String
Dim strGuid As String = strItem.Substring(strItem.IndexOf("Guid = ") + 7) ' Get the Guid String
Dim DeviceGuid As Guid = New Guid(strGuid) ' Convert the string into a Device Guid
Dim GameController As Device = New Device(DeviceGuid) ' Now use the Guid to find the device
Joystick.Initialize(Me, GameController) ' Initialise the selected Game Controller
End Sub
#End Region
Private Sub Joystick_Not_Found() Handles Joystick.Joystick_Not_Found
MessageBox.Show("The specified Joystick could not be found", "Unable to create a Joystick device")
End Sub
Private Sub UpdateControllerSpecifications() Handles Joystick.Initialized
' As soon as the Joystick has been initialized,
' display all of the Joystick Specifications/Parameters.
lblNumAxis.Text = "Number of Axis = " & Joystick.NumberOfAxis
lblNumPOVs.Text = "Number of POV's = " & Joystick.NumberOfPointOfViews
lblNumButtons.Text = "Number of Buttons = " & Joystick.NumberOfButtons
lblHasFF.Text = "Has Force Feedback = " & Joystick.HasFeedback.ToString
End Sub
#Region "Joystick Axis Events"
Private Sub S_Axis_Change(ByVal S) Handles Joystick.S_Axis_Change
' Display the Axis reading on a ProgressBar
' The reading will be -1000 to +1000
' ProgressBar minimum is 0, maximum is 2000
pbr_S_Axis.Value = 1000 - S
End Sub
Private Sub T_Axis_Change(ByVal T) Handles Joystick.T_Axis_Change
' Display the Axis reading on a ProgressBar
' The reading will be -1000 to +1000
' ProgressBar minimum is 0, maximum is 2000
pbr_T_Axis.Value = 1000 - T
Label2.Text = T
're1 = 0
If pbr_T_Axis.Value > 500 And pbr_T_Axis.Value < 1000 Then
Sp1.WriteLine("a")
ElseIf pbr_T_Axis.Value > 1500 Then
Sp1.WriteLine("B")
ElseIf pbr_T_Axis.Value < 500 And pbr_T_Axis.Value > 0 Then
Sp1.WriteLine("A")
ElseIf pbr_T_Axis.Value < 1500 And pbr_T_Axis.Value > 1000 Then
Sp1.WriteLine("b")
End If
End Sub
Private Sub U_Axis_Change(ByVal U) Handles Joystick.U_Axis_Change
' Display the Axis reading on a ProgressBar
' The reading will be -1000 to +1000
' ProgressBar minimum is 0, maximum is 2000
pbr_U_Axis.Value = 1000 - U
End Sub
Private Sub V_Axis_Change(ByVal V) Handles Joystick.V_Axis_Change
' Display the Axis reading on a ProgressBar
' The reading will be -1000 to +1000
' ProgressBar minimum is 0, maximum is 2000
pbr_V_Axis.Value = 1000 - V
End Sub
Private Sub W_Axis_Change(ByVal W) Handles Joystick.W_Axis_Change
' Display the Axis reading on a ProgressBar
' The reading will be -1000 to +1000
' ProgressBar minimum is 0, maximum is 2000
pbr_W_Axis.Value = 1000 - W
Label1.Text = W
If pbr_W_Axis.Value > 500 And pbr_W_Axis.Value < 1000 Then
Sp1.WriteLine("c")
ElseIf pbr_W_Axis.Value < 500 And pbr_W_Axis.Value > 0 Then
Sp1.WriteLine("C")
ElseIf pbr_W_Axis.Value > 1500 Then
Sp1.WriteLine("D")
ElseIf pbr_W_Axis.Value < 1500 And pbr_W_Axis.Value > 1000 Then
Sp1.WriteLine("d")
End If
End Sub
Private Sub X_Axis_Change(ByVal X) Handles Joystick.X_Axis_Change
' Display the Axis reading on a ProgressBar
' The reading will be -1000 to +1000
' ProgressBar minimum is 0, maximum is 2000
pbr_X_Axis.Value = 1000 - X
Label4.Text = pbr_X_Axis.Value
If pbr_X_Axis.Value > 500 And pbr_X_Axis.Value < 1000 Then
Sp1.WriteLine("e")
ElseIf pbr_X_Axis.Value < 500 And pbr_X_Axis.Value > 0 Then
Sp1.WriteLine("E")
ElseIf pbr_X_Axis.Value > 1500 Then
Sp1.WriteLine("F")
ElseIf pbr_X_Axis.Value < 1500 And pbr_X_Axis.Value > 1000 Then
Sp1.WriteLine("f")
End If
End Sub
Private Sub Y_Axis_Change(ByVal Y) Handles Joystick.Y_Axis_Change
' Display the Axis reading on a ProgressBar
' The reading will be -1000 to +1000
' ProgressBar minimum is 0, maximum is 2000
pbr_Y_Axis.Value = 1000 - Y
Label3.Text = Y
If pbr_Y_Axis.Value > 500 And pbr_Y_Axis.Value < 1000 Then
Sp1.WriteLine("g")
ElseIf pbr_Y_Axis.Value < 500 And pbr_Y_Axis.Value > 0 Then
Sp1.WriteLine("G")
ElseIf pbr_Y_Axis.Value > 1500 Then
Sp1.WriteLine("H")
ElseIf pbr_Y_Axis.Value < 1500 And pbr_Y_Axis.Value > 1000 Then
Sp1.WriteLine("h")
End If
End Sub
Private Sub Z_Axis_Change(ByVal Z) Handles Joystick.Z_Axis_Change
' Display the Axis reading on a ProgressBar
' The reading will be -1000 to +1000
' ProgressBar minimum is 0, maximum is 2000
pbr_Z_Axis.Value = 1000 - Z
End Sub
#End Region
#Region "Joystick Point Of View Events"
Private Sub POV(ByVal POV As Joystick.POV_Position) Handles Joystick.POV_0
Select Case POV ' The POV position values have been enumerated.
Case Joystick.POV_Position.Top
rbnTop.Checked = True
rbnTopLeft.Checked = False
rbnLeft.Checked = False
rbnBottomLeft.Checked = False
rbnBottom.Checked = False
rbnBottomRight.Checked = False
rbnRight.Checked = False
rbnTopRight.Checked = False
Case Joystick.POV_Position.TopLeft
rbnTop.Checked = False
rbnTopLeft.Checked = True
rbnLeft.Checked = False
rbnBottomLeft.Checked = False
rbnBottom.Checked = False
rbnBottomRight.Checked = False
rbnRight.Checked = False
rbnTopRight.Checked = False
Case Joystick.POV_Position.Left
rbnTop.Checked = False
rbnTopLeft.Checked = False
rbnLeft.Checked = True
rbnBottomLeft.Checked = False
rbnBottom.Checked = False
rbnBottomRight.Checked = False
rbnRight.Checked = False
rbnTopRight.Checked = False
Case Joystick.POV_Position.BottomLeft
rbnTop.Checked = False
rbnTopLeft.Checked = False
rbnLeft.Checked = False
rbnBottomLeft.Checked = True
rbnBottom.Checked = False
rbnBottomRight.Checked = False
rbnRight.Checked = False
rbnTopRight.Checked = False
Case Joystick.POV_Position.Bottom
rbnTop.Checked = False
rbnTopLeft.Checked = False
rbnLeft.Checked = False
rbnBottomLeft.Checked = False
rbnBottom.Checked = True
rbnBottomRight.Checked = False
rbnRight.Checked = False
rbnTopRight.Checked = False
Case Joystick.POV_Position.BottomRight
rbnTop.Checked = False
rbnTopLeft.Checked = False
rbnLeft.Checked = False
rbnBottomLeft.Checked = False
rbnBottom.Checked = False
rbnBottomRight.Checked = True
rbnRight.Checked = False
rbnTopRight.Checked = False
Case Joystick.POV_Position.Right
rbnTop.Checked = False
rbnTopLeft.Checked = False
rbnLeft.Checked = False
rbnBottomLeft.Checked = False
rbnBottom.Checked = False
rbnBottomRight.Checked = False
rbnRight.Checked = True
rbnTopRight.Checked = False
Case Joystick.POV_Position.TopRight
rbnTop.Checked = False
rbnTopLeft.Checked = False
rbnLeft.Checked = False
rbnBottomLeft.Checked = False
rbnBottom.Checked = False
rbnBottomRight.Checked = False
rbnRight.Checked = False
rbnTopRight.Checked = True
Case Joystick.POV_Position.Neutral
rbnTop.Checked = False
rbnTopLeft.Checked = False
rbnLeft.Checked = False
rbnBottomLeft.Checked = False
rbnBottom.Checked = False
rbnBottomRight.Checked = False
rbnRight.Checked = False
rbnTopRight.Checked = False
End Select
End Sub
#End Region
#Region "Joystick Button Events"
Private Sub Button_0(ByVal Down As Boolean) Handles Joystick.Button_0
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn0.Checked = Down
End Sub
Private Sub Button_1(ByVal Down As Boolean) Handles Joystick.Button_1
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn1.Checked = Down
End Sub
Private Sub Button_2(ByVal Down As Boolean) Handles Joystick.Button_2
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn2.Checked = Down
End Sub
Private Sub Button_3(ByVal Down As Boolean) Handles Joystick.Button_3
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn3.Checked = Down
End Sub
Private Sub Button_4(ByVal Down As Boolean) Handles Joystick.Button_4
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn4.Checked = Down
End Sub
Private Sub Button_5(ByVal Down As Boolean) Handles Joystick.Button_5
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn5.Checked = Down
End Sub
Private Sub Button_6(ByVal Down As Boolean) Handles Joystick.Button_6
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn6.Checked = Down
End Sub
Private Sub Button_7(ByVal Down As Boolean) Handles Joystick.Button_7
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn7.Checked = Down
End Sub
Private Sub Button_8(ByVal Down As Boolean) Handles Joystick.Button_8
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn8.Checked = Down
End Sub
Private Sub Button_9(ByVal Down As Boolean) Handles Joystick.Button_9
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn9.Checked = Down
End Sub
Private Sub Button_10(ByVal Down As Boolean) Handles Joystick.Button_10
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn10.Checked = Down
End Sub
Private Sub Button_11(ByVal Down As Boolean) Handles Joystick.Button_11
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn11.Checked = Down
End Sub
Private Sub Button_12(ByVal Down As Boolean) Handles Joystick.Button_12
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn12.Checked = Down
End Sub
Private Sub Button_13(ByVal Down As Boolean) Handles Joystick.Button_13
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn13.Checked = Down
End Sub
Private Sub Button_14(ByVal Down As Boolean) Handles Joystick.Button_14
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn14.Checked = Down
End Sub
Private Sub Button_15(ByVal Down As Boolean) Handles Joystick.Button_15
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn15.Checked = Down
End Sub
Private Sub Button_16(ByVal Down As Boolean) Handles Joystick.Button_16
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn16.Checked = Down
End Sub
Private Sub Button_17(ByVal Down As Boolean) Handles Joystick.Button_17
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn17.Checked = Down
End Sub
Private Sub Button_18(ByVal Down As Boolean) Handles Joystick.Button_18
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn18.Checked = Down
End Sub
Private Sub Button_19(ByVal Down As Boolean) Handles Joystick.Button_19
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn19.Checked = Down
End Sub
Private Sub Button_20(ByVal Down As Boolean) Handles Joystick.Button_20
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn20.Checked = Down
End Sub
Private Sub Button_21(ByVal Down As Boolean) Handles Joystick.Button_21
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn21.Checked = Down
End Sub
Private Sub Button_22(ByVal Down As Boolean) Handles Joystick.Button_22
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn22.Checked = Down
End Sub
Private Sub Button_23(ByVal Down As Boolean) Handles Joystick.Button_23
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn3.Checked = Down
End Sub
Private Sub Button_24(ByVal Down As Boolean) Handles Joystick.Button_24
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn4.Checked = Down
End Sub
Private Sub Button_25(ByVal Down As Boolean) Handles Joystick.Button_25
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn5.Checked = Down
End Sub
Private Sub Button_26(ByVal Down As Boolean) Handles Joystick.Button_26
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn6.Checked = Down
End Sub
Private Sub Button_27(ByVal Down As Boolean) Handles Joystick.Button_27
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn27.Checked = Down
End Sub
Private Sub Button_28(ByVal Down As Boolean) Handles Joystick.Button_28
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn28.Checked = Down
End Sub
Private Sub Button_29(ByVal Down As Boolean) Handles Joystick.Button_29
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn29.Checked = Down
End Sub
Private Sub Button_30(ByVal Down As Boolean) Handles Joystick.Button_30
' Display the button state with a RadioButton on State Change
' When Pressed -> Down = True
' When Depressed -> Down = False
rbn30.Checked = Down
End Sub
#End Region
#Region "Force Feedback"
Private Sub ForceFeedbackScroll() Handles hsrMag.Scroll, hsrDur.Scroll
Dim FFBMagnitude As Integer = hsrMag.Value - 5000 ' Accepts force Magnitude of +5000 to -5000
Dim FFBDuration As Integer = hsrDur.Value ' Accepts force Duration of 0 to 10000000 in microseconds
lblFFBMag.Text = "Magnitude = " & FFBMagnitude.ToString("N0")
lblFFBDur.Text = "Duration = " & FFBDuration.ToString("N0") & "μS"
Joystick.SendForce(FFBMagnitude, FFBDuration) ' This is where we send the force
End Sub
#End Region
End Class
DESIGN
PROGRAM PADA ARDUINO
int led1 = 13;
int led2 = 12;
int led3 = 11;
int led4 = 10;
int led5 = 9;
int led6 = 8;
int led7 = 7;
int led8 = 6;
String datamasukan;
void setup(){
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
pinMode(led8, OUTPUT);
Serial.println("ready");
}
void loop(){
if (Serial.available()>0){
datamasukan = Serial.readStringUntil('\n');
if (datamasukan=="A"){
digitalWrite(led1,HIGH);}
if (datamasukan=="a"){
digitalWrite(led1,LOW);};
if (datamasukan=="B"){
digitalWrite(led2,HIGH);}
if (datamasukan=="b"){
digitalWrite(led2,LOW);};
if (datamasukan=="C"){
digitalWrite(led3,HIGH);}
if (datamasukan=="c"){
digitalWrite(led3,LOW);};
if (datamasukan=="D"){
digitalWrite(led4,HIGH);}
if (datamasukan=="d"){
digitalWrite(led4,LOW);};
if (datamasukan=="E"){
digitalWrite(led5,HIGH);}
if (datamasukan=="e"){
digitalWrite(led5,LOW);};
if (datamasukan=="F"){
digitalWrite(led6,HIGH);}
if (datamasukan=="f"){
digitalWrite(led6,LOW);};
if (datamasukan=="G"){
digitalWrite(led7,HIGH);}
if (datamasukan=="g"){
digitalWrite(led7,LOW);};
if (datamasukan=="H"){
digitalWrite(led8,HIGH);}
if (datamasukan=="h"){
digitalWrite(led8,LOW);};
//Serial.print("-");
//Serial.println(datamasukan);
}
}