#include <Servo.h>
#include <Stepper.h>
Servo servo;
const int stepsPerRevolution = 2048;
Stepper motor(stepsPerRevolution, 8, 9, 10, 11);
const int trig = 12;
const int echo = 13;
int rotationAngle = 90;
char mode = 'M';
void setup() {
Serial.begin(9600);
servo.attach(6);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
servo.write(rotationAngle);
motor.setSpeed(15);
}
void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
long duration = pulseIn(echo, HIGH);
int distance = constrain(duration * 0.034 / 2, 0, 150);
Serial.print("Distance:");
Serial.println(distance);
if (Serial.available()) {
char command = Serial.read();
if (command == 'M') mode = 'M';
if (command == 'A') mode = 'A';
if (mode == 'M') {
if (command == 'U') motor.step(100);
if (command == 'D') motor.step(-100);
if (command == 'L') {
rotationAngle = max(0, rotationAngle - 15);
servo.write(rotationAngle);
}
if (command == 'R') {
rotationAngle = min(180, rotationAngle + 15);
servo.write(rotationAngle);
}
}
}
delay(50);
}
import serial
import tkinter as tk
from PIL import Image, ImageTk
serial_port = serial.Serial('COM3', 9600, timeout=0.01)
def send_command(command):
serial_port.write(command.encode())
def update_distance():
while serial_port.in_waiting:
line = serial_port.readline().decode().strip()
if line.startswith("Distance:"):
distance_value = line.split(":")[1]
distance_label.config(text=distance_value + " см")
root.after(50, update_distance)
def set_manual():
send_command('M')
mode_status.config(text="Ручной")
def set_auto():
send_command('A')
mode_status.config(text="Автомат")
root = tk.Tk()
root.title("Управление дроном")
root.geometry("350x450")
tk.Label(root, text="Расстояние до препятствия:").pack(pady=(10,0))
distance_label = tk.Label(root, text="0 см", font=("Arial", 20))
distance_label.pack()
mode_frame = tk.Frame(root)
mode_frame.pack(pady=10)
tk.Button(mode_frame, text="Ручной режим", command=set_manual, width=12).pack(side="left", padx=5)
tk.Button(mode_frame, text="Автомат режим", command=set_auto, width=12).pack(side="left", padx=5)
mode_status = tk.Label(root, text="Ручной")
mode_status.pack()
try:
img_up = ImageTk.PhotoImage(Image.open("вверх.png"))
img_down = ImageTk.PhotoImage(Image.open("вниз.png"))
img_left = ImageTk.PhotoImage(Image.open("влево.png"))
img_right = ImageTk.PhotoImage(Image.open("вправо.png"))
except:
img_up = img_down = img_left = img_right = None
buttons_frame = tk.Frame(root)
buttons_frame.pack(pady=20)
btn_up = tk.Button(buttons_frame, text="⬆" if img_up is None else "", image=img_up, command=lambda: send_command('U'), width=8)
btn_up.grid(row=0, column=1, padx=5, pady=5)
btn_down = tk.Button(buttons_frame, text="⬇" if img_down is None else "", image=img_down, command=lambda: send_command('D'), width=8)
btn_down.grid(row=1, column=1, padx=5, pady=5)
btn_left = tk.Button(buttons_frame, text="⬅" if img_left is None else "", image=img_left, command=lambda: send_command('L'), width=8)
btn_left.grid(row=1, column=0, padx=5, pady=5)
btn_right = tk.Button(buttons_frame, text="➡" if img_right is None else "", image=img_right, command=lambda: send_command('R'), width=8)
btn_right.grid(row=1, column=2, padx=5, pady=5)
root.after(100, update_distance)
root.mainloop()