#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(cmd):
serial_port.write(cmd.encode())
def update():
while serial_port.in_waiting:
line = serial_port.readline().decode().strip()
if line.startswith("Distance:"):
dist_label.config(text=line.split(":")[1] + " см")
root.after(50, update)
def manual():
send('M')
mode_label.config(text="Ручной")
def auto():
send('A')
mode_label.config(text="Автомат")
root = tk.Tk()
root.title("Управление дроном")
root.geometry("350x450")
tk.Label(root, text="Расстояние до препятствия:").pack(pady=(10,0))
dist_label = tk.Label(root, text="0 см", font=("Arial", 20))
dist_label.pack()
mode_frame = tk.Frame(root)
mode_frame.pack(pady=10)
tk.Button(mode_frame, text="Ручной режим", command=manual, width=12).pack(side="left", padx=5)
tk.Button(mode_frame, text="Автомат режим", command=auto, width=12).pack(side="left", padx=5)
mode_label = tk.Label(root, text="Ручной")
mode_label.pack()
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"))
btn_frame = tk.Frame(root)
btn_frame.pack(pady=20)
tk.Button(btn_frame, image=img_up, command=lambda: send('U'), bd=0).grid(row=0, column=1, padx=5, pady=5)
tk.Button(btn_frame, image=img_down, command=lambda: send('D'), bd=0).grid(row=1, column=1, padx=5, pady=5)
tk.Button(btn_frame, image=img_left, command=lambda: send('L'), bd=0).grid(row=1, column=0, padx=5, pady=5)
tk.Button(btn_frame, image=img_right, command=lambda: send('R'), bd=0).grid(row=1, column=2, padx=5, pady=5)
root.after(100, update)
root.mainloop()