Arduino code for mobile anti theft device
Arduino Code
#include <LiquidCrystal.h>
const int rs = 12; // Register Select pin of LCD
const int en = 11; // Enable pin of LCD
const int d4 = 5; // Data pin 4 of LCD
const int d5 = 4; // Data pin 5 of LCD
const int d6 = 3; // Data pin 6 of LCD
const int d7 = 2; // Data pin 7 of LCD
const int buttonPin = 7; // Pin for the push button
const int buzzerPin = 8; // Pin for the buzzer
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int buttonState = 0; // Variable for storing button state
bool isMobileSafe = false; // Flag to track if the mobile is safe
bool buzzerPlaying = false; // Flag to track if the buzzer is playing
void setup() {
lcd.begin(16, 2); // Initialize the LCD
pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as input with internal pull-up resistor
pinMode(buzzerPin, OUTPUT);
lcd.print("Made By Sufian");
delay(1000);
}
void loop() {
buttonState = digitalRead(buttonPin); // Read the button state
if (buttonState == LOW) { // Button is pressed
if (!isMobileSafe) { // Check if mobile is not safe
if (buzzerPlaying) { // If the buzzer was playing, stop it
noTone(buzzerPin);
buzzerPlaying = false;
}
lcd.clear(); // Clear the LCD
lcd.setCursor(0, 0); // Set the cursor to the beginning of the first line
lcd.print("Mobile is safe");
isMobileSafe = true; // Set the flag to indicate the mobile is safe
}
} else { // Button is not pressed
if (isMobileSafe) { // Mobile is safe
lcd.clear(); // Clear the LCD
lcd.setCursor(0, 0); // Set the cursor to the beginning of the first line
lcd.print("Mobile Taken");
isMobileSafe = false; // Reset the flag
if (!buzzerPlaying) { // If the buzzer wasn't playing, start it with a high frequency
tone(buzzerPin, 2000); // Adjust the frequency as needed for a high-pitched sound
buzzerPlaying = true;
}
}
}
}
Comments
Post a Comment