Go Down

Topic: Auto detect com port (Read 1 time) previous topic - next topic

Klaus_K

#15
Nov 18, 2019, 06:50 pm Last Edit: Nov 18, 2019, 06:55 pm by Klaus_K
OK, how is the game controlled? Does it support joysticks or just mouse and keyboard.

The USB has a class called HID (Human Interface Device). If you make your device behave like a keyboard, mouse or joystick, you do not need to write any driver. Windows has a standard way of doing this.

Could you be so kind and let me know exactly which board you have and would you be ok to get another board if that would make your live easier?

I have used some Teensy boards and just found this. I could check whether I get the example running.

https://www.pjrc.com/teensy/td_joystick.html

There are some Arduino boards with USB Keyboard, Mouse examples

https://www.arduino.cc/reference/en/language/functions/usb/keyboard/
https://www.arduino.cc/reference/en/language/functions/usb/mouse/

If you have the Arduino Uno Rev 3 it might be possible to hack the microcontroller that runs the USB.

https://store.arduino.cc/arduino-uno-rev3

Klaus_K

#16
Nov 19, 2019, 04:35 pm Last Edit: Nov 19, 2019, 04:36 pm by Klaus_K
I just connected a joystick breakout board (basically two potentiomters and a button) to my Teensy 3.2. There where already three buttons on the base board. I modified the joystick basic example to the pins on the board I have. And within 5 minutes I had a fully functioning Windows compatible joystick. No need for any special driver.

This is all the code it needs. This is almost too easy. :)

Code: [Select]
/* Basic USB Joystick Example
   Teensy becomes a USB joystick

   You must select Joystick from the "Tools > USB Type" menu

   This example code is in the public domain.
*/

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(18, INPUT); // Pin has external pull-up
}

void loop() {
  // read analog inputs and set X-Y position
  Joystick.X(analogRead(2));
  Joystick.Y(analogRead(3));

  // read the digital inputs and set the buttons
  Joystick.button(1, digitalRead(2));
  Joystick.button(2, digitalRead(3));
  Joystick.button(3, digitalRead(6));
  Joystick.button(4, digitalRead(18));

  // a brief delay, so this runs 20 times per second
  delay(50);
}

Juniorjrjr

#17
Nov 21, 2019, 03:54 pm Last Edit: Nov 21, 2019, 03:55 pm by Juniorjrjr
Hi! Thanks for the reply, and sorry for the delay. That's because I'm on tests week on my university.

I see your solution, but what I need to do is getting joystick sending data and simulate a key of the keyboard on a game. For example, when I put the joystick forward, the pc understand 'W' key. That's not XInput, but what I need is a key input(DirectInput).

What I'm thinking to do about auto detect port is getting the port's name with C#, save in a file and C program will read this file. That's a solution that I'm thinking to do at this moment.

But another solutions is validate.

Klaus_K

No problem, you can easily send keystrokes as well. Have a look at the modifications.

When my button1 in pressed the joystick will also send keystrokes based on the position of the joystick. I added this to make sure the joystick does not type into my source code. :)

I matched the joystick to the WASD keys. I left a window/dead zone in the middle, so the joystick does not send keys when in the center position.

I do not have games on my laptop, but I send the keys into a text file. :)

Code: [Select]
/* Basic USB Joystick Example
   Teensy becomes a USB joystick

   You must select Joystick from the "Tools > USB Type" menu

   This example code is in the public domain.
*/

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(18, INPUT);
}

void loop() {
  // read analog inputs and set X-Y position
  int joystickXPos = analogRead(2);
  Joystick.X(joystickXPos);

  int joystickYPos = analogRead(3);
  Joystick.Y(joystickYPos);

  // read the digital inputs and set the buttons
  bool button1State = digitalRead(2);
  Joystick.button(1, button1State);
  Joystick.button(2, digitalRead(3));
  Joystick.button(3, digitalRead(6));
  Joystick.button(4, digitalRead(18));


  if( button1State == LOW )
  {
//    Serial.println( joystickXPos );
//    Serial.println( joystickYPos );

    if( joystickYPos > 600 )
    {
      Keyboard.print("w");   
    }
    if( joystickYPos < 400 )
    {
      Keyboard.print("s");   
    }

    if( joystickXPos > 600 )
    {
      Keyboard.print("d");   
    }
    if( joystickXPos < 400 )
    {
      Keyboard.print("a");   
    }
  }

  // a brief delay, so this runs 20 times per second
  delay(50);
}


Good luck with your university tests.

Juniorjrjr

Hi! Thanks man for answer me all time!
I solve this problem using C#, because I don't have atmega8, and I think it's better to don't have atmega8 on this project. And another thing is I don't know if Keyboard.h works in all games, because when I use C, I need to do a DirectInput, and not a normal input to works in all games. If the Keyboard.h  was elaborated with a normal input, then it will not work in game.

Another thing to ask is(I don't know if I need to create another topic or no): How to start an application, for example .exe when I plug in the arduino?

Klaus_K

Its unlikely, you can start a program automatically when the Arduino is plugged into your PC.

You can have a program start automatically when Windows starts. Just have your program run all the time. Windows run lots of services and other stuff in the background.

To avoid using to much processing power make use of Windows sleep function. Just Google "visual studio program sleep c#" and find one that fits what you do.

Paul_KD7HB

Back in the day of Windows NT, there were thousands of documents for developers showing how to all kinds of operating system inquiries and controls. Since the current Windows is descended from NT, I suspect there are similar sources for today's software developers. Have you looked for Windows software developers help?

Paul

Klaus_K

Paul, if you google visual studio something the top result is almost always the microsoft documentation. And the documentation is really good, with explanation, syntax highlighted examples and clean looking pages.

Go Up