Untitled
unknown
c_cpp
2 years ago
4.4 kB
8
Indexable
#include<LiquidCrystal.h>
#include<Servo.h>
/* Initialize pins for Ultrasonic Distance Sensor */
const int trig_pin = 3;
const int echo_pin = 12;
/* Initialize Servo and Position */
Servo servo_9;
int pos = 0;
/* Initialize LCD display */
LiquidCrystal LCD(8, 2, 7, 6, 5, 4);
/* Variables for Ultrasonic Sensor */
long pulse_duration;
int distance_cm;
/* Initialize Button and Buzzer */
int buzzerPin = 13;
int buttonPin = 10;
int count = 0;
int buttonState = 0; //Button press is 1 and opposite is 0
int lastButtonState = 0; //Button press is 1 and opposite is 0
bool isInSecurityMode = false;
unsigned long buttonPressStartTime = 0;
unsigned long holdTime = 3000;
/* Security Feature */
unsigned long warningTime = 10000;
unsigned long startTime = 0;
void setup() {
/* Setup Servo */
servo_9.attach(9, 500, 2500);
/* Setup LCD to pins 8 to 2 */
LCD.begin(8, 2);
/* Setup I/O for Ultrasonic Sensor */
pinMode(trig_pin, OUTPUT);
pinMode(echo_pin, INPUT);
/* Set I/O for Buzzer */
pinMode(buzzerPin, OUTPUT);
/* Set I for Button */
pinMode(buttonPin, INPUT);
}
void loop(){
swap_mode();
}
void swap_mode(){
int readingState = digitalRead(buttonPin);
/*when press then counting time*/
if (readingState != lastButtonState) {
buttonPressStartTime = millis();
}
/*If press button in 3 second then restart counting time*/
if (readingState == 1 && (millis() - buttonPressStartTime) >= holdTime) {
isInSecurityMode = !isInSecurityMode; // set variable to TRUE
buttonPressStartTime = millis();
}
if (isInSecurityMode) { // if isInSecurity is TRUE then calling security mode else normal mode is enable
security_mode();
} else {
normal_mode();
}
lastButtonState = reading;
}
void security_mode(){
servo_9.write(0); // Close the door if in normal mode still open
Init_trig_echo();
shop_close();
distance_cm = (pulse_duration*0.034)/2; //calculate distances is "cm"
if(distance_cm < 30){
warning();
if (startTime == 0) {
startTime = millis();
}
if(millis() - startTime >= warningTime){ // calculate time and compare with warning time.
int count = 0;
buzzer_power_on(count);
count ++;
imposter();
buzzer_power_on(count);
delay(5000); // delay 5s each session
startTime = 0; // Restart the timer system
}
}else{
startTime = 0;
buzzer_power_off();
see_you_again();
}
delay(1000);
}
void normal_mode(){
Init_trig_echo();
shop_open();
LCD.setCursor(0,1);
LCD.print("DOOR: "); // line 1 will display "DOOR: "
distance_cm = (pulse_duration*0.034)/2; // calculate distance are "cm"
if(distance_cm < 60){
LCD.print(" OPEN "); // and concat with "DOOR: "
servo_9.write(170); // servo will turn on with 170* when distance < 60
delay(1000); // delay 1s to wait object through
}else{
LCD.print(" CLOSE ");
servo_9.write(0); // servo will turn off with 0* when distance < 60
}
}
/*Init the pins of trig and echo*/
void Init_trig_echo(){
digitalWrite(trig_pin, LOW);
delayMicroseconds(5);
digitalWrite(trig_pin, HIGH);
delayMicroseconds(10);
digitalWrite(trig_pin,LOW);
pulse_duration=pulseIn(echo_pin, HIGH);
}
/*when buzzer_power_on ended*/
void buzzer_power_off(){
digitalWrite(buzzerPin, LOW);
}
/*Called when the object stands in the alarm for more than 10 seconds*/
void buzzer_power_on(int count){
int n;
if(count == 1){
n = 15;
}else{
n = 3;
}
int duration = 200;
for (int i = 0; i < n; i++) {
tone(buzzerPin, 1000);
delay(duration);
noTone(buzzerPin);
delay(duration);
}
noTone(buzzerPin);
}
/*Message for Shop in Security mode of line 2*/
void see_you_again(){
LCD.setCursor(0,1);
LCD.print(" SEE YOU AGAIN! ");
}
/* Message for warning when too close > 3 second */
void imposter(){
LCD.setCursor(0,1);
LCD.print(" HAVE IMPOSTERS ");
}
/*Message for warning when too close*/
void warning(){
LCD.setCursor(0,1);
LCD.print("Warn: Too Close");
}
/*Message for Shop Close*/
void shop_close(){
LCD.setCursor(0,0);
LCD.print(" GRP5 Shop CLOSE");
}
/*Message for Shop Open*/
void shop_open(){
LCD.setCursor(0,0);
LCD.print(" GRP5 Shop OPEN ");
}
Editor is loading...
Leave a Comment