Dog Alarm

//12-12-11 Reversed test motor output to match John NPN motor control transistors
//12-11-11 Added - test switch input controls test motor and test sound output  
//                 switch closed - motor on for motorRun time and sound on while switch closed
 
#include <Servo.h>
 
int motorRun = 100;  //output motor run time in ms
int servoInitial = 90; //initial servo angle (0=CCW,90=mid;180=CW)
int servoAngle = 160;  //activated servo angle (0=CCW,90=mid;180=CW)
 
const int SWITCH = 19 ;       // arm/disarm switch pin
const int ARMED_LED = 18;     //armed LED pin
const int ALARM_PIN1 = 6;     //define 1st alarm pin location
const int ALARM_PIN2 = 7;     //define 2nd alarm pin location
const int ALARM_PIN3 = 8;     //define 3rd alarm pin location
const int INPUT_NUM = 4;      // define # of input circuits
const int ALARM_NUM = 3;     // define # of output circuits 
Servo servoOut1;               //1st servo output
 
//initialize for test switch input and motor and noise output
int n_switch = HIGH;
int old_switch = HIGH;
int freq = 440;             //test noise frequency
//add pin 9 as test input and pin 10 as test motor output and pin 11 as test noise output
const int TSWITCH_PIN = 9;
const int TMOTOR_PIN = 10;
const int TNOISE_PIN = 11;
 
 
int index = 0;
int val = HIGH;
int old_val = HIGH;
int armed = 0;              //system armed state 0=disarmed
int tripNumber =0;          //number of input circuits tripped
 
int inputPins[INPUT_NUM]={2,3,4,5};    // define input pin locations
int ledPins[INPUT_NUM]={14,15,16,17};  // define input led pin locations
int inputTripped[INPUT_NUM]={1,1,1,1};      // activation state of input circuits (1=not activated)
 
void setup()
{ 
 pinMode(SWITCH, INPUT);                //define arm/disarm switch as input
 digitalWrite(SWITCH, HIGH);           //engage pull up resistor for arm/disarm switch input
 pinMode(ARMED_LED, OUTPUT);           //define armed LED as output
 servoOut1.attach(ALARM_PIN1);         //1st servo output connected to pin
 
//initialize test pins
 pinMode(TSWITCH_PIN, INPUT);          //test switch pin as input
 digitalWrite(TSWITCH_PIN, HIGH);      //engage pull up resistor for test switch input
 pinMode(TMOTOR_PIN, OUTPUT);           //define test motor as output
 pinMode(TNOISE_PIN, OUTPUT);           //define test noise as output
 digitalWrite(TMOTOR_PIN, LOW);        //test motor pin for motor off 
 
 for ( index=0; index < INPUT_NUM ; index++) //NECESSARY TO STEP THROUGH OR JUST SPECIFY MATRIX?
   {
   pinMode(inputPins[index], INPUT);      // define input pins as input
   pinMode(ledPins[index], OUTPUT);       // define led pins as output
   digitalWrite(inputPins[index], HIGH);  // initialize input pins high to engage pull up resistors
   }
   pinMode(ALARM_PIN1, OUTPUT);    //define alarm pins as output
   pinMode(ALARM_PIN2, OUTPUT);    //define alarm pins as output
   pinMode(ALARM_PIN3, OUTPUT);    //define alarm pins as output
   servoOut1.write(servoInitial);  //initialize servo position 90=centered
   digitalWrite(ALARM_PIN2, LOW);  //initialize alarm pin 2 to off  
   digitalWrite(ALARM_PIN3, LOW);  //initialize alarm pin 3 to off                                                                           
}
 
void loop()
{
 
//check test switch and close test ouput and test noise circuits if activated
  n_switch = digitalRead( TSWITCH_PIN);
  if ((n_switch == LOW) && (old_switch == HIGH))  //test switch closed
   {
     delay(20);                         //eliminate read during contact bounce
     tone(TNOISE_PIN,freq);              //turn on noise at specified frequency
     digitalWrite(TMOTOR_PIN, HIGH);    //turn on test motor circuit
     delay(motorRun);                   //motor on for specified run time
     digitalWrite(TMOTOR_PIN, LOW);     //turn off test motor circuit
     old_switch = LOW;
   }
  n_switch = digitalRead( TSWITCH_PIN);  
  if ( (n_switch == HIGH) && (old_switch == LOW) )  //test switch opened
   {
     noTone(TNOISE_PIN);               //turn off noise
     old_switch = HIGH ; 
   }
 
 
 
  //check input circuits and set circuit LEDs and state matrix accordingly
  for (index=0 ; index < INPUT_NUM ; index++)  //for all input pins
   {
   if (digitalRead(inputPins[index]) == LOW) //check for closed circuit
    {
    digitalWrite(ledPins[index],HIGH);     //turn on LED for activated (closed) input circuit
    }
   else
    {digitalWrite(ledPins[index],LOW);   //turn off LED for deactivated (open) circuits
    inputTripped[index] = 1;   //record circuit as open - will always happen prior to arming if all circuits open
    }
   }
 
 //check arm/disarm switch and change armed state if closed
  val = digitalRead(SWITCH);  //Read arm/disarm switch and store LOW=switch pushed
  if ((val==LOW) && (old_val == HIGH)) //change from open to closed switch
   {
    armed = 1 - armed;     //Change armed state due to switch activation
    delay(20);             //Eliminate reading during contact bounce
 
   if ( armed == 1 ) //system armed
    {
     tripNumber = 0;
     digitalWrite(ARMED_LED,HIGH);  //turn on armed LED
    }
   else  //system disarmed
    {
     digitalWrite(ARMED_LED,LOW);    //turn off armed LED
     servoOut1.write(servoInitial);            //reinitialize servo position 90=centered
     digitalWrite(ALARM_PIN2, LOW);  //reinitialize output pin 2 to off
     digitalWrite(ALARM_PIN3, LOW);  //reinitialize output pin 3 to off    
 
    }
  }
  old_val = val;
 
 
//for armed system check for new activated input circuits and if so activate next alarm circuit 
  if (armed == 1)
  {
  for (index = 0; index<INPUT_NUM; index++)           //check all input circuits
   {
   if (digitalRead(inputPins[index]) < inputTripped[index] ) //circuit closed for first time
    { delay(50); }  // delay and read again to eliminate contact bounce
   if (digitalRead(inputPins[index]) < inputTripped[index] ) // second read of same pin
   {
    inputTripped[index] = 0;     //record input circuit as tripped
    if (tripNumber==2)           //3rd trip power output 3
    {
     digitalWrite(ALARM_PIN3, HIGH);
     delay(motorRun);
     digitalWrite(ALARM_PIN3, LOW);
     tripNumber = tripNumber+1;
    }
   if (tripNumber==1)           //2nd trip power output 2
    {
     digitalWrite(ALARM_PIN2, HIGH);
     delay(motorRun);
     digitalWrite(ALARM_PIN2, LOW);
     tripNumber = tripNumber+1;
    }  
   if (tripNumber==0)          //1st trip activate servo 1
    {
     servoOut1.write(servoAngle);
     tripNumber = tripNumber+1;
    }  
    }   
   }
  }
}
arduino/dogalarm.txt ยท Last modified: by 127.0.0.1