#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
// Pin Definitions
#define TRIGGER_PIN 14
#define ECHO_PIN 27
#define RELAY_PIN 26
#define BUZZER_PIN 25
// OLED setup
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Timing variables
unsigned long previousMillisUltrasonic = 0;
unsigned long previousMillisRelay = 0;
unsigned long previousMillisDisplay = 0;
unsigned long relayTimer = 0;
const long ultrasonicInterval = 500; // 500 ms for ultrasonic sensor
const long relayTimeout = 5000; // 5 seconds timeout to close door
const long displayInterval = 200; // 200 ms for OLED update
bool relayState = false; // Relay state (open/close door)
float distance = 0;
void setup() {
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Initial state: door closed
digitalWrite(BUZZER_PIN, LOW);
// OLED initialization
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Initializing...");
display.display();
Serial.begin(115200);
}
void loop() {
unsigned long currentMillis = millis();
// Ultrasonic sensor check
if (currentMillis - previousMillisUltrasonic >= ultrasonicInterval) {
previousMillisUltrasonic = currentMillis;
distance = getDistance(); // Function to calculate distance
if (distance > 0 && distance <= 5) {
// If object is too close, sound the buzzer
digitalWrite(BUZZER_PIN, HIGH);
} else {
digitalWrite(BUZZER_PIN, LOW);
}
// Control relay based on distance
if (distance > 0 && distance < 10) {
digitalWrite(RELAY_PIN, HIGH); // Open door
relayState = true;
relayTimer = currentMillis; // Reset the timer for relay
}
}
// Relay control: close after 5 seconds if no object
if (relayState && currentMillis - relayTimer >= relayTimeout) {
digitalWrite(RELAY_PIN, LOW); // Close door
relayState = false;
}
// OLED update
if (currentMillis - previousMillisDisplay >= displayInterval) {
previousMillisDisplay = currentMillis;
display.clearDisplay();
if (distance > 0) {
display.setCursor(0, 0);
display.print("Distance: ");
display.print(distance);
display.println(" cm");
} else {
display.setCursor(0, 0);
display.println("Waiting...");
}
if (relayState) {
display.setCursor(0, 20);
display.println("Pintu Terbuka");
} else {
display.setCursor(0, 20);
display.println("Pintu Tertutup");
}
display.display();
}
}
// Function to calculate distance from ultrasonic sensor
float getDistance() {
// Send a 10us pulse to the trigger pin
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
// Read the echo pin and calculate distance
long duration = pulseIn(ECHO_PIN, HIGH);
float distance = (duration * 0.034) / 2; // Speed of sound 343 m/s -> 0.034 cm/us
if (distance >= 400 || distance <= 0) {
return -1; // Invalid distance
} else {
return distance;
}
}