Click here to Skip to main content
15,886,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Friends, I am trying to make an SNES controller that works via bluetooth on my laptop, with the Serial Ports. My laptop has bluetooth native.The Arduino microcontroller with the help of a bluetooth module, sends chars to my program made in Visual Basic. The program in Visual Basic, transforms these chars in keyboard characters.The problem is the sensitivity of the keys. When I configure the keyboard as a controller in any emulator of SNES, the keys do not work as a conventional keyboard keys. For example, if the controller sends the char "D" to the program in Visual Basic and, "D" means "go right" on the emulator, the "go right" does not work as if I pressed "D" on the real keyboard. I will leave the code both the Arduino, as the program developed in Visual Basic. Friends, please, how can I make the sensitivity of the keyboard functions as a real keyboard? can you help me, please ? thank you all for your attention

Arduino Code
VB
 #include <SoftwareSerial.h>
SoftwareSerial BTMasterHC05(2, 3); // RX do BT | TX do BT
 char statusbutton = 0;
// Controller buttons
// (based on button-to-clock pulse assignment)
#define SNES_B        32768  // 1000000000000000
#define SNES_Y        16384  // 0100000000000000
#define SNES_SELECT   8192   // 0010000000000000
#define SNES_START    4096   // 0001000000000000
#define SNES_UP       2048   // 0000100000000000
#define SNES_DOWN     1024   // 0000010000000000
#define SNES_LEFT      512   // 0000001000000000
#define SNES_RIGHT     256   // 0000000100000000
#define SNES_A         128   // 0000000010000000
#define SNES_X          64   // 0000000001000000
#define SNES_L          32   // 0000000000100000
#define SNES_R          16   // 0000000000010000
 
// Arduino pins vs. SNES controller pins
// (default is latch 2, clock 3, data 4)
int LatchPin  = 5; // Latch
int ClockPin  = 6; // Clock
int DataPin   = 7; // Serial Data
 
// Current controller data
unsigned int ControllerData = 0;
 
// Setup the controller and serial output
void setup() {
  Serial.begin(57600);
  BTMasterHC05.begin(9600);
  pinMode(LatchPin,OUTPUT);
  pinMode(ClockPin,OUTPUT);
  pinMode(DataPin,INPUT);
 
  digitalWrite(LatchPin,HIGH);
  digitalWrite(ClockPin,HIGH);
}
 
// Read controller
void controllerRead() {
  // Reset controller states and data
  ControllerData = 0;
  digitalWrite(LatchPin,LOW);
  digitalWrite(ClockPin,HIGH);
 
  // Controller needs to latch the state of all buttons
  digitalWrite(LatchPin,HIGH);
  delayMicroseconds(12);
  digitalWrite(LatchPin,LOW);
  delayMicroseconds(6);
 
  // Read controller data (initial reading)
  ControllerData = digitalRead(DataPin);
 
  // Send 16 clock pulses, one for each button. 
  for (int i = 0; i < 16; i ++) {
	digitalWrite(ClockPin,LOW);
	delayMicroseconds(6);
	ControllerData = ControllerData << 1;
	ControllerData = ControllerData + digitalRead(DataPin) ;
	delayMicroseconds(6);
	digitalWrite(ClockPin,HIGH);
  }
 
  // Do a NOT, so '1' will be pressed buttons and '0' to the rest
  ControllerData = ~ControllerData;
}
 
// Program code
void loop() {
  // Read controller data
  controllerRead();
  
    while(BTMasterHC05.available() > 0){
      statusbutton =(char) BTMasterHC05.read();
      //imprime na Serial caso receba caracteres
      Serial.println(statusbutton);
    }
     
 
  //Mantem a leitura do HC-06 e envia para Serial do Arduino
  if (BTMasterHC05.available()){
    Serial.write( BTMasterHC05.read());
 }
 
  //Mantém a Leitura no Arduino e Envia para o HC-06
  if (Serial.available()){
    BTMasterHC05.write(Serial.read());
  }
 
  if (ControllerData != 0) {
	Serial.print("Pressed:");
	if (ControllerData & SNES_B) {
	  BTMasterHC05.write('J');
	}
	if (ControllerData & SNES_Y) {
	  BTMasterHC05.write('U');
	}
	if (ControllerData & SNES_SELECT) {
	BTMasterHC05.write('2');
	}
	if (ControllerData & SNES_START) {
	  BTMasterHC05.write('1');
	}
	if (ControllerData & SNES_UP) {
	  BTMasterHC05.write('W');
	}
	if (ControllerData & SNES_DOWN) {
	  BTMasterHC05.write('S');
	}
	if (ControllerData & SNES_LEFT) {
	 BTMasterHC05.write('A');
	}
	if (ControllerData & SNES_RIGHT) {
	  BTMasterHC05.write('D');
	}
	if (ControllerData & SNES_A) {
	 BTMasterHC05.write('K');
	}
	if (ControllerData & SNES_X) {
	  BTMasterHC05.write('I');
	}
	if (ControllerData & SNES_L) {
	  BTMasterHC05.write('L');
	}
	if (ControllerData & SNES_R) {
	BTMasterHC05.write('R');
	}
  }
  
  delay(16);
}


Sorry about the comments in Portuguese, but, i believe that you can understand the code.

Thank you all.

What I have tried:

And Here, the Visual Basic Program Code.

VB
Imports System
Imports System.IO.Ports
Imports System.Threading

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim sports As String() = System.IO.Ports.SerialPort.GetPortNames

        If sports Is Nothing Then
            MsgBox("Não foram encontradas portas seriais nesse computador")
        End If

        For i = 0 To UBound(sports)
            ComboBox1.Items.Add(sports(i))
            ComboBox1.SelectedIndex = 0
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            SerialPort1.PortName = ComboBox1.SelectedItem
            SerialPort1.BaudRate = 57600
            SerialPort1.DataBits = 8
            SerialPort1.Parity = IO.Ports.Parity.None
            SerialPort1.StopBits = IO.Ports.StopBits.One

            SerialPort1.Open()
            MsgBox("Conectou !")
            lbl_1.Text = "divirta-se ! ;-)"

        Catch ex As Exception
            SerialPort1.Close()

            MsgBox("Erro ao conectar porta Serial")
        End Try
    End Sub

       Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim strongo As String = SerialPort1.ReadExisting()

        If strongo = "W" Then
            SendKeys.Send("w")
        Else
        End If

        If strongo = "S" Then
            SendKeys.Send("s")
        End If

        If strongo = "D" Then

            SendKeys.Send("d")
        End If

        If strongo = "A" Then
            SendKeys.Send("a")

        End If

        If strongo = "1" Then
            SendKeys.Send("p")
        End If

        If strongo = "K" Then
            SendKeys.Send("k")
        End If
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        MsgBox("Esta é mais uma aplicação desenvolvida por Dan Fayal ! Obrigado por usar !")

        SerialPort1.Close()
    End Sub

End Class
Posted
Updated 18-Jul-17 6:33am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900