#include <Bounce2.h> // https://github.com/thomasfredericks/Bounce2
// WE WILL attach() THE Bounce INSTANCE TO THE FOLLOWING PIN IN setup()
const byte buttonUP_PIN = 4;
const byte buttonDOWN_PIN = 3;
#define LED_PIN 10
int brightness = 0;
// INSTANTIATE A Bounce OBJECT.
Bounce buttonUP = Bounce();
Bounce buttonDOWN = Bounce();
void setup() {
Serial.begin(9600);
buttonUP.attach(buttonUP_PIN, INPUT_PULLUP);
buttonDOWN.attach(buttonDOWN_PIN, INPUT_PULLUP);
buttonUP.interval(5); // interval in ms
buttonDOWN.interval(5); // interval in ms
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// Update the Bounce instance (YOU MUST DO THIS EVERY LOOP)
buttonUP.update();
buttonDOWN.update();
// GET THE STATE WITH <Bounce.read()>
int debouncedStateUP = buttonUP.read();
int debouncedStateDOWN = buttonDOWN.read();
// <Bounce>.duration() RETURNS THE TIME IN MILLISECONDS THE CURRENT STATE HAS BEEN HELD.
// SO WE CHECK IF THE STATE IS LOW AND IF IT HAS BEEN LOW FOR MORE THAN 200 MS.
if ((debouncedStateUP == LOW) && (buttonUP.currentDuration() > 200))
{
brightness++;
if (brightness > 255)
{
brightness = 255;
}
analogWrite(LED_PIN, brightness);
Serial.println(brightness);
}
if ((debouncedStateDOWN == LOW) && (buttonDOWN.currentDuration() > 200))
{
brightness--;
if (brightness < 0)
{
brightness = 0;
}
analogWrite(LED_PIN, brightness);
Serial.println(brightness);
}
}