📜 ⬆️ ⬇️

Connect a light gun through the Arduino



A little story about nailing with a microscope.

Having bought arduino, and traditionally blinking with a LED (such a kind of “Hello World”), it was decided to do something more useful. To train and explore the possibilities of the arduino, I decided to connect a light gun to a computer through it. Of course, such a sickly adapter turns out, but I have never had a deal with electronics before, so this task turned out to be quite interesting.
So, schemes and the description of work of the light gun were found:
ftp.radio.ru/pub/arhiv/1997/10-97/29-10-1997.jpg
ftp.radio.ru/pub/arhiv/1997/10-97/30-10-1997.jpg
From there, I was primarily interested in pinouting the contacts of the gun. Also for reliability the pistol was disassembled and pierced with a multimeter.
')


Brown wire - ground
Red wire - photodiode
Blue wire - trigger
Yellow wire - power

We connect the gun to arduino. The trigger was connected to the digital port, and the signal from the photodiode to analog.



A sketch was written for arduino:

const int DPIN7_SWITCH_GUN = 7; //  const int APIN4_LIGHT_GUN = 4; // int valSwitch = 0; int valLight = 0; byte data[5]; void setup() { Serial.begin(9600); pinMode(DPIN7_SWITCH_GUN, INPUT); } void loop() { valSwitch = digitalRead(DPIN7_SWITCH_GUN); valLight = analogRead(APIN4_LIGHT_GUN); if (valSwitch == HIGH || valLight>3) { data[0] = 0x47; //G data[1] = 0x55; //U data[2] = 0x4e; //N data[3] = valSwitch; data[4] = valLight/4; Serial.write(data, 5); } } 


During testing, it was found that the signal from the photodiode does not come all the time, but only with a sharp change in illumination from dark to light. In this case, if at this moment the trigger is pressed - the signal is stronger. With the hammer pressed, the port receives the value of 100-120, without pressing 10-30.

The case remains for small - to write a host program on the side of the computer.
Here I walked around the rake, trying to correctly receive data coming from the COM port. The data did not want to come in an even packet of 2 bytes (initially only the values ​​of the trigger and photodiode were sent). A trivial problem, but if somebody goes my way, you need to keep in mind. As a result, for reliability, a header was added, and the data stream as it should be was accumulated in the buffer and read from there as needed.

Finally, earned a test program.



And now, instead of darts, you can use a high-tech light gun. Or implement Duck Hunt.



However, there are still a few pitfalls. The program is written in Delphi \ Lazarus (not for the sake of holivar, and yes, the pascal is still alive), where the built-in timer and the image rendering speed on the canvas do not allow to carefully synchronize the chain - shot-> fill the screen with black-> highlight the target with white-> analyze current with photodiode. In view of this, substantial delays were inserted - the target illumination from the “passport” 20 ms was increased to half a second.



Of course, you can go to faster and more productive solutions, but I doubt that they will completely help get rid of the blinking screen. After all, the iron prefix, due to interruptions, always knew that it was at a particular point in time on the TV screen (since the refresh rate of the screen was also constant).

Thanks for attention. I will welcome any comments.

Source: https://habr.com/ru/post/141643/


All Articles