Offline
Newbie
Karma: 0
Posts: 3
|
 |
« on: June 08, 2012, 10:29:07 am » |
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? 
|
|
|
|
|
Logged
|
|
|
|
|
California
Offline
Edison Member
Karma: 37
Posts: 1826
|
 |
« Reply #1 on: June 08, 2012, 10:36:39 am » |
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?  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; }
|
|
|
|
« Last Edit: June 08, 2012, 11:07:58 am by Arrch »
|
Logged
|
|
|
|
|
Grand Blanc, MI, USA
Offline
Edison Member
Karma: 43
Posts: 2475
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #2 on: June 08, 2012, 11:01:40 am » |
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); } }
|
|
|
|
« Last Edit: June 08, 2012, 11:05:38 am by Jack Christensen »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 3
|
 |
« Reply #3 on: June 09, 2012, 04:23:26 pm » |
Thanks for the input I don't have time to check this out right away but I will get down to it next week 
|
|
|
|
|
Logged
|
|
|
|
|
Carlisle PA USA
Offline
Newbie
Karma: 0
Posts: 23
I'm so old I'm getting paid for it!
|
 |
« Reply #4 on: June 09, 2012, 09:35:17 pm » |
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
|
|
|
|
« Last Edit: June 09, 2012, 09:53:04 pm by webtest »
|
Logged
|
Blessings in abundance, all the best, & ENJOY! Art in Carlisle, PA USA
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 3
|
 |
« Reply #5 on: June 11, 2012, 03:02:37 pm » |
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 
|
|
|
|
|
Logged
|
|
|
|
|
Grand Blanc, MI, USA
Offline
Edison Member
Karma: 43
Posts: 2475
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #6 on: June 11, 2012, 03:29:14 pm » |
codfangler, glad it worked for you, thank you for the kind words  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!
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #7 on: June 11, 2012, 03:58:33 pm » |
Awesome Jack that works for me to.  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
|
|
|
|
|
Logged
|
|
|
|
|
Grand Blanc, MI, USA
Offline
Edison Member
Karma: 43
Posts: 2475
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #8 on: June 11, 2012, 09:09:01 pm » |
Awesome Jack that works for me to.  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.
|
|
|
|
|
Logged
|
|
|
|
|
|
|
|