3

I am able to control my monitor and most other applications by giving serial input to python through arduino. Arduino code:

void setup()
 {

  pinMode(2, INPUT); 
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  Serial.begin(9600);

}

void loop()
{
  if(digitalRead(2) == HIGH)
  {
    delay(5);
  }
  else 
  {
    Serial.println("up");
  }
  if(digitalRead(3) == HIGH)
  {
    delay(5);
  }
  else 
  {
    Serial.println("down");
  }
  if(digitalRead(4) == HIGH)
  {
    delay(5);
  }
  else 
  {
    Serial.println("left");
  }
  if(digitalRead(5) == HIGH)
  {
    delay(5);
  }
  else 
  {
    Serial.println("right");
  }
  if(digitalRead(6)== HIGH)
  {
    delay(5);
  } 
  else
  {
    Serial.println("space");//orange
  }
  if(digitalRead(7)==HIGH)
  {
     delay(5);
  }   
  else
  {  
    Serial.println("nitro");//brown
  }  
 }

Python code:

import serial
from pymouse import PyMouse
from pykeyboard import PyKeyboard

k = PyKeyboard()

ser = serial.Serial('COM7', 9600)


#sp.write("AT\r\n".encode('ascii'))

while True:
    p=ser.readline()
    print p
    if ('up' in p):
        k = PyKeyboard()

        k.tap_key(k.up_key)

    if ('down' in p):
        k = PyKeyboard()

        k.tap_key(k.down_key)


    if ('right' in p):
        k = PyKeyboard()

        k.tap_key(k.right_key)


    if ('left' in p):
        k = PyKeyboard()

        k.tap_key(k.left_key)

    if ('space' in p):
        k = PyKeyboard()

        k.tap_key(k.space_key)

    if ('nitro' in p):
        k = PyKeyboard()

        k.tap_key(k.enter_key)    

My problem is that..I am not able to play games such as NFS and other similar games using these controls.. What should i do??

1

2 Answers 2

2

Your examples of sending input to the Arduino IDE monitor and to Python involve the Arduino sending data via "Serial over USB" convention. However this is not seen by the operating system (Windows, MacOS) as legitimate keyboard/mouse/joystick input.

There are separate USB conventions (device types) for keyboard/mouse/joystick. Only Arduinos where the USB interface is programmable are able to present those conventions, and some more easily than others, and require understanding some USB client libraries. The link provided by Chris Stratton has some useful background.

A particularly easy way to get into this is with the PJRC Teensy, https://www.pjrc.com/teensy/, which comes with libraries and examples that let you do this out-of-the-box. See the "Teensyduino" section of their website for USB Keyboard, Mouse, Joystick and so on. These pages are useful background even if not using Teensy. It has attracted a small following of users interested in this area. Plus the various Teensies are conveniently small boards that fits nicely in some user-interface device you might want to build.

1

I'm later, but, I think you should redesign your python and arduino code, to make messages like "start up", "end up", and if message is "start *", hold that key, if "end *", release that key. This should work as intended. I will pin python and arduino code later here. (Also sorry for my english, i'm russian)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.