// Global Varialables
int pirPin = 3; // PIR connected to A3
int RledPin = 13; // red LED connected to pin 13
int GledPin = 12; // green LED connected to in 12
int val = 0; // Varialbe for reading PIR #
unsigned long time = 0; // Variable to count time
void setup()
{
Serial.begin(9600);
pinMode(pirPin, INPUT); // PIR input
pinMode(RledPin, OUTPUT); // LED output
pinMode(GledPin, OUTPUT); // LED output
// The PIR sensor needs approx 30 seconds to calibrate before use
Serial.print("Loading");
for(int i = 0; i < 31; i++)
{
Serial.print(".");
delay(1000);
}
Serial.println("Complete");
Serial.println("SENSOR ACTIVE"); // The PIR sensor is now calibrated and ready to use
delay(50);
}
void loop()
{
// millis() is a function that starts counting how long the program started in milli seconds [can count to approx 50 days]
time = millis(); // assign variable time equal to millis()
time = time/1000; // divide time by 1000 to give an answer in seconds
val = (analogRead(pirPin)); // Assign variable val equal to the same value as the PIR
if(val < 1000) // the PIR gives unreliable reading's thus if it is any less than 1000 do the following...
{
digitalWrite(RledPin, HIGH); // red LED HIGH
digitalWrite(GledPin, LOW); // green LED LOW
Serial.println("Motion Detected at:"); // print that motion was detected and when
Serial.println(time);
Serial.println("Seconds Since Start of Program");
tone(10, 2200); // 2.2KHz sent to the PEIZO buzzer connected to PWM pin 10
delay(5000); // Sound the alarm for 5 seconds
}
if(val == 1023) // if PIR doesn't detect motion then it gives a HIGH
{
digitalWrite(RledPin, LOW); // red LED LOW
digitalWrite(GledPin, HIGH); // green LED HIGH
noTone(10); // if no motion is being detected then turn off the PEIZO buzzer
}
}
« Last Edit: Today at 11:20:57 pm by RandyLeahy »