const int buttonPin = A0;
const int ledPin = LED_BUILTIN;
unsigned long previousMillis; //obsluha tlačítka
unsigned long previousMillisLED; //delay LED
const unsigned long interval = 5000; // 5 seconds button
const unsigned long RT = 3000; // delka sviceni led
bool ledOn = false; // default, led is off
int lastButtonState = HIGH; // HIGH is idle (button not pressed)
void setup()
{
pinMode( buttonPin, INPUT_PULLUP);
pinMode( ledPin, OUTPUT);
}
void loop()
{
unsigned long currentMillis = millis();
unsigned long currentMillisLED = millis();
int buttonState = digitalRead( buttonPin);
if( buttonState != lastButtonState) // something changed ?
{
if( buttonState == LOW) // button is pressed ?
{
previousMillis = currentMillis; // remember millis value
}
else // button is released
{
if( currentMillis - previousMillis < interval) // short press
{
digitalWrite( ledPin, HIGH); // turn led on
ledOn = true; // remember that led is on
unsigned long currentMillisLED = millis();
if(currentMillisLED - previousMillisLED >= RT)
{
digitalWrite(ledPin,LOW); // button is released
previousMillisLED = currentMillisLED;
}
//previousMillisLED = currentMillisLED;
}
}
lastButtonState = buttonState; // remember new button state
}
else if( buttonState == LOW && ledOn) // button still pressed and led is on ?
{
if( currentMillis - previousMillis >= interval) // pressed long enough ?
{
digitalWrite(ledPin, LOW);
ledOn = false;
}
}
delay( 30); // a delay as a simple way to debounce the button
}