Hello everyone. I’m such as new in all this Arduino stuff. Recently I’ve been trying to figure out how to make a serial communication with a Joystick (http://www.elecfreaks.com/1999.html), with VB.NET. But unfortunately, I’ve been experimenting a lot of problems, and I’m really starting to become frustrated. Below I posted my codes, please, look at them and if you have some ideas to fix it, I’ll really appreciate. Thanks.
//ARDUINO UNO <> INPUT JOYSTICK (5 DIGITAL INPUTS = 5 BUTTONS AND 2 ANALOG INPUTS= AXIS X and Y) <> SEND SERIAL DATA TO RECEIVE INTO VB.NET //
const byte PIN_BUTTON_SELECT = 9;
const byte PIN_BUTTON_RIGHT = 5;
const byte PIN_BUTTON_UP = 4;
const byte PIN_BUTTON_DOWN = 6;
const byte PIN_BUTTON_LEFT = 7;
const byte PIN_ANALOG_X = 0;
const byte PIN_ANALOG_Y = 1;
int valueX = 0;
int valueY = 0;
int vbNetIn = 0;
void setup() {
Serial.begin(9600);
pinMode(PIN_BUTTON_RIGHT, INPUT);
digitalWrite(PIN_BUTTON_RIGHT, HIGH);
pinMode(PIN_BUTTON_LEFT, INPUT);
digitalWrite(PIN_BUTTON_LEFT, HIGH);
pinMode(PIN_BUTTON_UP, INPUT);
digitalWrite(PIN_BUTTON_UP, HIGH);
pinMode(PIN_BUTTON_DOWN, INPUT);
digitalWrite(PIN_BUTTON_DOWN, HIGH);
pinMode(PIN_BUTTON_SELECT, INPUT);
digitalWrite(PIN_BUTTON_SELECT, HIGH);
}
int treatValue(int data) {
return (data * 9 / 1024) + 48;
}
void loop() {
case = Serial.read();
if (Serial.available()>0){
switch (vbNetIn){
case 10:
while (Serial.available() == 10){
if (digitalRead(PIN_BUTTON_RIGHT) == LOW){
Serial.println("10");
}
else if (digitalRead(PIN_BUTTON_LEFT) == LOW){
Serial.println("20");
}
else if (digitalRead(PIN_BUTTON_UP) == LOW){
Serial.println("30");
}
else if (digitalRead(PIN_BUTTON_DOWN) == LOW){
Serial.println("40");
}
else if (digitalRead(PIN_BUTTON_SELECT) == LOW){
Serial.println("50");
}
}
break;
case 20:
{
while (Serial.available() == 20){
valueX = analogRead(PIN_ANALOG_X);
valueX = map (valueX, 0, 1023, 0, 100);
String sx = String(valueX);
Serial.println(sx);}
}
break;
case 30:
{
while (Serial.available() == 30){
valueY = analogRead(PIN_ANALOG_Y);
valueY = map (valueY, 0, 1023, 100, 0); /*A put it in reverse, because the vertical scroll in VB.NET is from up to down, and I wanted from the "common" zero axis point.
String sy = String(valueY);
Serial.println(sy);}
}
break;
}
}
delay(30);
}
The main problem, is that apparently there are corrupted bytes, I don’t really know, just when I run it with the digital reads, my form application in VB.NET doesn’t work, but, when I run it without digital reads (buttons) and only the analog reads, it looks like works pretty good.
This one is my VB.NET code:
Imports System
Imports System.IO.Ports
Imports System.Threading
Imports System.ComponentModel
Public Class Form1
Dim s As String
Dim data As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Open()
End Sub
Private Sub start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles start.Click
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
comparison() 'If you quit this sub-rutine, you will see that apparently everything works fine, but whitin don't.
axisX()
axisY()
End Sub
Sub comparison()
SerialPort1.Write(Chr(10))
s = SerialPort1.ReadLine
data = Integer.Parse(s)
Select data
Case 10
TextBox2.Text = "UP"
Case 20
TextBox3.Text = "DOWN"
Case 30
TextBox4.Text = "RIGHT"
Case 40
TextBox5.Text = "LEFT"
Case 50
TextBox6.Text = "SELECT"
Case Else
TextBox3.Text = " "
TextBox5.Text = " "
TextBox2.Text = " "
TextBox4.Text = " "
TextBox6.Text = " "
End Select
End Sub
Sub axisX()
SerialPort1.Write(Chr(20))
Try
s = SerialPort1.ReadLine
data = Integer.Parse(s)
'TextBox1.Text = data
Catch ex As Exception
End Try
If (data >= 0 AndAlso data <= 100)
If (data >= 48 AndAlso data <= 51) Then
data = 50
End If
HScrollBar1.Value = data
TextBox1.Text = data
End If
End Sub
Sub axisY()
SerialPort1.Write(Chr(30))
Try
s = SerialPort1.ReadLine
dato = Integer.Parse(s)
'TextBox1.Text = data
Catch ex As Exception
End Try
If (data >= 0 AndAlso data <= 100) Then
If (data >= 48 AndAlso data <= 51) Then 'This one is jut to stabilize the fluctuant value,
data = 50 'and put it exactly to half.
End If
VScrollBar1.Value = data
TextBox1.Text = data
End If
End Sub
End Class
And finally this one below, is the form in VB.NET, the scrollbars depends from X and Y axis, and the 5 textbox in his property text, depends from pressed buttons in joystick. The textbox in the middle, is just a text to see the value of every single value input. And the button with “Start” is just to make a beggin of the communication, I think doesn’t have a really function.