Скетч
#define FIRST_LED_PIN 2
#define LAST_LED_PIN 11
#define INC_BUTTON_PIN 13
#define DEC_BUTTON_PIN 12
boolean incButtonUp = true;
boolean decButtonUp = true;
int pos = 0;
void setup()
{
for (int pin = FIRST_LED_PIN; pin <= LAST_LED_PIN; ++pin)
pinMode(pin, OUTPUT);
pinMode(INC_BUTTON_PIN, INPUT_PULLUP);
pinMode(DEC_BUTTON_PIN, INPUT_PULLUP);
}
void loop()
{
for (int pin = FIRST_LED_PIN; pin <= LAST_LED_PIN; ++pin)
digitalWrite(pin, LOW);
digitalWrite(FIRST_LED_PIN + pos, HIGH);
incButtonUp = handleClick(INC_BUTTON_PIN, incButtonUp, +1, pos);
decButtonUp = handleClick(DEC_BUTTON_PIN, decButtonUp, -1, pos);
}
boolean handleClick(int buttonPin, boolean wasUp, int delta, int & pos)
{
boolean isUp = digitalRead(buttonPin);
if (wasUp && !isUp) {
delay(10);
isUp = digitalRead(buttonPin);
// если был клик, меняем яркость в пределах от 0 до 255
if (!isUp)
pos = constrain(pos + delta, 0, 9);
}
return isUp; // возвращаем значение обратно, в вызывающий код
}