const unsigned long tickPeriod = 400;
bool prevState = HIGH;
bool wejscie = HIGH;
bool beenPressed = LOW;
bool beenReleased = LOW;
uint8_t brightness = 50;
bool brightnessDirectionUp = true;
uint8_t brightnessLo = 0;
uint8_t brightnessMid = 50;
uint8_t brightnessHi = 100;
volatile unsigned long lastDebounceTime = 0;
volatile unsigned long lastPressedTime = 0;
volatile unsigned long lastRepeatTime = 0;
void setup() {
// put your setup code here, to run once:
pinMode(2, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
volatile unsigned long currentTime = millis(); // Get the current time
//volatile unsigned long timeDiff = (currentTime - lastDebounceTime);
wejscie = digitalRead(DDC2);
if ((wejscie == LOW) and (prevState == HIGH)) {
lastPressedTime = currentTime;
beenPressed = HIGH;
Serial.println("Pressed");
} else {
beenPressed = LOW;
}
prevState = wejscie;
volatile unsigned long timeDiffRepeatDelay = (currentTime - lastPressedTime);
volatile unsigned long timeDiffRepeatPeriod = (currentTime - lastRepeatTime);
// if ((beenPressed == HIGH) or ((wejscie == LOW) and (timeDiffRepeatDelay > 1200) and (timeDiffRepeatPeriod > tickPeriod))) {
// lastRepeatTime = currentTime;
// Serial.println("TickRepeat");
// digitalWrite(LED_BUILTIN, HIGH);
// delay(100);
// digitalWrite(LED_BUILTIN, LOW);
// }
if ((brightness == brightnessLo) or (brightness == brightnessMid) or (brightness != brightnessHi)) {
if ((wejscie == LOW) and (timeDiffRepeatDelay > 1200) and (timeDiffRepeatPeriod > tickPeriod)) {
Serial.println("Dent");
}
}
if ((beenPressed == HIGH) or ((wejscie == LOW) and (timeDiffRepeatDelay > 1200) and (timeDiffRepeatPeriod > tickPeriod))) {
lastRepeatTime = currentTime;
if (brightnessDirectionUp == true) {
brightness += 8;
} else {
brightness -= 8;
}
Serial.println(brightness);
Serial.println("TickRepeat");
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
} else if ((wejscie == HIGH) and (brightness == brightnessLo) and (brightness == brightnessHi)) {
brightnessDirectionUp = not brightnessDirectionUp;
Serial.print("Direction Change to:");
Serial.println(brightnessDirectionUp);
}
}