Hola a todos. Soy prácticamente nuevo en lo de Arduino (3 días). Recientemente he estado intentando averiguar, cómo poder hacer una comunicación serial con un Joystick (http://www.elecfreaks.com/1999.html), hacia VB.NET. Pero desafortunadamente, he estado presentando algunos problemas, y realmente comienzo a frustrarme. A continuación, he puesto mis códigos, por favor, obsérvenlos y si tienen alguna idea de qué es lo que pasa o cómo repararlo, les estaré muy agradecido. Gracias.
//ARDUINO UNO <> INPUT JOYSTICK (5 ENTRADAS DIGITALES = 5 BOTONES Y 2 ENTRADAS ANALÓGICAS= EJES X y Y) <> ENVÍO DE LOS DATOS HACIA 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");
/*Serial.flush();*/
}
else if (digitalRead(PIN_BUTTON_LEFT) == LOW){
Serial.println("20");
/*Serial.flush();*/
}
else if (digitalRead(PIN_BUTTON_UP) == LOW){
Serial.println("30");
/*Serial.flush();*/
}
else if (digitalRead(PIN_BUTTON_DOWN) == LOW){
Serial.println("40");
/*Serial.flush();*/
}
else if (digitalRead(PIN_BUTTON_SELECT) == LOW){
Serial.println("50");
/*Serial.flush();*/
}
}
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);}
/*Serial.flush();*/
}
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);
}
El principal problema, es que aparentemente hay bytes corruptos, realmente no lo sé, sólo que cuando ejecuto el programa con las entradas digitales (o sea, los botones del joystick), mi form en VB.NET se congela, pero cuando ejecuto el programa sin las lecturas digitales (los botones del joystick) y únicamente las lecturas analógicas (los ejes del joystick X y Y) parece funcionar muy bien.
Este es mi código en VB.NET:
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() 'Si se elimina esta subrutina, observarán que funciona el programa al parecer bien, pero con esta subrutina o función se congela.
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 'Esta sentencia es solo para estabilizar los valores fluctuantes,
data = 50 y colocar el scroll justo a la mitad.
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 'Esta sentencia es solo para estabilizar los valores fluctuantes,
data = 50 y colocar el scroll justo a la mitad.
End If
VScrollBar1.Value = data
TextBox1.Text = data
End If
End Sub
End Class
Y finalmente este a continuación, es el formulario que hice en VB.NET, los scrollbars dependen de los ejes del joystick X y Y, y los 5 textbox en su propiedad de texto dependen de los botones del joystick presionados. El textbox que está al centro, y sólo un texto donde se muestra el valor entrante del puerto serial de cada valor que entra. Y el botón de "Start", es sólo para comenzar la comunicación serial, no creo que tenga una real función.