An Automatic Tazer Drawer (inspired by Techjoyce)
by nagarihant in Circuits > Arduino
30 Views, 0 Favorites, 0 Comments
An Automatic Tazer Drawer (inspired by Techjoyce)
Are you tired of your siblings taking whatever prized object you have, well look no further than a tazer controlled by an ultrasonic sensor strapped to a drawer¯\_(ツ)_/¯
Supplies
Arduino uno
boost converter (tazer)
relay
esp32 (power)
Arduino to Relay
here instead of a light or a lock being toggled a TAZER will be😈.
Tazer
connect the red wire to your vin or 5v and your black wire to the COM on your relay. after that connect another black wire form the 5v or VIN to your NO on the relay.
Code (the Worst Part)
const int TRIG_PIN = 7;
const int ECHO_PIN = 6;
const int RELAY_PIN = A5;
const float DISTANCE_THRESHOLD = 3.81; // cm (1.5 inches)
const float SOUND_SPEED = 0.017; // cm/us
const unsigned long DEBOUNCE_DELAY = 100; // milliseconds
float duration_us, distance_cm;
bool relayState = LOW;
unsigned long lastToggleTime = 0;
void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
}
void loop() {
// Trigger the sensor
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Measure the pulse duration
duration_us = pulseIn(ECHO_PIN, HIGH);
// Handle case where pulseIn times out
if (duration_us > 0) {
distance_cm = SOUND_SPEED * duration_us;
// Check distance against threshold
if (distance_cm < DISTANCE_THRESHOLD) {
if (relayState == LOW) {
relayState = HIGH; // Activate relay
lastToggleTime = millis();
}
} else {
if (relayState == HIGH && (millis() - lastToggleTime >= DEBOUNCE_DELAY)) {
relayState = LOW; // Deactivate relay
}
}
} else {
Serial.println("Measurement timeout or error");
}
// Control the relay
digitalWrite(RELAY_PIN, relayState);
// Print the distance
Serial.print("Distance: ");
Serial.print(distance_cm > 0 ? distance_cm : 0); // Avoid negative distances
Serial.println(" cm");
delay(120); // Small delay for stability
}