Hey all, first post and first few days playing with the arduino.
I'm hoping that someone might be able to tell me if my code makes sense? I really can't follow the code listed on the arduino playground for debouncing. I've managed to come up with something.. and it works.. is that all that matters or am i breaking some rule here? Thanks!
int led=8;
int button=3;
unsigned int counter = 1;
void setup()
{
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}
void loop()
{
int val = digitalRead(button);
if (val==HIGH)
{
delay(200);
counter=counter++;
}
if (counter % 2 == 0)
{
digitalWrite(led,HIGH);
}
else (digitalWrite(led,LOW));
}
Boolean debounces are tons more easy to follow, no idea why you're using modulus anyway while you can compare 1 to 1 or 2 to 2 instead of 2%2 to 0.
boolean debounce = false;
void setup(){}
void loop(){
if (!debounce){ //Don't know if this works for Arduino programming... If not, then if (debounce == false) will suffice
//The debounce isn't active
debounce = true; //Set the debounce to active
//Code would go here
delay(1000); //Wait one full second
debounce = false; //Deactivate the debounce
}
}
Moderator edit: Superscript tags swapped to code tags.
Here is another good tutorial. It's also an excellent series. This tutorial also includes a very simple and reliable hardware debounce so that you don't have to worry about coding.
Thanks for the links. I've checked a few of Blum's videos yet haven't made it to the hardware debounce quite yet (and sort of want to understand the concept behind software first).
Thanks for the ladyada tutorial. It helps a lot and actually makes a more sense than some other debounces (Store value, wait, see if value changes, if not switch led on). But compared to my own method (button press increases counter by 1, if counter is even = led is on, if counter is odd = led is off), which is the better choice?
I've got a strong feeling that the ladyada tutorial is better, but I have no idea why?
I have actually used the Lady Ada code in some programs, but I have found that it's much easier to just wire it up as a hardware debounce and forget about it. It works just fine and just a few cents in discreet components. I don't remember the value of the capacitor, but a small one is fine. I think .1uF or 10uF is what I use. I'll look when I get home. As far as what method to use, it's just a matter of personal preference. Whatever you're comfortable with.
If a resistor is in series with a capacitor such as the attached schematic, it almost acts like a tiny battery storing energy. When the capacitor is already discharged and voltage is applied, it will charge up. When voltage is removed, it discharges. This doesn't happen instantly, and the amount of time for the capacitor to charge (or discharge) within about 63% of its maximum is called the time constant. That formula is R (resistance in ohms) x C (value of capacitor in farads) = T (time) It's actually a little more complicated than this, but this suits our purposes.
I used a pull up resistor of 10k ohms and a capacitor of 22uF. 10,000 ohms x 0.000022 farads = .22seconds
That gives us about .22 seconds margin of error to debounce our switch.