Go Down

Topic: Debounce tutorial isn't a toggle... or is it? (Read 569 times) previous topic - next topic

JimboZA

Mar 16, 2013, 12:47 pm Last Edit: Mar 16, 2013, 12:50 pm by JimboZA Reason: 1
(I've put this under Sensors, since a switch is a sensor...)

With a need to debounce a switch in a project, I decided to check out the debounce tutorial for the first time.

I understand the debounce stuff... allowing a certain time to pass before "accepting" the press; happy with that.

But the tutorial says it also acts as a toggle switch:

Quote
This example demonstrates the use of a pushbutton as a switch: each time you press the button, the LED (or whatever) is turned on (if it's off) or off (if on)


Well it doesn't toggle for me: just works as a normal (albeit debounced) momentary switch... the LED is on when the switch is pressed, and off when it's not. It doesn't stay on after a press-release, it goes off.Walking through the code, I see where the debounce comes in, but don't see a toggle mechanism.

Is the narrative to the tutorial out of whack with the code, or am I missing the plot, here?

Edit: I don't need a toggle switch, I actually just need a debounced momentary switch, so I'm not fussed that it isn't a toggle. Just confused in case it is a toggle and I'm not getting that part.
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

johnwasser

The standard de-bounce example didn't work correctly according to the comments in the sketch.  I re-wrote it to actually work:
Code: [Select]


// 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

// 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 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
const unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH); // turn on the internal pull-up
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  int currentButtonState = digitalRead(buttonPin);
 
  if (currentButtonState != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay &&
    currentButtonState != buttonState) {
    // We have a new and different button state   
    buttonState = currentButtonState;
    if (buttonState == LOW) // Pressed
    {
      // The new state is "Pressed"
      ledState = !ledState;
      digitalWrite(ledPin, ledState);
    }
  }
 
  lastButtonState = currentButtonState;
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

JimboZA

Quote
The standard de-bounce example didn't work correctly according to the comments in the sketch.  I re-wrote it to actually work:


Thanks John, thought I was going nuts there.
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

daviddec

yeah! thanks to you Jeff

it hadn't worked for me either... and this noob thought he had bad hardware troubleshooting.

glad other's won't stumble as much when using the tutorials.

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview