To simulate the buttons being pressed, I am using a set of relays. The relays are connected in parallel with the switches on the gun. One side of the relay switch needs to be connected to each side of the switch on the gun. When the relay is turned on, the functions on the gun will activate just as if the button had been pressed.
The relays that I used only required 20mA to activate, so I was able to connect them directly to the Arduino. If your relay requires more than 30mA, then you need to use some kind of driver circuit to activate it. You can also use an Arduino relay shield.
Any time you have a relay connected to a sensitive circuit such as a microcontroller, you should connect a diode across the terminals of the relay coil. This helps to prevent damage from the voltage spikes that occur when the relay turns off.
First I prototyped the relay circuit on a breadboard. Then I connected the wires from the gun and the Arduino. This let me test it out to make sure that I could control all the functions with the Arduino. Once that was working, I connected to the motion sensor.
Upload the Arduino code:
// Here is some sample code that you can use to start the project.
int relayOne = 2; // Relay connected to digital pin 2
int relayTwo = 5; // Relay connected to digital pin 4
int relayThree = 6; // Relay connected to digital pin 6
int motionSensorPin = 13; // Motion Sensor connected to digital pin 13
int motionSensorState = 0;
void setup()
{
pinMode(relayOne, OUTPUT); // sets the digital pin 2 as output
pinMode(relayTwo, OUTPUT); // sets the digital pin 4 as output
pinMode(relayThree, OUTPUT); // sets the digital pin 6 as output
pinMode(motionSensorPin, INPUT); // sets the digital pin 13 as input
Serial.begin(9600);
}
void loop()
{
motionSensorState = digitalRead(motionSensorPin); //read output of motion sensor
if (motionSensorState == HIGH) // if motion is detected, fire gun
{
digitalWrite(relayTwo, HIGH); // turns the second relay on to fire the gun
delay(1000);
digitalWrite(relayOne, HIGH); // turns the first relay on to turn the turret
delay(150);
digitalWrite(relayOne, LOW); // turns the first relay off
delay(500);
digitalWrite(relayThree, HIGH); // turns the third relay on to turn the turret back
delay(300);
digitalWrite(relayThree, LOW); // turns the third relay off
delay(500);
digitalWrite(relayOne, HIGH); // turns the first relay on to turn the turret
delay(300);
digitalWrite(relayOne, LOW); // turns the first relay off
delay(500);
digitalWrite(relayThree, HIGH); // turns the third relay on to turn the turret back
delay(300);
digitalWrite(relayThree, LOW); // turns the third relay off
delay(500);
digitalWrite(relayTwo, LOW); // turns the second relay off to stop firing the gun
digitalWrite(relayOne, HIGH); // turns the first relay on to turn the turret all the way to one side
delay(700);
digitalWrite(relayOne, LOW); // turns the first relay off
delay(500);
digitalWrite(relayThree, HIGH); // turns the third relay on to turn the turret back to the center position
delay(375);
digitalWrite(relayThree, LOW); // turns the third relay off
delay(1000);
}
else // if no motion is detected, default to setting all motors off
{
digitalWrite(relayOne, LOW);
digitalWrite(relayTwo, LOW);
digitalWrite(relayThree, LOW);
}
}