const int pirPin = 2; // PIR sensor pin
const int buzzerPin = 9; // Buzzer pin (using tone for passive buzzer)
const int ledPin = 13; // Status LED pin
const int buttonPin = 10; // Button pin for arming/disarming
bool theftDetected = false;
bool systemArmed = false; // System state: armed/disarmed
bool buttonState = false; // Tracks button toggle
// Simulated GPS Coordinates
float simulatedLat = 37.7749; // Example latitude
float simulatedLon = -122.4194; // Example longitude
void setup() {
pinMode(pirPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up
digitalWrite(buzzerPin, LOW);
digitalWrite(ledPin, LOW);
Serial.begin(9600); // Initialize Serial Monitor
Serial.println("Anti-Theft Device Initialized");
}
void loop() {
// Handle button input to toggle system state
if (digitalRead(buttonPin) == LOW) { // Button pressed
delay(10); // Debounce delay
buttonState = !buttonState;
systemArmed = buttonState;
// Update status LED
if (systemArmed) {
digitalWrite(ledPin, HIGH);
Serial.println("System Armed");
} else {
digitalWrite(ledPin, LOW);
Serial.println("System Disarmed");
}
delay(20); // Avoid multiple toggles due to button bounce
}
if (systemArmed) {
// Check PIR sensor if system is armed
if (digitalRead(pirPin) == HIGH) {
theftDetected = true;
triggerAlarm();
}
if (theftDetected) {
sendAlert();
delay(10000); // Wait before sending another alert
theftDetected = false;
}
}
delay(500);
}
void triggerAlarm() {
// Start buzzer sound continuously and blink LED
tone(buzzerPin, 1000); // Play sound at 1kHz
for (int i = 0; i < 10; i++) { // Blink LED and sound buzzer 10 times
digitalWrite(ledPin, HIGH);
delay(200); // 200ms ON
digitalWrite(ledPin, LOW);
delay(200); // 200ms OFF
}
// Keep buzzer ON for some time after blinking
delay(3000); // Continue sound for 3 more seconds after LED blinking
noTone(buzzerPin); // Stop buzzer sound after the alarm duration
}
void sendAlert() {
// Simulate GPS and SMS
String message = "Theft detected! Location: ";
message += "Lat: ";
message += simulatedLat;
message += ", Lon: ";
message += simulatedLon;
Serial.println(message);
// Simulate sending SMS
Serial.println("Simulated SMS Sent: " + message);
}