DeBounce problem

I am probably missing something very obvious here but............ I am trying to set up a control box to simply toggle a relay on and off with one push button. The obvious way to to this seemed to be to use the simple debounce sketch from the examples. However, no matter what I do, the LED or relay will not lock on. It will happily activate whilst the button is pushed but releases as soon as the button is released. I have tried several versions of this sketch and yesterday used the latest software to re-install it on an Arduino.I am still not getting a lock. I am currently assuming I am making an error with the circuit but it's so simple that I fail to see how. Has anyone else had this embarrassing error and, if so, how did you cure it please? :astonished:

codfangler:
I am probably missing something very obvious here but............ I am trying to set up a control box to simply toggle a relay on and off with one push button. The obvious way to to this seemed to be to use the simple debounce sketch from the examples. However, no matter what I do, the LED or relay will not lock on. It will happily activate whilst the button is pushed but releases as soon as the button is released. I have tried several versions of this sketch and yesterday used the latest software to re-install it on an Arduino.I am still not getting a lock. I am currently assuming I am making an error with the circuit but it's so simple that I fail to see how. Has anyone else had this embarrassing error and, if so, how did you cure it please? :astonished:

The button examples do a great job with recognizing HIGH and LOW, but don't really offer anything for signal edge detection, which is what you want. This is detecting the instant where the button is pushed down (or released). This is accomplished by reading the buttons state and comparing the values of the last read value and the current one. Once you can determine when an edge has occurred, you turn the relay on or off, depending on the last state.

Example of signal edge detection:

void setup() {
  // stuff
}

void loop() {
  static int lastReading = LOW;
  int currentReading = digitalRead(btnPin);

  if (lastReading != currentReading ) {
    // Any signal edge
  }

  if ((lastReading==HIGH)&&(currentReading==LOW)) {
    // Falling (negative) edge
  }

  if ((lastReading==LOW)&&(currentReading==HIGH)) {
    // Rising (positive) edge
  }
  lastReading = currentReading;
}

Understood that you are using the example, but as ever, "post your code" always helps.

I really hate dealing with debouncing, I think it's a great thing to get out of the main code and into a library to handle the details. I wrote a library for this purpose and one of the examples that comes with it should do what you want. Google around a bit too, there are multiple other libraries out there. Here's the example sketch for what it's worth.

/*----------------------------------------------------------------------*
 * Example sketch for Arduino Button Library by Jack Christensen        *
 *                                                                      *
 * The simplest example. Using a tactile button switch to turn          *
 * the Arduino's pin 13 LED on and off. Wire a switch from Arduino      *
 * pin 2 to ground.                                                     *
 *                                                                      *
 * This work is licensed under the Creative Commons Attribution-        *
 * ShareAlike 3.0 Unported License. To view a copy of this license,     *
 * visit http://creativecommons.org/licenses/by-sa/3.0/ or send a       *
 * letter to Creative Commons, 171 Second Street, Suite 300,            *
 * San Francisco, California, 94105, USA.                               *
 *----------------------------------------------------------------------*/

#include <Button.h>        //https://github.com/JChristensen/Button

#define BUTTON_PIN 2       //Connect a tactile button switch (or something similar)
                           //from Arduino pin 2 to ground.
#define PULLUP true        //To keep things simple, we use the Arduino's internal pullup resistor.
#define INVERT true        //Since the pullup resistor will keep the pin high unless the
                           //switch is closed, this is negative logic, i.e. a high state
                           //means the button is NOT pressed. (Assuming a normally open switch.)
#define DEBOUNCE_MS 20     //A debounce time of 20 milliseconds usually works well for tactile button switches.
#define LED_PIN 13         //The standard Arduino "Pin 13" LED

Button myBtn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS);    //Declare the button
boolean ledState;          //A variable that keeps the current LED status

void setup(void)
{
    pinMode(LED_PIN, OUTPUT);    //Set the LED pin as an output
}

void loop(void)
{
    myBtn.read();                    //Read the button

    if (myBtn.wasReleased()) {       //If the button was released, change the LED state
        ledState = !ledState;
        digitalWrite(LED_PIN, ledState);
    }
}

Thanks for the input I don't have time to check this out right away but I will get down to it next week :slight_smile:

codfangler ...

If you want help, please post the relevant snippets of your code. I am debouncing a pair of float switches that control a sump pump, and when the pump is running, it causes ripples on the surface of the water that cause the switches to dance like crazy for well over 100 milliseconds!

I would recommend that you put your debounce code in a function ... here's mine:

/******************************************************************
 *************    FUNCTION: DEBOUNCE FLOAT SWITCH    **************
 Arduino Uno R3 hardware
 Pass the switch id number into the functon
 The function returns the true logical state of the switch (closed = true)
 Switch is wired from an INPUT_PULLUP to GROUND
 ******************************************************************/
 boolean getFS(int sNum) 
 {
  long x = 0;      // Counter to total consecutive states
  boolean stateA;  // Initial state for subsequent comparison
  boolean stateB;
  
  stateA = !digitalRead(sNum);  //Fetch the initial switch state
  // Accumulate successive IDENTICAL states
  while (x < 33300)  // Calibrated for a 200 millisecond bounce-free time
  { // Read the new state of the switch
    stateB = !digitalRead(sNum);
    // Compare the previous state to the current state
    if (stateB == stateA)
    { // The switch state is steady
      x++;  // Advance the counter
    }
    else
    { x = 0;  // The switch BOUNCED ... reset the counter
      stateA = stateB;  // Set the new reference state
    }
  }
  return stateB;              // Return the debounced state of the switch
 }

// UNTESTED example Change Of State snippet
int switchNum;  // ID number of the switch pin
boolean switchState1;  // State of the switch going into the change of state detector
boolean switchState2;  // Current state of the switch
 ...
do  // Hangs here waiting for a debounced change of state
{ switchState2 = getFS(switchNum); }  // Fetch the current state of the switch
while (!(switchState2 ^ switchState1));  // Exclusive NOR of 2 booleans
// switchState2 is the current new stable state of the switch
if(switchState2) { ... Leading edge of switch closure goes here ... ; }
// OR
if(!switchState2) { ... Trailing edge of switch closure goes here ... ; }
 ...

My function hangs in a loop for a LONG time (200 milliseconds). You can tailor it to your needs. If you have performance issues in future projects, you'll have to use a fast timer interrupt approach.

Blessings in abundance, all the best, & ENJOY!
Art in Carlisle, PA USA

My thanks to all who replied to this. I have found my error but also having now tried the various offerings I have decided that Jacks method is best for me and so I will be using it from now on for my current project For anybody else who might be interested, Jacks method is very elegant but also very simple to implement across multiple relays which is what I intend to use it for
Thanks again XD

codfangler, glad it worked for you, thank you for the kind words :slight_smile:

I have to laugh at myself a bit, the humble push button proved more elusive to me in some ways than various sensors, displays, RTCs, etc. The library was my attempt at conquering the darn things. I wonder if it's not more of a heavyweight than needed, processing-wise, but so far so good. I'm always using it whenever I have even a single button, and no long-press requirements, etc.

All feedback welcome!

Awesome Jack that works for me to. :slight_smile:
Tell me is there a way to count how many times the relay switch has been on(As in a HIGH = OUTPUT) Example :Count and remember each time relay became High till 10 times.
Then relay low and another pin high after the count is reach.
Regards
F

Fransie:
Awesome Jack that works for me to. :slight_smile:
Tell me is there a way to count how many times the relay switch has been on(As in a HIGH = OUTPUT) Example :Count and remember each time relay became High till 10 times.
Then relay low and another pin high after the count is reach.
Regards
F

Well if I'm understanding, that sounds relatively straightforward. Do you have code you could post that did not do what you wanted? Post it and tell us what it did instead.

OK not a problem. Gonna start a new tread.
Thanks

Here is it now Jack .http://arduino.cc/forum/index.php/topic,109680.0.html