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);
}