Advertisement
digigram

TripBeam

Dec 30th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. /*
  2.   Laser Trip Beam Alarm
  3.   Created by Chris Swart
  4.   http://digigram.za.net
  5.   Using Energia 0101E0009 on Linux Mint 13
  6.  
  7.   Version 0.0.1 - 30 December 2012
  8.  
  9.   Alarm Activates {armedDelay} seconds after reset button
  10.   Beam needs to be interupted for more than {howLongMax}x10 milliseconds to sound the alarm
  11.   This is to weed out false trips from ADC errors
  12. */
  13.  
  14. /*
  15. Circuit
  16. Status LED - ARMED connected to pin 14
  17. Status LED - INTRUDER connected to pin 9
  18. Sensor LED - RED connected to pin A0 (1) and GND --- Red LED for use with red laser pointer (<5mW)
  19. */
  20.  
  21. //Declarations
  22. const int analogInPin = A0;  // Analog input pin that the receiving diode (Red LED) is attached to
  23. const int LED_INTRUDER = 9;  // Intruder alert LED. Mine is Blue :)
  24. const int LED_ARMED = 14;    // System is arming when blinking, armed when steady on
  25. int sensorValue = 0;         // value read from the LED
  26. int threshold = 100;         // Below this value the alarm will trip. You might need to alter this value
  27. int howLong = 0;             // How many consecutive measurements were missed
  28. const int howLongMax = 5;    // Alarm will only sound if this many consecutive measurements were missed
  29. const int armedDelay = 10;   // Delay from reset to Armed in seconds
  30.  
  31. void setup() {
  32.   // Debugging
  33.   Serial.begin(9600);
  34.   // Set status LEDs
  35.   pinMode(LED_INTRUDER, OUTPUT);
  36.   pinMode(LED_ARMED, OUTPUT);
  37.   digitalWrite(LED_INTRUDER, LOW);
  38.   digitalWrite(LED_ARMED, LOW);  
  39.   // Delay arming
  40.   for (int timer = armedDelay; timer >= 0; timer--){
  41.     digitalWrite(LED_ARMED, HIGH);
  42.     delay(200);
  43.     digitalWrite(LED_ARMED, LOW);
  44.     delay(800);
  45.   }
  46.   digitalWrite(LED_ARMED, HIGH);  //system is armed
  47. }
  48.  
  49. void SoundAlarm() {
  50.   //Intruder LED will light. Siren can be inserted here.
  51.   //Feedback to controlling MCU will follow later
  52.   digitalWrite(LED_INTRUDER, HIGH);
  53.   digitalWrite(LED_ARMED, LOW);  
  54.   Serial.println("INTRUDER");
  55.   delay(1000);
  56. }
  57.  
  58. void loop() {
  59.   //replace with interrupts in following versions to decrease power usage
  60.   sensorValue = analogRead(analogInPin);      
  61.   if (sensorValue < threshold){
  62.     howLong = 0; //Clean the counter
  63.   }
  64.   else {
  65.     howLong += 1;
  66.     Serial.println(howLong);
  67.   }
  68.  
  69.   // If counts are exceeded, alarm will sound WITHOUT interruption
  70.   // Interruption and reset will be established at the controlling MCU
  71.   while (howLong >= howLongMax){
  72.     SoundAlarm();
  73.   }
  74.   //Uncomment this line to establish a good value for threshold
  75.   //Serial.println(sensorValue);  
  76.   delay(10);      
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement