Advertisement
ces-engine

AC_PWM

Jul 9th, 2025
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.78 KB | None | 0 0
  1. #include <Ticker.h>
  2. #include "U8g2lib.h"
  3. #include "Wire.h"
  4. #include <GyverButton.h>
  5. #include "settings.h"
  6. #include "WiFiManager.h"      
  7. #include <LittleFS.h>        
  8. #include <FS.h>              
  9. #include "SaveLoadManager.h"  
  10. Ticker blinker;
  11. WiFiManager wifiManager;
  12.  
  13. U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
  14.  
  15. GButton extBtn(pinExternalBtn);
  16.  
  17. void ICACHE_RAM_ATTR handleInterrupt();
  18.  
  19. void ICACHE_RAM_ATTR onTimerISR() {
  20.   digitalWrite(dimPin, HIGH);
  21.   delayMicroseconds(pulseDuration);
  22.   digitalWrite(dimPin, LOW);
  23.   timer1_write(50000);  // 10мс
  24. }
  25.  
  26. void ICACHE_RAM_ATTR handleInterrupt() {
  27.   power = 49000 - 475 * dimmerPercent;
  28.   timer1_write(power);
  29. }
  30.  
  31. void setup() {
  32.   Serial.begin(115200);
  33.   delay(1000);
  34.   Serial.println("Старт");
  35.   if (!LittleFS.begin()) {
  36.     Serial.println("Ошибка инициализации LittleFS");
  37.   } else {
  38.     Serial.println("Инициализирован LittleFS");
  39.   }
  40.   pinMode(SENSOR_PIN, INPUT);
  41.   pinMode(interruptPin, INPUT_PULLUP);
  42.   pinMode(dimPin, OUTPUT);
  43.  
  44.  
  45.   //attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING);
  46.  
  47.   timer1_attachInterrupt(onTimerISR);
  48.   timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE);
  49.  
  50.  
  51.   extBtn.setDebounce(50);
  52.   extBtn.setTimeout(300);
  53.   extBtn.setClickTimeout(600);
  54.   extBtn.setType(HIGH_PULL);
  55.   extBtn.setDirection(NORM_OPEN);
  56.  
  57.   wifiManager.begin();
  58.   delay(400);
  59.  
  60.  
  61.   u8g2.begin();
  62.   u8g2.enableUTF8Print();
  63.   WelcomeText();
  64. }
  65.  
  66.  
  67. void loop() {
  68.   wifiManager.handleClient();
  69.   handleButton();
  70.   // === Обработка оборотов ===
  71.   if (motorRunning) {
  72.     bool currentState = digitalRead(SENSOR_PIN);
  73.     if (lastState == HIGH && currentState == LOW) {
  74.       hasEntered = true;
  75.     }
  76.     if (lastState == LOW && currentState == HIGH) {
  77.       if (hasEntered) {
  78.         unsigned long now = millis();
  79.         if (now - lastPulseTime > pulseDebounce) {
  80.           lastPulseTime = now;
  81.           count++;
  82.           Serial.print("Detected marks: ");
  83.           Serial.println(count);
  84.           lengthPerRevolution = (piData * spoolDiameter) / 1000.0f;  
  85.           lengthPerRevolution *= (1.0f + correctionCoefficient);    
  86.           totalLength = count * lengthPerRevolution;                
  87.           DisplayStatus();
  88.         }
  89.         hasEntered = false;
  90.       }
  91.     }
  92.     lastState = currentState;
  93.   }
  94.  
  95.  
  96.   // === Автостоп по достижению 20м ===
  97.   if (motorRunning && totalLength >= targetLength) {
  98.     motorRunning = false;
  99.   }
  100.  
  101.  
  102.   // === Плавное управление ШИМ ===
  103.  
  104.   static unsigned long lastUpdate = 0;
  105.  
  106.   if (millis() - lastUpdate >= updateInterval) {
  107.     lastUpdate = millis();
  108.  
  109.     if (motorRunning) {
  110.       if (dimmerValue < maxDimmerValue) {
  111.         dimmerValue += dimmerStep;
  112.         if (dimmerValue > maxDimmerValue) dimmerValue = maxDimmerValue;
  113.       }
  114.     } else {
  115.       if (dimmerValue > 0) {
  116.         dimmerValue = 0;
  117.         val = 0;
  118.         DisplayStatus();
  119.       }
  120.     }
  121.  
  122.     val = dimmerValue;
  123.     dimmerPercent = dimmerValue;
  124.   }
  125. }
  126.  
  127. void DisplayStatus() {
  128.   // === Отображение на дисплее ===
  129.   u8g2.clearBuffer();
  130.   u8g2.setFont(u8g2_font_8x13_t_cyrillic);
  131.  
  132.   u8g2.setCursor(0, 12);
  133.   u8g2.print("Обороты: ");
  134.   u8g2.print(count);
  135.  
  136.   u8g2.setCursor(0, 64);
  137.   u8g2.print("T:");
  138.   u8g2.print(maxDimmerValue);
  139.   u8g2.print('%');
  140.  
  141.   u8g2.setCursor(0, 24);
  142.   u8g2.print("Намотка: ");
  143.   u8g2.print(totalLength, 2);  
  144.   u8g2.print("м");
  145.  
  146.   u8g2.setCursor(36, 48);
  147.   u8g2.print(motorRunning ? "Запущен" : "Остановлен");
  148.  
  149.   u8g2.sendBuffer();
  150. }
  151.  
  152. void WelcomeText() {
  153.   u8g2.clearBuffer();
  154.   u8g2.setFont(u8g2_font_8x13_t_cyrillic);
  155.  
  156.   u8g2.setCursor(10, 12);
  157.   u8g2.print("Готов к ");
  158.   u8g2.setCursor(0, 26);
  159.   u8g2.print("использованию");
  160.   u8g2.setCursor(0, 42);
  161.   u8g2.print("Нажмите кнопку");
  162.   u8g2.setCursor(40, 56);
  163.   u8g2.print(" Старт");
  164.   u8g2.sendBuffer();
  165. }
  166.  
  167. void handleButton() {
  168.   extBtn.tick();  
  169.  
  170.   if (extBtn.isClick()) {
  171.     Serial.println("click");
  172.     if (!motorRunning) {
  173.       // Запуск
  174.       startMotor();
  175.     } else {
  176.       // Остановка
  177.       stopMotor();
  178.     }
  179.   }
  180. }
  181.  
  182. void startMotor() {
  183.   motorRunning = true;
  184.   count = 0;
  185.   totalLength = 0;
  186.   dimmerValue = 0;
  187.   dimmerStep = (float)maxDimmerValue / ((float)accelerationTime / (float)updateInterval);
  188.   attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, RISING);
  189.   Serial.println("Motor started");
  190.   DisplayStatus();
  191. }
  192.  
  193. void stopMotor() {
  194.   dimmerValue = 0;
  195.   motorRunning = false;
  196.   detachInterrupt(digitalPinToInterrupt(interruptPin));
  197.   Serial.println("Motor stopped");
  198.   DisplayStatus();
  199. }
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement