Debounce to work from DigitalPin.

Hello guys, I know how debounce works, but I want to control it with DigitalPin 7. Then (Pin 7) is HIGH leds goes on/off with button and then (PIN 7) LOW leds with button would shut down.
Maybe someone have an example something like this or could give a hand here ?

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int DigitalPin = 7; // Button which control

// 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 unsigned long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers

void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(DigitalPin, INPUT);
pinMode(ledPin, OUTPUT);

// set initial LED state
digitalWrite(ledPin, ledState);
}

void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);

// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:

// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;

// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}

// set the LED:
digitalWrite(ledPin, ledState);

// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}

Easy. All you need to do is make debouncing the button a function, and then use a conditional 'if' statement to determine whether to run said function.

Simply change 'void loop' to 'void debounce' to create a function. Then, make a new void loop with the conditional statement:

if (digitalRead(DigitalPin) == HIGH){
debounce();
}

That should do the trick. If not, create a variable to store the state of pin 7, e.g.

int pinState;

void loop (){

pinState = digitalRead(DigitalPin);

if (pinState == HIGH){
debounce();
};

}

That will definitely do it.

  • D.E.

Thanks for the replay now it's working , but it takes about 4 seconds to turn off leds and button then the DigitaPin is LOW. Does it need another command to read DigitalPin State? Or something else is wrong ?

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int ledPin1 = 12; // the number of the LED pin
const int InPin = 3; // Input of DigitalPin

// Variables will change:
int ledState = LOW; // the current state of the output pin
int ledState1 = LOW; // 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
int pinState; // DigitalPin ( InPin ) STATE

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

void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(InPin, INPUT);

// set initial LED to off
digitalWrite(ledPin, LOW);
digitalWrite(ledPin1, LOW);
}

void loop () {

pinState = digitalRead(InPin);

if (pinState == HIGH) {
debounce();

}

else if (pinState == LOW) {

digitalWrite(ledPin, LOW);
digitalWrite(ledPin1, LOW);

}

}

void debounce()

{
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);

// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:

// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;

// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
ledState1 = !ledState1;
}
}
}

// set the LED:
digitalWrite(ledPin, ledState);
digitalWrite(ledPin1, ledState1);

// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}

Thanks for the replay now it's working , but it takes about 4 seconds to turn off leds and button then the DigitaPin is LOW. Does it need another command to read DigitalPin State? Or something else is wrong ?

Why are you reading the input on InPin 3 and applying debounce() to buttonPin 2.

Is there any pull up or pull down resistor on InPin 3? The 4 seconds seems like the time for a floating pin to change state.

I need leds to work only then input on InPin is HIGH, and debounce for changing the state of leds then InPin is HIGH. Then InPin is LOW nothing should work. I use 5V from arduino to get HIGH on InPin.

I use 5V from arduino to get HIGH on InPin.

Then you will need a pull down resisitor to Ground. The pin is taking time for the 5V to bleed off.

It may be more simple to make InPin INPUT_PULLUP, and reverse the logic so that it reads LOW to call debounce() and HIGH to turn the leds off.

It’s working fine now. But i need one more argument. Lets say InPin went from HIGH to LOW and then again TO HIGH it keeps last button state and leds are on. My question is how to make leds off then InPin was reconnected?

It's kind a working with this line , but it's not stabe enough :confused:

// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH && digitalRead(InPin)== HIGH) {

ledState = !ledState;
ledState1 = !ledState1;
digitalWrite(ledPin, ledState);
digitalWrite(ledPin1, ledState1);
}
}
}

// set the LED:

// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn’t show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.