Help with mouse input and output?

David1937:
Okay even though I probably wouldn't do the inline method how do I do that?

  1. Connect the USB mouse to the native USB port on the Due using an on-the-go adaptor (I admit, I've never used one or done this so I am making the assumption that this is the correct method)
  2. Write Arduino code to read the mouse buttons and movement
  3. connect the programming port to the PC and install serial port mouse drivers
  4. Write Arduino code to send serial port mouse buttons and movement obtained from the code in (2)
  5. write yet more code which sits between (2) and (4) which alters the state of the mouse button as appropriate
    You may have to google around for code you like the look of for the serial mouse emulation.
    I would do (1) and (2) then output the mouse controls to the serial monitor in the Arduino IDE, then continue with (3) and (4) when I was happy that the mouse was being read correctly. I would then only do (5) when the Arduino was successfully passing all the mouse control to the PC.

weird_dave:

  1. Connect the USB mouse to the native USB port on the Due using an on-the-go adaptor (I admit, I've never used one or done this so I am making the assumption that this is the correct method)
  2. Write Arduino code to read the mouse buttons and movement
  3. connect the programming port to the PC and install serial port mouse drivers
  4. Write Arduino code to send serial port mouse buttons and movement obtained from the code in (2)
  5. write yet more code which sits between (2) and (4) which alters the state of the mouse button as appropriate
    You may have to google around for code you like the look of for the serial mouse emulation.
    I would do (1) and (2) then output the mouse controls to the serial monitor in the Arduino IDE, then continue with (3) and (4) when I was happy that the mouse was being read correctly. I would then only do (5) when the Arduino was successfully passing all the mouse control to the PC.

I think this method would be good to but I really don't understand how I would code it. I am in college right now and I don't have the time to make something like this so the only way I could use this solution is to pay someone to do that. I understand the serial communication method on the side of the Arduino (as in reading serial from the computer) but I really don't understand how to write it with something like AutoHotkey. Like I said my best option would be to pay for someone to write this code for me because I don't have much time available to research let alone code because of my exams that are coming up and they are more of a priority.

You are way smarter than me on this so when I go to research I usually don't end up with many search results because I don't think I am looking up the right thing.

For the KVM switch thing I found that solution out of luck and didn't really understand why it would work or why it wouldn't work. I thought basically what it does it is it switches back and forth the connection to two PC's which allows two mice and two keyboards to be connected simultaneously. Which is why I thought that it could be a possible solution to my problem because with my theory if I did that then I wouldn't have to code any crazy mice movement translations.

Also what is westftw's ideas? I don't understand them completely but they sound good and I ignored them just because of the fact I don't understand what he is saying.

There is no autoit/autohotkey code to write on the PC for that solution, it uses serial mouse drivers. For debugging you just use the Arduino serial monitor to see what's going on so again, no PC code to write there either.

The other reason a KVM might not work is the same problem of mapping the left mouse button to autofire, how does windows know which left mouse button is correct? It may just hold the mouse button down all the time and not autofire/repeat. But that's irrelevent, KVMs don't split the control between 2 PCs, could you imagine trying to look at the output from 2 PCs on 1 monitor that's switching between the 2 at a fast rate? Not that this would even work because the video output from the 2 PCs isn't synced, so the monitor will take some time to lock on to the signal when it switches.
From the product description:
"Users can switch between computers using a manual pushbutton switch located on a wired remote that can be placed on your desk for convenience."

westftw's ideas both solve the usbhost/client problem by making either the input or the output a ps2 mouse connection. this only works if you have a ps2 mouse or a ps2 connection on the PC. You also need an appropriate ps2 connector, either to plug the mouse into or to plug into the PC.

I think that I finally understand your previous post, but just correct me if I am wrong.

So basically what I was thinking of coding it is basically using many if statements and put them in the void loop ()?

I am not going to use the exact code for lack of time.

If (get.Button(left)) //will have to make this one more complex if I was thinking of making the clicker run while I had this pressed, but for now I am just wondering the code for the mouse emulation.
{
Mouse.press(left))
}

while (get.Button(right)) //emulate the right click and other mouse buttons to the computer with this code???
{
Mouse.press(right)
}
if (!get.Button(right))
{
Mouse.release(right)
}

This is where I get confused, couldn't I just used getXchange() and getYChange() and just make those values be set as variables where a few lines later just does mouse.move()?

I think that I am missing a big portion of this but with your good explaining I am starting to understand it more.
If I were to pay someone to make this code for me what is a reliable place to go and what would something like this cost to make?

There are some issues. Firstly, I went for a google and came back without finding any serial mouse emulation. I did find PS2 emulation:
http://playground.arduino.cc/ComponentLib/Ps2mouse
but this does require wiring up a PS2 connection with the Arduino, I'd find an old PS2 mouse and "borrow" the cable. The emulation code is at the bottom of that page.

The while loop you have there will block mouse movement while the right button is pressed.

The mouse.press() commands are for emulating mouse movements via the native USB port, but this is where your USB mouse will be connected, so there will be a different command depending on the emulation solution (serial or PS2). Same for mouse.move().

weird_dave:
There are some issues. Firstly, I went for a google and came back without finding any serial mouse emulation. I did find PS2 emulation:
Arduino Playground - HomePage
but this does require wiring up a PS2 connection with the Arduino, I'd find an old PS2 mouse and "borrow" the cable. The emulation code is at the bottom of that page.

The while loop you have there will block mouse movement while the right button is pressed.

The mouse.press() commands are for emulating mouse movements via the native USB port, but this is where your USB mouse will be connected, so there will be a different command depending on the emulation solution (serial or PS2). Same for mouse.move().

Okay gotcha and what do you mean by "borrowing" the cable, if I wanted to use a gaming mouse which is USB how would I do that? Is it the same?

I found something online that takes a USB connection and turns it into ps/2 like a converter, is that somewhat what you are talking about?

David1937:
Okay gotcha and what do you mean by "borrowing" the cable, if I wanted to use a gaming mouse which is USB how would I do that? Is it the same?

You plug the USB mouse into the Native port on the Arduino Due, then find an old PS2 mouse or keyboard and cut the cable off it so you can connect the cut end to the Arduino and the connector end into the PC, so that the Arduino can pretend to be a PS2 mouse, passing the the USB mouse controls to the PC.

David1937:
I found something online that takes a USB connection and turns it into ps/2 like a converter, is that somewhat what you are talking about?

I wasn't thinking of using those, but you might need one if your PC doesn't have a PS2 port
If it looks like this then yes, it should work:

But not if it looks like this:

Alright I think I am close here for coming up with a solution. I have a few questions:

So now that I have a script that will basically emulate a USB mouse, where and how do I add the code where it autoclicks? Will the two codes interfere at all? Will this downgrade the performace of my mouse to make it harder to play? Like my mouse is set to 1000hz by defualt but I am fine playing on 500hz if I have too but I don't want to play on anything that is delayed noticably.

How do I take attach the PS/2 cable into the Arduino?

Thanks!

Take a look at the PS2 code, it should indicate which pins to connect to.

Where and how the autoclick will fit depends on the code you have.
You will need to record the time the button is pressed, something like:

//define these somewhere sensible
bool LeftState = false;// state of the real mouse button
bool LeftOutput = false;// to store the state of the fake mouse left button
unsigned long ClickTime;// to store the time of a click/release
#define Rate 50 // press and release time, in milliseconds [rep rate is 1000/(2*Rate) ]

// this goes in the loop
  bool newLeftState = mouse.getButton(LEFT_BUTTON);
  if (newLeftState != LeftState) {
    LeftState = newLeftState;
    if (LeftState)  {
      ClickTime = millis();
      Mouse.press(MOUSE_LEFT);
      LeftOutput = true;
    }
  }

Then when the button is pressed, alternate the button state at a fixed rate:

// this goes in the loop
if (LeftState)  {
    if (millis() - ClickTime >= Rate) {
      ClickTime = millis(); // new start time for timing the next change in left mouse state
      LeftOutput != LeftOutput; //toggle the state
      if (LeftOutput) {
        Mouse.press(MOUSE_LEFT);
      }
      else  {
        Mouse.release(MOUSE_LEFT);
      }
    }
  }

obviously Mouse.press() and Mouse.release() will change depending on the PS2 code, which I've not had time to look at.

weird_dave:
Take a look at the PS2 code, it should indicate which pins to connect to.

Where and how the autoclick will fit depends on the code you have.
You will need to record the time the button is pressed, something like:

//define these somewhere sensible

bool LeftState = false;// state of the real mouse button
bool LeftOutput = false;// to store the state of the fake mouse left button
unsigned long ClickTime;// to store the time of a click/release
#define Rate 50 // press and release time, in milliseconds [rep rate is 1000/(2*Rate) ]

// this goes in the loop
  bool newLeftState = mouse.getButton(LEFT_BUTTON);
  if (newLeftState != LeftState) {
    LeftState = newLeftState;
    if (LeftState)  {
      ClickTime = millis();
      Mouse.press(MOUSE_LEFT);
      LeftOutput = true;
    }
  }



Then when the button is pressed, alternate the button state at a fixed rate:


// this goes in the loop
if (LeftState)  {
    if (millis() - ClickTime >= Rate) {
      ClickTime = millis(); // new start time for timing the next change in left mouse state
      LeftOutput != LeftOutput; //toggle the state
      if (LeftOutput) {
        Mouse.press(MOUSE_LEFT);
      }
      else  {
        Mouse.release(MOUSE_LEFT);
      }
    }
  }



obviously Mouse.press() and Mouse.release() will change depending on the PS2 code, which I've not had time to look at.

Okay thanks I think I gotcha.

I guess all that is left to know is how to make that autoclicker and my usb work with this code an I am all good. Instead of a fixed rate could I do it at a random rate btw? Or would the code have to change a lot because of this?

Simplest option would be to create a random offset after each toggle and add that to the Rate, 1 line changes slightly and another is added:

if (millis() - ClickTime >= Rate + RandOffset) {
 RandOffset = random(20);

But I wouldn't worry about this yet, get the basics working otherwise you'll end up with a huge bunch of code which doesn't work and won't have a clue where to start debugging it.

weird_dave:
Simplest option would be to create a random offset after each toggle and add that to the Rate, 1 line changes slightly and another is added:

if (millis() - ClickTime >= Rate + RandOffset) {

RandOffset = random(20);



But I wouldn't worry about this yet, get the basics working otherwise you'll end up with a huge bunch of code which doesn't work and won't have a clue where to start debugging it.

I think I found a USB mouse code on GitHUB but you are the master at this so I don't know if it will work. Will using this: https://github.com/dalelyunas/ArduinoMouse/blob/master/ArduinoMouse.ino work? Is it better and/or easier than using the other method?

weird_dave:
Simplest option would be to create a random offset after each toggle and add that to the Rate, 1 line changes slightly and another is added:

if (millis() - ClickTime >= Rate + RandOffset) {

RandOffset = random(20);



But I wouldn't worry about this yet, get the basics working otherwise you'll end up with a huge bunch of code which doesn't work and won't have a clue where to start debugging it.

On a side note, I am kinda getting sick of asking questions and stuff.

Is there any other microcontroller device or combination, I don't care the brand, that would make this any easier? Or would I still be in the same situation?

That mouse code will probably work, but it's for reading the mouse, which is already solved with Due specific code. I'm definitely not a master at this :smiley:

There is other hardware out there for sure. You could even use 2 Dues, one to read the mouse and the other to be a mouse, both then use their own Native USB ports (one as a host for the real USB mouse, the other a client to be the fake mouse connected to the PC), a simple serial connection (2 wires, 1 way comms) would transfer the mouse info between them. Or get a USB shield, there are many solutions to the problem. Personally I'd use software until the anticheat breaks it. I think I stumbled upon some python code to emulate a mouse recently, you could use C++ or even assembly. But then again, I enjoy this sort of thing, so finding new ways to solve the problem when the old solution breaks interests me :slight_smile:

The inline hardware solution will pretty much work for all time, but the game experience quality will depend on how good the hardware and its software is, the PC software solution should be much easier to get a good experience with since there's no interception of the movement.

weird_dave:
That mouse code will probably work, but it's for reading the mouse, which is already solved with Due specific code. I'm definitely not a master at this :smiley:

There is other hardware out there for sure. You could even use 2 Dues, one to read the mouse and the other to be a mouse, both then use their own Native USB ports (one as a host for the real USB mouse, the other a client to be the fake mouse connected to the PC), a simple serial connection (2 wires, 1 way comms) would transfer the mouse info between them. Or get a USB shield, there are many solutions to the problem. Personally I'd use software until the anticheat breaks it. I think I stumbled upon some python code to emulate a mouse recently, you could use C++ or even assembly. But then again, I enjoy this sort of thing, so finding new ways to solve the problem when the old solution breaks interests me :slight_smile:

The inline hardware solution will pretty much work for all time, but the game experience quality will depend on how good the hardware and its software is, the PC software solution should be much easier to get a good experience with since there's no interception of the movement.

Sorry, I am kinda of confused by this and by the research I have been doing.

So if I were to understand you correctly if I were to use a USB host shield (or even two due's not doing that though :stuck_out_tongue: lol) with the Due I wouldn't need to make all that emulation code?

What solution (buying more things or not) do you think would make it the easiest and best quality? But if the quality is still playable I would like something that would be the easiest solution.

And thanks you are the master if you know all this, seems unbelievable when new people like me join and see you guys with all your knowledge with this stuff. :stuck_out_tongue:

Assistance withdrawn.

David1937:
So if I were to understand you correctly if I were to use a USB host shield (or even two due's not doing that though :stuck_out_tongue: lol) with the Due I wouldn't need to make all that emulation code?

You still need to pretend to be a mouse, there's no getting away from that, this just lets you use a USB mouse and a USB port on your PC easily, you simply can't do that with a Due on its own.

David1937:
What solution (buying more things or not) do you think would make it the easiest and best quality? But if the quality is still playable I would like something that would be the easiest solution.

The in-line method is definitely not the easiest to get right. You never know, anti cheat may eventually block an Arduino mouse from being used. I really would do this purely on the PC first, then when that doesn't work, send commands to the Due to do the clicks.

MorganS:
I've been watching this discussion for a while. I don't understand the need to pass through the mouse at all.

Me neither, I wouldn't do it that way. (until the anticheat software decides otherwise, but there's always a workaround of some kind).

MorganS:
You can plug more than one mouse into your computer. If you have a laptop with a touchpad plus an external mouse, you are doing this all the time. You can make movements with one mouse and clicks with the other. This seems to be a normal way of doing things.

This is one of the methods I mentioned early on, with various options of getting the due to click (switches, serial commands from the PC).

MorganS:
So use a Due (or whatever native-USB Arduino variant you prefer - there's a lot of them now) and have it generate clicks when you push a button on that Due. Keep using your normal mouse like normal, with no modifications. You might like to hot-glue the Due's button on top of your mouse, but that's a minor enhancement.

The problem is activating the button on the due, I mentioned a foot switch in post 15. mounting an extra switch on the mouse isn't really viable for long term battle, the un-ergonomic nature of it will likely cause pain. The OP doesn't want to modify any mice, so re-wiring a spare button to the due isn't an option really either since you'd need to make some holes for the wires. very thin wire might just squash-fit next to the existing cable entry point.

weird_dave:
Me neither, I wouldn't do it that way. (until the anticheat software decides otherwise, but there's always a workaround of some kind).
This is one of the methods I mentioned early on, with various options of getting the due to click (switches, serial commands from the PC).
The problem is activating the button on the due, I mentioned a foot switch in post 15. mounting an extra switch on the mouse isn't really viable for long term battle, the un-ergonomic nature of it will likely cause pain. The OP doesn't want to modify any mice, so re-wiring a spare button to the due isn't an option really either since you'd need to make some holes for the wires. very thin wire might just squash-fit next to the existing cable entry point.

Actually I kinda don't mind the option for the foot pedal. What would be the best way that I could use my feet to trigger a quick response. For example, for guns I would have to maybe have the foot pedal rapid-firing for 10 seconds then have to let go for maybe a second to let it reload quickly and then repeat a few times before I don't see anyone anymore or I am dead.

What is a idea that I could use my feet where I wouldn't accidentally press it that often but also holding it down in small spurts and then quickly taking it off? Like would a normal foot pedal be fast enough? What do you think is the best option?

weird_dave:
Me neither, I wouldn't do it that way. (until the anticheat software decides otherwise, but there's always a workaround of some kind).
This is one of the methods I mentioned early on, with various options of getting the due to click (switches, serial commands from the PC).
The problem is activating the button on the due, I mentioned a foot switch in post 15. mounting an extra switch on the mouse isn't really viable for long term battle, the un-ergonomic nature of it will likely cause pain. The OP doesn't want to modify any mice, so re-wiring a spare button to the due isn't an option really either since you'd need to make some holes for the wires. very thin wire might just squash-fit next to the existing cable entry point.

This is just a guess but I was wondering if getting one of those LattePanda mini computers would be a solution to the problem. I could plug the mouse into the mini microcontroller computer and run anything I wanted (you said you found a Python script) and then convey that to my computer.