Pages: [1]   Go Down
Author Topic: Debounce tutorial isn't a toggle... or is it?  (Read 553 times)
0 Members and 1 Guest are viewing this topic.
Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 113
Posts: 4945
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

(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.
« Last Edit: March 16, 2013, 06:50:37 am by JimboZA » Logged

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

// 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;
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 113
Posts: 4945
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Olympia, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: