#include <Servo.h>
const int pirPin = 2; // PIR sensor input pin
const int redLEDPin = 3; // Red LED pin (restroom off-limits)
const int greenLEDPin = 4; // Green LED pin (restroom available)
const int whiteLEDPin = 5; // White LED (lamp) pin
const int redLEDLowSanPin = 6; // Red LED pin (sanitizer low level)
const int greenLEDFullSanPin = 7; // Green LED pin (sanitizer full level)
const int switchPin = 8; // Slide switch pin (simulating water level sensor)
const int servoPin = 9; // Servo motor pin
const int trigPin = 10; // Ultrasonic sensor trig pin
const int echoPin = 11; // Ultrasonic sensor echo pin
Servo servoMotor;
void setup() {
pinMode(pirPin, INPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(whiteLEDPin, OUTPUT);
pinMode(redLEDLowSanPin, OUTPUT);
pinMode(greenLEDFullSanPin, OUTPUT);
pinMode(switchPin, INPUT_PULLUP); // Enable internal pull-up resistor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servoMotor.attach(servoPin);
digitalWrite(redLEDPin, LOW);
digitalWrite(greenLEDPin, HIGH);
digitalWrite(whiteLEDPin, LOW);
digitalWrite(redLEDLowSanPin, LOW);
digitalWrite(greenLEDFullSanPin, LOW);
servoMotor.write(0); // Initial position of the servo motor
}
void loop() {
// PIR Sensor Handling
int pirState = digitalRead(pirPin);
if (pirState == HIGH) { // Motion detected
digitalWrite(whiteLEDPin, HIGH); // Turn on the lamp
digitalWrite(redLEDPin, LOW); // Ensure the red LED is off
digitalWrite(greenLEDPin, LOW); // Ensure the green LED is off
delay(4000); // Simulate person using the restroom
// Person exits, turn off the lamp and start sanitation process
digitalWrite(whiteLEDPin, LOW);
digitalWrite(redLEDPin, HIGH); // Indicate sanitation in progress
digitalWrite(greenLEDPin, LOW); // Ensure the green LED is off
// Simulate sanitizing process
servoMotor.write(90); // Rotate servo to spray sanitizer
delay(4000); // Wait for 5 seconds
servoMotor.write(0); // Rotate servo back to initial position
// End of sanitizing process
digitalWrite(redLEDPin, LOW); // Turn off the red LED
digitalWrite(greenLEDPin, HIGH); // Indicate restroom is available
}
// Slide Switch Handling for Sanitizer Level
int switchState = digitalRead(switchPin);
if (switchState == LOW) { // Switch is pressed (simulating low sanitizer level)
digitalWrite(redLEDLowSanPin, HIGH); // Indicate low sanitizer level
digitalWrite(greenLEDFullSanPin, LOW); // Ensure full level LED is off
} else {
digitalWrite(redLEDLowSanPin, LOW); // Turn off low level LED
}
// Ultrasonic Sensor Handling
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1; // Convert to cm
if (distance < 15) { // Adjust threshold to 15 cm
digitalWrite(greenLEDFullSanPin, HIGH); // Indicate sanitizer is full
} else {
digitalWrite(greenLEDFullSanPin, LOW); // Turn off full level LED
}
}