#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Servo.h>
#include <NewPing.h>
#include <math.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 2
#define ECHO_PIN 3
#define MAX_DISTANCE 200
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
Servo radarServo;
#define SERVO_PIN 9
const int radarCenterX = 64;
const int radarCenterY = 60;
const int radarRadius = 50;
int angle = 0;
// مصفوفة لتخزين نقاط الأجسام
#define MAX_OBJECTS 50
int objX[MAX_OBJECTS];
int objY[MAX_OBJECTS];
int objCount = 0;
void setup() {
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for(;;);
}
display.clearDisplay();
radarServo.attach(SERVO_PIN);
// رسالة ترحيبية
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10,10);
display.println("مرحبا بكم");
display.println("Initializing...");
display.display();
delay(2000);
display.clearDisplay();
drawRadarBase();
}
void loop() {
// Sweep 0 -> 180
for(angle = 0; angle <= 180; angle += 5) {
radarServo.write(angle);
delay(80);
unsigned int distance = sonar.ping_cm();
if(distance == 0) distance = MAX_DISTANCE;
int x = radarCenterX + (int)(distance * radarRadius / MAX_DISTANCE * cos(radians(angle)));
int y = radarCenterY - (int)(distance * radarRadius / MAX_DISTANCE * sin(radians(angle)));
// حفظ النقاط المكتشفة
if(objCount < MAX_OBJECTS) {
objX[objCount] = x;
objY[objCount] = y;
objCount++;
}
drawRadarBase();
drawObjects();
drawRadarSweep(angle);
display.display();
}
// Sweep 180 -> 0
for(angle = 180; angle >= 0; angle -= 5) {
radarServo.write(angle);
delay(80);
unsigned int distance = sonar.ping_cm();
if(distance == 0) distance = MAX_DISTANCE;
int x = radarCenterX + (int)(distance * radarRadius / MAX_DISTANCE * cos(radians(angle)));
int y = radarCenterY - (int)(distance * radarRadius / MAX_DISTANCE * sin(radians(angle)));
if(objCount < MAX_OBJECTS) {
objX[objCount] = x;
objY[objCount] = y;
objCount++;
}
drawRadarBase();
drawObjects();
drawRadarSweep(angle);
display.display();
}
}
// رسم نصف دائرة الرادار
void drawRadarBase() {
display.clearDisplay();
// نصف دائرة
for(int a=0; a<=180; a+=2){
int x = radarCenterX + (int)(radarRadius * cos(radians(a)));
int y = radarCenterY - (int)(radarRadius * sin(radians(a)));
display.drawPixel(x, y, SSD1306_WHITE);
}
// مركز الرادار
display.fillCircle(radarCenterX, radarCenterY, 2, SSD1306_WHITE);
}
// رسم كل الأجسام المكتشفة
void drawObjects() {
for(int i=0; i<objCount; i++){
display.fillCircle(objX[i], objY[i], 2, SSD1306_WHITE);
}
}
// رسم شعاع الرادار متدرج الشفافية
void drawRadarSweep(int angle){
// نرسم عدة خطوط متدرجة (أقرب إلى السينما)
for(int i=0; i<5; i++){
float alpha = (5 - i) * 50; // تأثير تدرج
int a = angle - i*2;
if(a < 0) a = 0;
int x = radarCenterX + (int)(radarRadius * cos(radians(a)));
int y = radarCenterY - (int)(radarRadius * sin(radians(a)));
display.drawLine(radarCenterX, radarCenterY, x, y, SSD1306_WHITE);
}
}
Loading
ssd1306
ssd1306