turnoff delay with debounce

hi wondering how to combine this code with debonce. Thanks

const int ledPin = 8;
const int button = 3;
unsigned long interval = 5000;
unsigned long startTime;
byte lastPress;

// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin

// the following variables are long’s because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 100; // the debounce time; increase if the output flickers

//
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(button, INPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
}
//
void loop()
{
byte pressed = digitalRead(button);
if (pressed)
{
if (pressed != lastPress)
{
digitalWrite(ledPin
, HIGH);
startTime = millis();
}
}
lastPress = pressed;
if (digitalRead(ledPin))
{
if (millis() - startTime >= interval)
{
digitalWrite(ledPin, LOW);
}
}
}

fr_nslagsf_rdr_jning_m_debounce.ino (1.1 KB)

hi wondering how to combine this code with debonce.

What do you want to debounce?

Why are you reading the state of an output pin? Remember what state you set it to!

Debounce usually applies to a mechanical switching device.

Paul

PaulS: IsWhat do you want to debounce?

Why are you reading the state of an output pin? Remember what state you set it to!

I am new at this so i dont Know how to do a turnoff delay on the output and a debounce on the input

so i dont Know how to do a turnoff delay on the output

I have no clue what this means.

and a debounce on the input

Debouncing means ignoring any change that happens too soon after a change of interest. So, you need to record WHEN the change of interest happens. If another potentially interesting change happens, and the time since the last change of interest is less than some value, ignore the change.

So, you need to separate reading the switch state, and determining whether the change should be acknowledged, from using the changed value.

Create a function that manages reading the switch state, recording when the change happens, comparing the time of this change to the time of the last change, and storing, or not, the change.

Create another function that does something based on the current state of the switch. The code in the two functions should know NOTHING about the other function. The only global variable you should have is the pin number. All the rest of the variables should be static and local to the function. Exchange information as needed using function arguments and return values.