newbie stuck on monifying samples

I'll start with explaining what I wanted to accomplish. I started with the digital "StateChange" example and wanted to turn the LED on with a depressed button. The LED would remain on until a seconday button press and release. The LED would light with the depression of the button and turn off with the release of the second button press. This code I got working to my satsfaction (see below)

/*
  State change detection (edge detection)
    
 Often, you don't need to know the state of a digital input all the time,
 but you just need to know when the input changes from one state to another.
 For example, you want to know when a button goes from OFF to ON.  This is called
 state change detection, or edge detection.
 
 This example shows how to detect when a button or button changes from off to on
 and on to off.
    
 The circuit:
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 * LED attached from pin 13 to ground (or use the built-in LED on
   most Arduino boards)
 
 created  27 Sep 2005
 modified 14 Oct 2010
 by Tom Igoe

This example code is in the public domain.
    
 http://arduino.cc/en/Tutorial/ButtonStateChange
 
DBF - Modifications

Modifed by using changing the button so it is using the internal pullup resistor
LED turn on as soon as FallingEdge is detected, rising edge is ignored
LED turns off with next risingedge detection, falling edge is ignored
 
 */

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 12;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 1;  // previous state of the button
boolean LEDState = LOW;

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // Enable the builtin pullup resistor
  digitalWrite(buttonPin, HIGH); 

  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is LOW then the button
      // went from released to depressed:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter, DEC);
      //LED is currently off so turn it on immediately
      if ((buttonPushCounter+1) % 2 == 0) {
        LEDState= HIGH;
      }
    } 
    else {
      // if the current state is HIGH then the button
      // went from depressed to released:
      if (buttonPushCounter % 2 == 0) {
        LEDState = LOW;
      }
      Serial.println("off"); 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;

    digitalWrite(ledPin, LEDState);
}

Then I decided I would debounce the button using sample code from the debounce example. Somehow I have thoroughly confused myself and cant seem to fine the error in my ways. Any hints would be greatly appreciated. I have fought with this code for days now and realize I can no longer see the forest for the trees here. I hope my code posting works as this is my first time post here and dont know all the ins and outs of this forum yet.

/*
  State change detection (edge detection)
    
 Often, you don't need to know the state of a digital input all the time,
 but you just need to know when the input changes from one state to another.
 For example, you want to know when a button goes from OFF to ON.  This is called
 state change detection, or edge detection.
 
 This example shows how to detect when a button or button changes from off to on
 and on to off.
    
 The circuit:
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 * LED attached from pin 13 to ground (or use the built-in LED on
   most Arduino boards)
 
 created  27 Sep 2005
 modified 14 Oct 2010
 by Tom Igoe

This example code is in the public domain.
    
 http://arduino.cc/en/Tutorial/ButtonStateChange
 
DBF - Modifications

Modifed by using changing the button so it is using the internal pullup resistor
LED turn on as soon as FallingEdge is detected, rising edge is ignored
LED turns off with next risingedge detection, falling edge is ignored

Added code to debounce button **** NOT WORKING! ****
 
 */

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 12;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 1;         // current state of the button
int lastButtonState = 1;  // previous state of the button
boolean LEDState = LOW;

// 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.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // Enable the builtin pullup resistor
  digitalWrite(buttonPin, HIGH); 

  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  int reading = digitalRead(buttonPin);

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    buttonState = reading;
  }

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is LOW then the button
      // went from released to depressed:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter, DEC);
      //LED is currently off so turn it on immediately
      if ((buttonPushCounter+1) % 2 == 0) {
        LEDState= HIGH;
      }
    } 
    else {
      // if the current state is HIGH then the button
      // went from depressed to released:
      if (buttonPushCounter % 2 == 0) {
        LEDState = LOW;
      }
      Serial.println("off"); 
    }
  }
  lastButtonState = reading;

  // save the current state as the last state, 
  //for next time through the loop

  digitalWrite(ledPin, LEDState);
  //  Serial.print("CurButtonState:  ");
  //  Serial.println(buttonState, DEC);
  //  Serial.println(lastButtonState, DEC);

}

I started with the digital "StateChange" example and wanted to turn the LED on with a depressed button. The LED would remain on until a seconday button press and release. The LED would light with the depression of the button and turn off with the release of the second button press. This code I got working to my satsfaction (see below)

That code looks reasonable.

Then I decided I would debounce the button using sample code from the debounce example. Somehow I have thoroughly confused myself and cant seem to fine the error in my ways. Any hints would be greatly appreciated.

The error was in not telling us what the problem is. Nothing in the 2nd code you posted pops out and screams "Idiot!", so the way to find the problem will involve knowing what to look for. Tell us what the code does that you don't want it to do, and/or what is doesn't do that you do want it to do.

Keep in mind that debouncing a button simply means ignoring changes in state that occur very close together. With the Serial.print()s that you have in the code, you won't get back to reading the button state again until long after any bouncing has died out.

There is also a library that manages all the stuff you are managing. http://www.arduino.cc/playground/Code/Bounce If nothing else, you can look at the code to see HOW it is done, and then do it yourself.

Keep in mind that debouncing a button simply means ignoring changes in state that occur very close together. With the Serial.print()s that you have in the code, you won't get back to reading the button state again until long after any bouncing has died out.

Thats a good point. Let me comment out the serial prints and see where I stand. I guess I am kind of used to a more full featured IDE with watchlists, breakpoints, step, etc so I think the best way to approach this might be to hand trace with paper. Thanks for the response and you insight hasgiven me a new direction to explore. As far as what my code was doing (or was not)... all I can really say was it was possessed. It seemed to do nothing that I had intended. Even the on/off states of the LED didnt folow the rising/falling edge state changes. And to be honst I had tried so many things it was nearly impossible to try to describe everything I had tried and the results of each change.

Are you using proper pull-down resistors for the switch input pins as explained in the preamble of the code you posted? Floating input pins can confuse even the best ghostbusters. :smiley:

Lefty

Are you using proper pull-down resistors for the switch input pins as explained in the preamble of the code you posted? Floating input pins can confuse even the best ghostbusters.

Sorry about that. I should have changed the notes up top but that was just from the sample code. If you look at my added comments for my modification, I changed the switch to use the built in pullup resistor

Woo hoo!!! The hand tracing and the tip about the serial debuggin lines slowing down the code making them useless was the key to getting past this. I have included the modified code below which does what I want. Next step... add fading code to fade the led in and out if depressed longer that .5 seconds. And to maintain brightness level after button is released. The fade should NOT turn off led on rising edge if fade routine was activated.

/*
  State change detection (edge detection) with debounce code.
    
 Often, you don't need to know the state of a digital input all the time,
 but you just need to know when the input changes from one state to another.
 For example, you want to know when a button goes from OFF to ON.  This is called
 state change detection, or edge detection.
 
 This example shows how to detect when a button or button changes from off to on
 and on to off.
    
 The circuit:
 * pushbutton attached to pin 2 from with builtin pullup resistor turned on
 * Button depressed with cause pin to go LOW, release will cause pin 2 to be HIGH
 * LED attached from pin 12 to ground (or use the built-in LED on most Arduino boards)
 
 created  27 Sep 2005
 modified 14 Oct 2010
 by Tom Igoe

This example code is in the public domain.
    
 http://arduino.cc/en/Tutorial/ButtonStateChange
 
DBF - Modifications - 02/26/2011

Modifed by using changing the button so it is using the internal pullup resistor
LED turn on as soon as FallingEdge is detected, rising edge is ignored
LED turns off with next risingedge detection, falling edge is ignored

Added code to debounce button. Needed to capture state change when ONCE when
debounce time had exceeded threshhold.
 
 */

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 12;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 1;         // current state of the button
int lastButtonState = 1;  // previous state of the button
boolean LEDState = LOW;
boolean StateChange = 0;

// 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.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // Enable the builtin pullup resistor
  digitalWrite(buttonPin, HIGH); 

  // initialize serial communication:
//  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  int reading = digitalRead(buttonPin);

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    if (buttonState != lastButtonState) {
      StateChange = 1;
    }
    buttonState = reading;
  }

  // compare the buttonState to its previous state
//  if (buttonState != lastButtonState) {
  if (StateChange) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is LOW then the button
      // went from released to depressed:
      buttonPushCounter++;
//      Serial.println("on");
//      Serial.print("number of button pushes:  ");
//      Serial.println(buttonPushCounter, DEC);
      //LED is currently off so turn it on immediately
      if ((buttonPushCounter+1) % 2 == 0) {
        LEDState= HIGH;
      }
    } 
    else {
      // if the current state is HIGH then the button
      // went from depressed to released:
      if (buttonPushCounter % 2 == 0) {
        LEDState = LOW;
      }
//      Serial.println("off"); 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = reading;
  // reset StateChange - we only want to process each state change once
  StateChange = 0;


  digitalWrite(ledPin, LEDState);
  //  Serial.print("CurButtonState:  ");
  //  Serial.println(buttonState, DEC);
  //  Serial.println(lastButtonState, DEC);

}

I tried to make this thread as "SOLVED" but I dont know how to do that.

One last note. I was aware that there are already a few librarys for debouncing input, but since I was a newbie, I thougth it would be a better learning experience if I tried to work out the code myself rather than just included a pre-packaged library with out really understanding what is going on. Thanks for the responses and help. It definately helped get past the wall i was stuck at.

Thats a good point. Let me comment out the serial prints and see where I stand. I guess I am kind of used to a more full featured IDE with watchlists, breakpoints, step, etc so I think the best way to approach this might be to hand trace with paper.

Well, now, that all works great when the code and the debugger run on the same hardware. The code you are writing in the Arduino IDE on the PC does not run on the PC. It gets uploaded to another computer, and runs there.

Well, now, that all works great when the code and the debugger run on the same hardware. The code you are writing in the Arduino IDE on the PC does not run on the PC. It gets uploaded to another computer, and runs there.

I did not intend that to sound as if I was putting down the arduino platform in any way. I was just stating that I was used to a more robust debugging expereince. I am fully aware that compiled code is run entirely on the arduino processor. The hand trace is something I havent done in year but I can see it will still be a valuable skill going forward. Again, I thank you for your insight which ultimately lead me to resolving my issue. I'm not sure how many more hours I would have spent banging my head against the wall till the light went on about the serial debugging lines.

I did not intend that to sound as if I was putting down the arduino platform in any way.

OK. It did sound just a teensy bit like a complaint. Debugging embedded code requires a much different skill set than debugging PC applications. The pencil and paper method, in my opinion, is still the best debugging tool going.