Skip to content

Instantly share code, notes, and snippets.

@alexellis
Created January 16, 2015 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexellis/1ebca0b448fd623b672d to your computer and use it in GitHub Desktop.
Save alexellis/1ebca0b448fd623b672d to your computer and use it in GitHub Desktop.
Laser/torch trip wire for Arduino. Uses RCTime equation and capacitor/LDR.
int sensorPin = 7; // 220 or 1k resistor connected to this pin
long threshold = 200;
long ambientReading;
long ambientReadingCount;
bool tripEnabled = false;
int LED_PIN = 3;
int BUZZER_PIN = 4;
int ambientReadingsToTake = 10;
void setup() // run once, when the sketch starts
{
Serial.begin(9600);
Serial.print("Taking initial ");
Serial.print(ambientReadingsToTake);
Serial.println(" baseline readings");
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN,OUTPUT);
tripEnabled=false;
ambientReadingCount=0;
ambientReading=0;
}
void ProcessAmbient(value) {
if(ambientReadingCount<ambientReadingsToTake)
{
ambientReadingCount=ambientReadingCount+1;
ambientReading=ambientReading+value;
}
else
{
Serial.print(ambientReading);
// Average the readings.
ambientReading = ambientReading/ambientReadingsToTake;
Serial.print("Baseline:");
Serial.println(ambientReading);
UpdateOutput(HIGH);
delay(300);
UpdateOutput(LOW);
tripEnabled=true;
}
}
void UpdateOutput(int value){
digitalWrite(LED_PIN, value);
digitalWrite(BUZZER_PIN, value);
}
void loop() // run over and over again
{
long value = RCtime(sensorPin);
Serial.print( value);
Serial.print(" - ");
if(tripEnabled)
{
Serial.println(ambientReading );
}
if(tripEnabled == false)
{
ProcessAmbient(value);
}
else
{
if(ambientReading - value > threshold)
{
UpdateOutput(HIGH);
Serial.println("Trigger");
}
else
{
UpdateOutput(LOW);
}
}
delay(15);
}
long RCtime(int sensingPin){
long result = 0;
pinMode(sensingPin, OUTPUT); // make pin OUTPUT
digitalWrite(sensingPin, HIGH); // make pin HIGH to discharge capacitor - study the schematic
delay(1); // wait a ms to make sure cap is discharged
pinMode(sensingPin, INPUT); // turn pin into an input and time till pin goes low
digitalWrite(sensingPin, LOW); // turn pullups off - or it won't work
while(digitalRead(sensingPin)){ // wait for pin to go low
result++;
}
return result; // report results
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment