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


const int buttonPin = 2;   // кнопка
const int ledPin = 9;      // светодиод (PWM)

int brightness = 0;
int step = 0;
bool increasing = true;

int lastButtonState = HIGH;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);

  analogWrite(ledPin, 0);
}

void loop() {
  int buttonState = digitalRead(buttonPin);

  // Определяем новое нажатие кнопки
  if (lastButtonState == HIGH && buttonState == LOW) {

    if (increasing) {
      step++;
      brightness = step * 64;

      if (step == 4) {
        brightness = 255;
        increasing = false;
      }
    } 
    else {
      step--;
      brightness = step * 64;

      if (step == 0) {
        brightness = 0;
        increasing = true;
      }
    }

    analogWrite(ledPin, brightness);

    delay(200); // защита от дребезга кнопки
  }

  lastButtonState = buttonState;
}