Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Ticker.h>
- #include "U8g2lib.h"
- #include "Wire.h"
- #include <GyverButton.h>
- #include "settings.h"
- #include "WiFiManager.h"
- #include <LittleFS.h>
- #include <FS.h>
- #include "SaveLoadManager.h"
- Ticker blinker;
- WiFiManager wifiManager;
- U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
- GButton extBtn(pinExternalBtn);
- void ICACHE_RAM_ATTR handleInterrupt();
- void ICACHE_RAM_ATTR onTimerISR() {
- digitalWrite(dimPin, HIGH);
- delayMicroseconds(pulseDuration);
- digitalWrite(dimPin, LOW);
- timer1_write(50000); // 10мс
- }
- void ICACHE_RAM_ATTR handleInterrupt() {
- power = 49000 - 475 * dimmerPercent;
- timer1_write(power);
- }
- void setup() {
- Serial.begin(115200);
- delay(1000);
- Serial.println("Старт");
- if (!LittleFS.begin()) {
- Serial.println("Ошибка инициализации LittleFS");
- } else {
- Serial.println("Инициализирован LittleFS");
- }
- pinMode(SENSOR_PIN, INPUT);
- pinMode(interruptPin, INPUT_PULLUP);
- pinMode(dimPin, OUTPUT);
- //attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING);
- timer1_attachInterrupt(onTimerISR);
- timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE);
- extBtn.setDebounce(50);
- extBtn.setTimeout(300);
- extBtn.setClickTimeout(600);
- extBtn.setType(HIGH_PULL);
- extBtn.setDirection(NORM_OPEN);
- wifiManager.begin();
- delay(400);
- u8g2.begin();
- u8g2.enableUTF8Print();
- WelcomeText();
- }
- void loop() {
- wifiManager.handleClient();
- handleButton();
- // === Обработка оборотов ===
- if (motorRunning) {
- bool currentState = digitalRead(SENSOR_PIN);
- if (lastState == HIGH && currentState == LOW) {
- hasEntered = true;
- }
- if (lastState == LOW && currentState == HIGH) {
- if (hasEntered) {
- unsigned long now = millis();
- if (now - lastPulseTime > pulseDebounce) {
- lastPulseTime = now;
- count++;
- Serial.print("Detected marks: ");
- Serial.println(count);
- lengthPerRevolution = (piData * spoolDiameter) / 1000.0f;
- lengthPerRevolution *= (1.0f + correctionCoefficient);
- totalLength = count * lengthPerRevolution;
- DisplayStatus();
- }
- hasEntered = false;
- }
- }
- lastState = currentState;
- }
- // === Автостоп по достижению 20м ===
- if (motorRunning && totalLength >= targetLength) {
- motorRunning = false;
- }
- // === Плавное управление ШИМ ===
- static unsigned long lastUpdate = 0;
- if (millis() - lastUpdate >= updateInterval) {
- lastUpdate = millis();
- if (motorRunning) {
- if (dimmerValue < maxDimmerValue) {
- dimmerValue += dimmerStep;
- if (dimmerValue > maxDimmerValue) dimmerValue = maxDimmerValue;
- }
- } else {
- if (dimmerValue > 0) {
- dimmerValue = 0;
- val = 0;
- DisplayStatus();
- }
- }
- val = dimmerValue;
- dimmerPercent = dimmerValue;
- }
- }
- void DisplayStatus() {
- // === Отображение на дисплее ===
- u8g2.clearBuffer();
- u8g2.setFont(u8g2_font_8x13_t_cyrillic);
- u8g2.setCursor(0, 12);
- u8g2.print("Обороты: ");
- u8g2.print(count);
- u8g2.setCursor(0, 64);
- u8g2.print("T:");
- u8g2.print(maxDimmerValue);
- u8g2.print('%');
- u8g2.setCursor(0, 24);
- u8g2.print("Намотка: ");
- u8g2.print(totalLength, 2);
- u8g2.print("м");
- u8g2.setCursor(36, 48);
- u8g2.print(motorRunning ? "Запущен" : "Остановлен");
- u8g2.sendBuffer();
- }
- void WelcomeText() {
- u8g2.clearBuffer();
- u8g2.setFont(u8g2_font_8x13_t_cyrillic);
- u8g2.setCursor(10, 12);
- u8g2.print("Готов к ");
- u8g2.setCursor(0, 26);
- u8g2.print("использованию");
- u8g2.setCursor(0, 42);
- u8g2.print("Нажмите кнопку");
- u8g2.setCursor(40, 56);
- u8g2.print(" Старт");
- u8g2.sendBuffer();
- }
- void handleButton() {
- extBtn.tick();
- if (extBtn.isClick()) {
- Serial.println("click");
- if (!motorRunning) {
- // Запуск
- startMotor();
- } else {
- // Остановка
- stopMotor();
- }
- }
- }
- void startMotor() {
- motorRunning = true;
- count = 0;
- totalLength = 0;
- dimmerValue = 0;
- dimmerStep = (float)maxDimmerValue / ((float)accelerationTime / (float)updateInterval);
- attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, RISING);
- Serial.println("Motor started");
- DisplayStatus();
- }
- void stopMotor() {
- dimmerValue = 0;
- motorRunning = false;
- detachInterrupt(digitalPinToInterrupt(interruptPin));
- Serial.println("Motor stopped");
- DisplayStatus();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement