const int ledPin = PB6;
const int PWM = 8;
const int maxDutyCycle = 255;
const unsigned long fadeDuration = 3000;
unsigned long previousTime = 0;
int currentDutyCycle = 0;
bool increasing = true;
void setup() {
Serial.begin(115200);
pinMode(ledPin, PWM);
Serial.println("Плавное изменение яркости с точным таймингом");
Serial.println("Используется метод millis() для точного времени");
}
void loop() {
unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime % (fadeDuration * 2);
if (elapsedTime < fadeDuration) {
currentDutyCycle = map(elapsedTime, 0, fadeDuration, 0, maxDutyCycle);
} else {
currentDutyCycle = map(elapsedTime, fadeDuration, fadeDuration * 2, maxDutyCycle, 0);
}
analogWrite(ledPin, currentDutyCycle);
if (currentTime - previousTime >= 100) {
previousTime = currentTime;
int percent = map(currentDutyCycle, 0, maxDutyCycle, 0, 100);
Serial.print("Время: ");
Serial.print(elapsedTime);
Serial.print(" мс, Яркость: ");
Serial.print(percent);
Serial.println("%");
}
delay(1);
}