Sound based trigger mechanism attachment for toy gun

What?

It is a trigger mechanism for a (toy) gun composing of a mic, an Elegoo Uno (Arduino knock-off), and a servo motor. 

It detects when your stand-off opponent fires and then returns fire for you. Doubles as a shout-activated trigger mechanism.


Why?
 
I wanted to expand my coding skills from Python to another language, and doing some Arduino projects seemed like a fun and practical way to do that. 

My original plan was to use it for a flamethrower project, the concept was simple enough - use a mic to detect a loud noise then move a motor. As simple as the firmware should be, I wanted to test it out on something a little less dangerous first and the parts for the flame thrower were taking forever to arrive. 

So I arrived at this. The concept of an old wild west style standoff was one that always excited me as a kid but also confused me. 

The whole point is that someone will eventually shoot, and if the other person shoots first it's very unlikely that your reactions will be quick enough to return fire.

So to combat this as almost a failsafe, this mechanism can detect when the other person fires and return fire for you so you can take the other person down with you... hence every standoff will be a draw, everyone wins!


The Video



Hardware
 
The BB gun I have owned since I was a lot younger - a few days playing with it with friends then it was forgotten about for years. Finally, a productive(?) use for it.

The other components used:

§  1 x Elegoo Uno R3 (I purchased this starter kit but you can buy just the uno separately.)

§  1 x Microphone Amplifier

§  1 x Servo Motor

§  1 x 9V battery

§  Some string to pull the trigger and Duct tape to hold it all together 


Not much to say here about the assembly process, it was pretty much a bodge job. The mic & Uno were hanging off the front and motor & battery were duct-taped to the side. 


If I had access to a 3D printer or maybe some more tools I could've made a proper housing for the components to make it look prettier, but there's something about how bad it looks that makes it more funny, I think.



When I return to University in September I will have access to more equipment and so hopefully on future projects, the finished product will look better.


Firmware

 

The following code was written in the Arduino IDE:


int threshold;
#include <Servo.h>
Servo myservo;

void setup() {
  Serial.begin(9600);
  pinMode(A0, INPUT); //mic connected to A0 pin
  myservo.attach(9); //servo connected to pin 9
  myservo.write(30); //moves servo to start position
  FindThreshold(); // Calculates threshold based on ambient sound level
}

//_______________________________________________________
void loop() { if(analogRead(A0) > threshold) { myservo.write(0); delay(500); myservo.write(30); } Serial.println(analogRead(A0)); delay(10); // Avoids double sampling the same noise } //_______________________________________________________ void FindThreshold() { int i; unsigned long sum = 0; Serial.print("...calculating background sound level..."); digitalWrite(LED_BUILTIN, HIGH); for (i = 0; i < 10000; i++) { sum += analogRead(A0); delay(1); } digitalWrite(LED_BUILTIN, LOW); threshold = int((sum/10000) * 1.5); Serial.print("\nAverage Background sound level: "); Serial.println(sum/10000); Serial.print("Threshold set at 1.5 x Average: "); Serial.println(threshold); }


I've tried to add //comments as much as possible, but here is a basic explanation of what's going on:

Sound waves cause a diaphragm in the Mic to vibrate, which produces a voltage, giving essentially a volume level. This voltage is passed to the Elegoo Uno in real-time and looks like this:


In this snippet, I am talking which can be seen in the pulses of higher volume level, but in the gaps of my speech, it is still constantly vibrating. 

We only want our trigger to be pulled if the opponent has fired, and this constantly changing volume could trigger it at a random point if we set a constant threshold level, especially if there is wind or someone talking.

To account for the vibration,  the FindThreshold() function in the code takes a 10-second sample of the ambient sound level and sets a threshold of 1.5 times that level. 


Then in the main loop of the code, the microphone is constantly reading in the sound level, and if it detects that this threshold is exceeded it will send a command to the servo motor to rotate 30 degrees, which will pull the string attached to the trigger. 

This is of course not meant to be used in a serious way, but it was fun to put together nonetheless.


Obstacles

No real obstacles on this one apart from the servo motor I originally had from the starter kit was not providing enough torque to pull the trigger, but this was a quick fix in purchasing a stronger one from Amazon.


Comments

Popular Posts