Created
January 16, 2015 14:43
-
-
Save alexellis/1ebca0b448fd623b672d to your computer and use it in GitHub Desktop.
Laser/torch trip wire for Arduino. Uses RCTime equation and capacitor/LDR.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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