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


import serial
import tkinter as tk

try:
    ser = serial.Serial('COM5', 9600, timeout=0.1)
except:
    print("Не удалось подключиться к COM4")
    exit()

def update():
    if ser.in_waiting:
        line = ser.readline().decode().strip()
        if line.isdigit():
            v = int(line)
            lbl.config(text=f"{v//60:02d}:{v%60:02d}")
    root.after(50, update)

def send_command(cmd):
    ser.write(cmd.encode())

root = tk.Tk()
root.title("Таймер")
root.geometry("400x300")

lbl = tk.Label(root, text="00:00", font=("Arial", 60))
lbl.pack(pady=20)

# Фрейм для кнопок
btn_frame = tk.Frame(root)
btn_frame.pack(pady=10)

# Кнопка СТАРТ
tk.Button(btn_frame, text="СТАРТ", command=lambda: send_command('S'),
          bg="green", fg="white", font=("Arial", 14), width=8).pack(side=tk.LEFT, padx=5)

# Кнопка СТОП
tk.Button(btn_frame, text="СТОП", command=lambda: send_command('P'),
          bg="orange", font=("Arial", 14), width=8).pack(side=tk.LEFT, padx=5)

# Кнопка СБРОС
tk.Button(btn_frame, text="СБРОС", command=lambda: send_command('R'),
          bg="red", fg="white", font=("Arial", 14), width=8).pack(side=tk.LEFT, padx=5)

update()
root.mainloop()


#include <QuadDisplay.h>

#define BUTTON_PIN 7
#define LED_PIN 9
#define DISPLAY_PIN 11

int s = 0;
unsigned long t = 0;
bool running = true;  // Флаг: true - таймер идет, false - остановлен

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  displayInt(DISPLAY_PIN, 0);
}

void loop() {
  // Обработка команд с компьютера
  if (Serial.available()) {
    char cmd = Serial.read();
    
    if (cmd == 'R') {  // Сброс
      s = 0;
      running = true;  // После сброса автоматически запускаем
      Serial.println(s);
      displayInt(DISPLAY_PIN, 0);
      digitalWrite(LED_PIN, LOW);
      t = millis();
    }
    else if (cmd == 'S') {  // Старт
      running = true;
      t = millis();  // Синхронизируем время
    }
    else if (cmd == 'P') {  // Пауза (Стоп)
      running = false;
    }
  }
  
  // Физическая кнопка (работает как сброс)
  if (digitalRead(BUTTON_PIN) == LOW) {
    s = 0;
    running = true;
    Serial.println(s);
    displayInt(DISPLAY_PIN, 0);
    digitalWrite(LED_PIN, LOW);
    delay(500);
    t = millis();
  }
  
  // Управление светодиодом
  if (s > 0 && running) {
    digitalWrite(LED_PIN, HIGH);
  } else if (!running || s == 0) {
    digitalWrite(LED_PIN, LOW);
  }
  
  // Отсчет времени (только если таймер запущен)
  if (running && millis() - t >= 1000) {
    t = millis();
    s++;
    Serial.println(s);
    displayInt(DISPLAY_PIN, (s / 60) * 100 + (s % 60));
  }
}