Hello.
I have a ps2 p99 light gun with aimtrak module.
It have a motor for recoil and i'm using this code for control it with arduino.-
/*
P99RecoilLAPW (L293d Attiny PulseWidth)
This program receives a 5v pulse on the int pin on the uC and activates the motor pin on a L293d to spin
the recoil motor on a P99 light gun for a fixed amount of time. This version uses an ATTiny85 instead of
an arduino. Also, this version measures the width of the pulse coming in and adds that to a static wait
time so that the motor run time can be adjusted slightly adjusted directly through the Aimtrak software
(adjustment time should be between 1 - 40 ms. Even with 8MHz processor speed, the smallest pulse duration
of 1ms should be captured by the uC). Works fine with an ATTiny running at 8MHz.
Pinout (ATTiny85)
_____
(N/C) PB5 -|1 8|- VCC (5v)
(N/C) PB3 -|2 7|- PB2 (Aimtrak recoil signal input)
(P99 Recoil Enable Switch) PB4 -|3 6|- PB1 (L293d enable pin)
GND -|4___5|- PB0 (L293d input 2)
This code is in the public domain.
Foodbag
July 6, 2013
*/
#define in2Pin 0 // Drives the motor driver
#define recoilPin 2 // Reads recoil pin from Aimtrak
#define recoilPinInt 0 // Pin 2 Interrupt
#define recoilEnbSwitch 4 // Reads the enable recoil switch
#define enablePin 1 // Sets the enable pin on the L293d
unsigned long staticWaitTime = 130000; // How long the static wait time for the firing is (in microseconds)
unsigned long maximumMotorTime = 230000; // Disable motor after this long
volatile boolean readyToFire = true;
volatile boolean fireMotor = false;
volatile boolean lastEdgeRising = false; // Seems to help with to falling edges doubling motor run time
volatile unsigned long firingTime = 0; // When the rising signal from the Aimtrak Recoil pin is captured
volatile unsigned long recoilSigLen = 0; // How long the pulse is from the Aimtrak Recoil pin
void setup()
{
pinMode(enablePin, OUTPUT); // Sets the driver enable pin to output
digitalWrite(enablePin, LOW); // Sets the driver enable pin low to start (it gets enabled with one loop of program)
pinMode(in2Pin, OUTPUT); // Sets the driver pin 2 to output
digitalWrite(in2Pin, HIGH); // Sets the driver pin 2 to 5v (which is the motor break)
pinMode(recoilEnbSwitch, INPUT); // Sets the recoil enable switch pin to input
digitalWrite(recoilEnbSwitch, HIGH); // Sets the internal interrupt for the recoil enable pin
pinMode(recoilPin, INPUT); // Sets the recoil sensor pin to an input (we need to read the pin the interrupt
attachInterrupt(recoilPinInt, captureFiringSignal, CHANGE); // Sets up the interrupt for the recoil pin
}
void captureFiringSignal()
{
if (readyToFire == true)
{
if (digitalRead(recoilPin) == HIGH) // If this is the rising edge of the pulse, record pulse start time and let the motor be fired
{
firingTime = micros();
fireMotor = true;
lastEdgeRising = true;
}
else if (lastEdgeRising == true) // If this is the first falling edge, calculate pulse width. Otherwise ignore
{
recoilSigLen = (micros() - firingTime);
readyToFire = false;
fireMotor = true;
lastEdgeRising = false;
}
}
}
void loop()
{
if(fireMotor == true)
{
digitalWrite(in2Pin, LOW); // Start motor
unsigned long firingStart = micros();
while (micros() - firingStart <= staticWaitTime + recoilSigLen && micros() - firingStart <= maximumMotorTime) {} //Wait for motor to spin long enough
digitalWrite(in2Pin, HIGH); // Stop motor
fireMotor = false; // Don't let motor run till next pulse received
readyToFire = true; // Look for new pulse
}
else if (digitalRead(recoilEnbSwitch) == LOW) // If switch is turned to recoil, enable L293d to spin motor (ie enable recoil)
{
digitalWrite(enablePin, HIGH);
}
else // Otherwise, disable L293d so motor can't spin (ie disable recoil)
{
digitalWrite(enablePin, LOW);
}
}
I've attached the schematic for my circuit with a 3 position switch (food_bag user write the code and the circuit). Original schematic only has a normal 2 position switch.
I want to mod the code for machinegun mode. In machinegun mode i want recoil without stop when I keep trigger button pushed. I will add switch with 3 position: position 1 recoil off, position 2 recoil normal and position 3 machinegun mode.
Could somebody help me with the code? My knowledge are really limited...
Thanks.