Загрузка данных


import tkinter as tk
import serial

# Настройка порта (замените 'COM3' на ваш порт)
try:
    ser = serial.Serial('COM3', 9600, timeout=1)
except:
    print("Ошибка подключения к порту")

def send(cmd):
    if 'ser' in globals() and ser.is_open:
        ser.write(cmd.encode())

root = tk.Tk()
root.title("Control")

# Кнопки сервопривода
tk.Label(root, text="Сервопривод").grid(row=0, column=1)
tk.Button(root, text="Лево", command=lambda: send('L')).grid(row=1, column=0)
tk.Button(root, text="Прямо", command=lambda: send('C')).grid(row=1, column=1)
tk.Button(root, text="Право", command=lambda: send('R')).grid(row=1, column=2)

# Кнопки мотора
tk.Label(root, text="Шаговый мотор").grid(row=2, column=1)
tk.Button(root, text="Стоп", command=lambda: send('0')).grid(row=3, column=0)
tk.Button(root, text="Скор 1", command=lambda: send('1')).grid(row=3, column=1)
tk.Button(root, text="Скор 2", command=lambda: send('2')).grid(row=3, column=2)
tk.Button(root, text="Скор 3", command=lambda: send('3')).grid(row=4, column=1)

root.mainloop()



#include <Servo.h> // Встроенная библиотека

Servo servo;
const int pins[4] = {8, 9, 10, 11}; // Пины мотора (например, ULN2003)
int motorSpeed = 0; // Задержка между шагами в мс (0 - стоп)
int stepIdx = 0;

void setup() {
  Serial.begin(9600);
  servo.attach(6);
  servo.write(90);
  for (int i = 0; i < 4; i++) pinMode(pins[i], OUTPUT);
}

void loop() {
  if (Serial.available() > 0) {
    char cmd = Serial.read();
    if (cmd == 'L') servo.write(45);
    if (cmd == 'C') servo.write(90);
    if (cmd == 'R') servo.write(135);
    
    // Меняем задержку: чем она меньше, тем быстрее крутится мотор
    if (cmd == '0') motorSpeed = 0;
    if (cmd == '1') motorSpeed = 15; // Медленно
    if (cmd == '2') motorSpeed = 8;  // Средне
    if (cmd == '3') motorSpeed = 3;  // Быстро
  }

  // Шагаем без использования delay(), чтобы не тормозить прием Serial
  if (motorSpeed > 0) {
    static unsigned long lastStep = 0;
    if (millis() - lastStep >= motorSpeed) {
      lastStep = millis();
      
      // Полношаговый режим (управление 4 фазами)
      for (int i = 0; i < 4; i++) {
        digitalWrite(pins[i], (stepIdx == i));
      }
      stepIdx = (stepIdx + 1) % 4;
    }
  } else {
    // Отключаем ток на обмотках при стопе, чтобы мотор не грелся
    for (int i = 0; i < 4; i++) digitalWrite(pins[i], LOW);
  }
}