3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Motion Sensitive Tesla Coil

Step 2Code

Below is the code for the Halloween part of the program. When someone walks by less than 2m away and no one has passed by for 1.5 minutes, the Tesla Coil will turn on.

int transistor = 13;                                 //transistor base to resistor to pin 13unsigned long time = 0;int avgdistance = 0;void setup(){  Serial.begin(9600);                                //protip: plug in a serial lcd for quick testing  sonarSetup();  pinMode(transistor, OUTPUT);  delay(2000);                                       //give time for everything to get ready  for(int i = 0; i <= 30; i++)                       //get an average distance on how far away the other wall is  {    avgdistance += sonarPulseWidth();  }  avgdistance /= 30;  avgdistance -= 50;}void loop(){  int distance = sonarPulseWidth();  Serial.println(distance);  if(distance < avgdistance && (time + 90000) < millis())    //distance has to be less than the avgdistance away                                                             //and can't have been triggered in the past 90 seconds  {    digitalWrite(transistor, HIGH);    delay(500);                                      //turn coil on for 1/2 second    digitalWrite(transistor, LOW);    time = millis();  }}

Also, the code to run the sonar sensor is below.

                         //Sonar GND to ground                         //Sonar +5 to +5                         //Sonar TX -- Useless for this project                         //Sonar RX -- Useless for this project                         //Sonar AN -- Useless for this projectint sonarPW    = 5;      //Sonar PW to digital pin 5, 147uS per inch (2.54cm/in)                         //Sonar BW -- Useless for this projectvoid sonarSetup(){  pinMode(sonarPW, INPUT);}int sonarPulseWidth(){  return ((pulseIn(sonarPW, HIGH) / 147) * 2.54);}

You can download the sketch below.
« Previous StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
79
Followers
6
Author:DevCoder(My Hompage)
Devin is a cool guy, eh doesnt kill himself while doing stupid things, and doesnt afraid of anything.