0

It's my custom debounce code

It keeps the led on by flipping the required threshold value when triggered after the led has turned on to keep the led in a high state and vice verse triggered again, the comments on the code explain it in more detail.

/*
 * This code is my custom debounce code, I made this but this isn't working as planned, 
 * see the thing is that it turns on when I hold the button and if I release it, 
 * it starts to flickers and then gradually turn off, what is wrong with my code ? 
 * I can't see it.
 */
const int ledPin = 13; // led connected to pin 13
const int PushButton = 2; // a pushbutton connected to pin 2
int switcher = HIGH; // the switcher changes it's value, we require this later
int dilay = 1; // controls the delay
unsigned long lastTrigger = 0; // records the last time the led was turned on
void setup() {
 pinMode(ledPin, OUTPUT); // sets ledpin as output
 pinMode(PushButton, INPUT); // sets pushbutton pin as input
}
void loop() {
 int pushButtonState = digitalRead(PushButton); // stores the value of the pin each time the loop begins agian
 if((millis() - lastTrigger) >= dilay){ // my custom debounce code
 if(pushButtonState == switcher){ // this is the trick, when the pushbutton pin is high it's equal to the switcher value
 digitalWrite(ledPin, HIGH); // it turns on the led 
 int switcher = LOW; // now the switcher value will be low now the pushbutton pin value will also be low keeping the led on
 }else{
 digitalWrite(ledPin, LOW); // now if the led is on and pushbutton is high, then it will not be equal to the switcher because we had changed it in the previous statement 
 int switcher = HIGH; // now the switcher is high and the pushbutton value low, switcher isn't equal to pushbutton so there by keeping the ledpin as low
 }
 if(pushButtonState == 1){ // if pushbutton is high
 int lastTrigger = millis(); // then lastTrigger equals to the current time
 }
 }
}
Gerben
11.3k3 gold badges22 silver badges34 bronze badges
asked Sep 28, 2018 at 17:47
3
  • 1
    how is the button wired? do you have a pulldown resistor? Commented Sep 28, 2018 at 18:11
  • the button is a switch which connects the 5v pin and pin 2, and no i don't use a pull down resistor Commented Oct 2, 2018 at 6:56
  • without pulldown, the pin free floats between LOW and HIGH when disconnected from 5V Commented Oct 2, 2018 at 7:04

1 Answer 1

1

Change int switcher = LOW/HIGH to just switcher = LOW/HIGH.

Otherwise you are redefining the variable, and will make it a local variable, and not change the global variable with the same name (as you are expecting).

answered Sep 28, 2018 at 18:17
2
  • 1
    PS you code isn't actually doing any debouncing. It negates it a bit by only polling the input pin every 1 ms. But in some switches bouncing can take up to 6ms. Commented Sep 28, 2018 at 18:21
  • now with the change you suggested, it makes the led blink now Commented Oct 2, 2018 at 7:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.