CODE
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define TRIG_PIN 9
#define ECHO_PIN 10
#define SERVO_PIN 11
#define BUZZER_PIN 12
#define LED_PIN 13
#define RELAY_PIN 8 // ⚡ Relay added on pin 8
Servo servo;
const int maxDistance = 50;
const int centerX = SCREEN_WIDTH / 2;
const int centerY = SCREEN_HEIGHT - 1;
const int maxRadius = 50;
bool objectDetected = false;
int currentAngle = 0;
int currentDistance = 0;
unsigned long lastServoMove = 0;
unsigned long lastMeasurement = 0;
unsigned long lastUpdate = 0;
unsigned long lastToggle = 0;
bool toggleState = false;
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED_PIN, LOW);
digitalWrite(RELAY_PIN, HIGH);
servo.attach(SERVO_PIN);
servo.write(0);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for (;;);
}
display.clearDisplay();
display.display();
}
float measureDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH, 30000);
if (duration == 0) return maxDistance;
float distance = duration * 0.034 / 2;
return (distance > maxDistance) ? maxDistance : distance;
}
void handleBuzzerLEDRelay() {
unsigned long currentTime = millis();
if (objectDetected) {
if (currentTime - lastToggle >= 500) {
toggleState = !toggleState;
digitalWrite(BUZZER_PIN, toggleState);
digitalWrite(LED_PIN, toggleState);
lastToggle = currentTime;
}
digitalWrite(RELAY_PIN, LOW);
} else {
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED_PIN, LOW);
digitalWrite(RELAY_PIN, HIGH);
toggleState = false;
}
}
void drawRadar(int angle, int distance) {
display.clearDisplay();
// Draw circles
display.drawCircle(centerX, centerY, maxRadius / 2, SSD1306_WHITE);
display.drawCircle(centerX, centerY, maxRadius, SSD1306_WHITE);
// Sweep line
int sweepX = centerX + maxRadius * cos(radians(angle));
int sweepY = centerY - maxRadius * sin(radians(angle));
display.drawLine(centerX, centerY, sweepX, sweepY, SSD1306_WHITE);
// Draw detected object if within range
if (distance < maxDistance) {
int objX = centerX + (distance * maxRadius / maxDistance) * cos(radians(angle));
int objY = centerY - (distance * maxRadius / maxDistance) * sin(radians(angle));
display.fillCircle(objX, objY, 3, SSD1306_WHITE);
}
// Show info
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Angle:");
display.print(angle);
display.setCursor(70, 0);
display.print("Dist:");
display.print(distance);
display.print("cm");
display.display();
}
void loop() {
static int angle = 0;
static bool forward = true;
unsigned long currentTime = millis();
// Measure distance every 100ms
if (currentTime - lastMeasurement >= 100) {
currentDistance = (int)measureDistance();
objectDetected = (currentDistance < maxDistance);
lastMeasurement = currentTime;
}
handleBuzzerLEDRelay();
// If object detected → stop servo
if (objectDetected) {
// Keep showing frozen display
drawRadar(angle, currentDistance);
return;
}
// If no object → resume servo sweep
if (currentTime - lastServoMove >= 25) {
servo.write(angle);
currentAngle = angle;
if (forward) {
angle += 2;
if (angle >= 180) {
angle = 180;
forward = false;
}
} else {
angle -= 2;
if (angle <= 0) {
angle = 0;
forward = true;
}
}
lastServoMove = currentTime;
}
// Update display normally
if (currentTime - lastUpdate >= 33) {
drawRadar(angle, currentDistance);
lastUpdate = currentTime;
}
}
