Main > Lightguns
Updated IR light gun Arduino sketch
(1/4) > >>
JayBee:
Hey guys,

I thought I would share my IR light gun Arduino code I've been working on lately, for whoever is interested.
This project started as a small update of SAMCO's great IR Cam code ( https://github.com/samuelballantyne ), but since I rewrote most of the code I decided to make my own Github.
It uses the same hardware (beside pin numbers since I'm using a ATmega32U4 chipset), and I also added more hardware for a solenoid (will post about that later).

The Arduino sketch can be found here: https://github.com/JayBee-git/IR_Light_Gun

Here is the list of notable differences compared to SAMCO's original code:

* A bit more precise mouse cursor; I kept SAMCO's efficient position calculation code, but made it float instead of integer. The difference is not huge (few pixels), but it removes a bit of cursor jittering.
* Faster cursor and less latency; since I rewrote everything without delay function in the main loop, the exection is faster and the mouse has less lag.
* Out of range detection; Detects if there are less than 2 points visible (out of the screen aiming, obsctacle...), leaves the cursor at the last known point (to avoid accidental cursor jump), and releases the mouse control (to use a normal mouse).
* Calibration parameters; You can now edit where you want the calibration points to be on the screen.
* Calibration error check; To avoid calibration problems, it will not let you save calibration points if it's out of range (for instance if your IR Cam doesn't see one of the IR LED). It will let you repeat the aiming as long as it's not correctly detected.
* Joystick mode support; Now you can also use your Gun as a joystick for games that don't have mouse support. You can enable/disable it by holding the calibration button more than 2 seconds. There seems to be slightly more latency in this mode, so I would advice always using the mouse mode whenever possible.
* Save calibration points and joystick/mouse mode in the arduino EEPROM; Now your last calibration points, as well as the last mode used (joystick/mouse) are saved in the arduino EEPROM memory. You can disable the option easily if you don't want to use it.
* Button holding detection, with timer for each button; A hold function will be triggered on whatever button you hold, with the timing you want. Useful for things like fullauto recoil mode or simply having a secondary button function when holding.
* Sleep timer; This timer is used to pause any calculation when not aiming at the screen for a while. I wanted to also shutdown the camera, but it doesn't seem to want to be switched OFF and ON again. (limitation of the Wire library?)
* Position filter; You can activate this option to make motion become a lot smoother, and removes lot of jittering, but it also adds more latency. I recommend to leave it off unless you really have precision trouble.
* Rumble motors and solenoid support; If you connect a solenoid board or a rumble motor to pin 7 (or other pin if you change it), you will get rumble/recoil support. It will be triggered once when the left button (gun trigger) is pushed, and switch to fullauto mode if the button is hold. Be very careful if you use solenoid as you need the correct circuit for it, and using a bad timing settings might also damage it. As the other options, you can change the settings at the beginning of the sketch file.
Thing to add next:

* Add button combo detection (for instance calibration + trigger).
* A way to enable/disable cursor filter with a button combo.
* Make a Windows tool to manage those options, and help with the configuration.
* Investigating in new ways of improving even further the accuracy and viewing angle.
* Trying to support PS2/XBox or other old hardware. If somebody can help me with the PS2/XBox protocol, that would be great :)
* Add a RGB LED support for status/mode control and errors.
Tell me what you think about it, and if you have anything to add/change, any suggestion is welcome ;)

External libraries used from DFRobot, Jonathan Edgecome and Matthew Heironimus (big thanks to them). You can find them here: https://github.com/DFRobot/DFRobotIRPosition https://github.com/jonathanedgecombe/absmouse https://github.com/MHeironimus/ArduinoJoystickLibrary
lightgungamer:
Hey, this looks awesome. It’s great to see people working on this.

So would your code convert to work on the itsybitsy m0 used in the samco project just by changing the ports used in the code?

Do you think there would be any benefit to using more led points?
JayBee:
Thanks, I really like working on it, I will keep updating it as much as needed :)

Yes of course it should work on the istybitsy m0.
Although I forgot to add a code to disable the solenoid/rumble function if you don't have one, might add it today.

I thought about adding more led too, but here is the big problem with it: the camera is not able to recognize which led is which anymore if you stop pointing them.
For instance if you have 4 LEDs, and aim outside of the screen, and come back to see only 2 of them, the camera has no idea of which 2 led it is.
When you have only 2 led it's pretty simple, there is always one on the left, one on the right, easy to know where they should be.

But I am currently investigating for a better aiming solution that will be fully compatible with this hardware, stay tuned for more ;)
lightgungamer:
That sounds interesting. I’ll definitely keep an eye on it.
The viewing angle and issue with losing sight of the points does seem to be a problem. There is some chat on the samco github page which looked interesting about dealing with more points and keeping track of them. It’s a bit beyond me though!
https://github.com/samuelballantyne/IR-Light-Gun/issues/1
JayBee:

--- Quote from: lightgungamer on September 08, 2019, 03:20:31 am ---That sounds interesting. I’ll definitely keep an eye on it.
The viewing angle and issue with losing sight of the points does seem to be a problem. There is some chat on the samco github page which looked interesting about dealing with more points and keeping track of them. It’s a bit beyond me though!
https://github.com/samuelballantyne/IR-Light-Gun/issues/1

--- End quote ---
Yeah Toni Spets code looks so nice and promising, I really want to study it and see if I can use it for our project, it would be awesome to have such precision, range and auto calibration for it ;D
Navigation
Message Index
Next page

Go to full version