#include <Servo.h>
Servo servo1;
Servo servo2;
int button1Left = 2;
int button1Right = 3;
int button2Left = 4;
int button2Right = 5;
int pos1 = 90;
int pos2 = 90;
void setup() {
servo1.attach(6);
servo2.attach(7);
pinMode(button1Left, INPUT_PULLUP);
pinMode(button1Right, INPUT_PULLUP);
pinMode(button2Left, INPUT_PULLUP);
pinMode(button2Right, INPUT_PULLUP);
servo1.write(pos1);
servo2.write(pos2);
}
void loop() {
if (digitalRead(button1Left) == LOW) {
pos1 = pos1 - 5;
if (pos1 < 0) pos1 = 0;
servo1.write(pos1);
delay(50);
}
if (digitalRead(button1Right) == LOW) {
pos1 = pos1 + 5;
if (pos1 > 180) pos1 = 180;
servo1.write(pos1);
delay(50);
}
if (digitalRead(button2Left) == LOW) {
pos2 = pos2 - 5;
if (pos2 < 0) pos2 = 0;
servo2.write(pos2);
delay(50);
}
if (digitalRead(button2Right) == LOW) {
pos2 = pos2 + 5;
if (pos2 > 180) pos2 = 180;
servo2.write(pos2);
delay(50);
}
}
————-
import serial
import tkinter as tk
from tkinter import PhotoImage
import time
arduino = None
try:
arduino = serial.Serial('COM3', 9600, timeout=1)
time.sleep(2)
except:
print("Arduino не найден")
def send(cmd):
if arduino:
arduino.write(cmd.encode())
root = tk.Tk()
root.title("Servo Control")
# Загрузка изображений (нужно заменить пути на свои)
# Поддерживаются форматы .png, .gif (но не .jpg стандартным Tkinter)
# Для .jpg нужна библиотека PIL (Pillow)
try:
# Способ 1:直接用 PhotoImage для PNG/GIF
img_left1 = tk.PhotoImage(file="left_arrow.png") # замените на свой файл
img_right1 = tk.PhotoImage(file="right_arrow.png")
img_left2 = tk.PhotoImage(file="left_arrow2.png")
img_right2 = tk.PhotoImage(file="right_arrow2.png")
# Кнопки с картинками
tk.Button(root, text="Серво1 ВЛЕВО", image=img_left1, compound=tk.LEFT,
command=lambda: send("1L")).pack(pady=5)
tk.Button(root, text="Серво1 ВПРАВО", image=img_right1, compound=tk.LEFT,
command=lambda: send("1R")).pack(pady=5)
tk.Button(root, text="Серво2 ВЛЕВО", image=img_left2, compound=tk.LEFT,
command=lambda: send("2L")).pack(pady=5)
tk.Button(root, text="Серво2 ВПРАВО", image=img_right2, compound=tk.LEFT,
command=lambda: send("2R")).pack(pady=5)
except:
print("Не удалось загрузить изображения, использую обычные кнопки")
tk.Button(root, text="Серво1 ВЛЕВО", command=lambda: send("1L")).pack(pady=5)
tk.Button(root, text="Серво1 ВПРАВО", command=lambda: send("1R")).pack(pady=5)
tk.Button(root, text="Серво2 ВЛЕВО", command=lambda: send("2L")).pack(pady=5)
tk.Button(root, text="Серво2 ВПРАВО", command=lambda: send("2R")).pack(pady=5)
root.mainloop()