Let me say I combined 2 peoples pieces of code so I could get an understanding of what is going on. I pretty much understand the code except maybe 1 part or 2. The thing is the debounce isnt working too great. If you hold the switch down too long it will bounce, also if you hold it too light it will bounce a lot.
The latching code itself's debounce is terrible ( no offense) also it prints
on
number of button pushes: 1
off
on
number of button pushes: 2
off
on
in the serial console. Im assuming this is cause of a bounce or just an error in the original code i havent been able to figure out yet (ive only been messing with it for a half hour)
I used part of Nick Gammon's Debounce code which Again didnt work perfectly but it worked better than the one that came with the latching code, so i combined both.
I even left the original authors comments because im not trying to be a thief im trying to learn and this has helped me a lot. Does anyone know a better way to debounce this so holding down the button wont cause bounces and or holding too little pressure wont cause bounces, or is it just the cheap tactile switches I have and not really the code lol.
Also if anyone wants to this could be posted in a list of Latching/Debounce code so others can learn from it, because I had a hard time finding the right info. Luckily I do have a programming background, just not with something that only uses a start up and a loop, im using to visual c++, scripting languages, some C but not containing all my code in a loop hehe.
Thanks all
/*
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
- 10 kilohm 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 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/ButtonStateChange
*/
//Debounce variables
const unsigned long debounceTime = 150; //milliseconds
unsigned long switchPressTime; //when the switch last changed state
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // 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 = 0; // previous state of the button
void setup()
{
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
int buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState)
{
//debounce
if(millis() - switchPressTime >= debounceTime)
{
switchPressTime = millis(); //when we closed the switch
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// if the state has changed, increment the counter
if (buttonState == HIGH) // if the current state is HIGH then the button went from off to on:
{
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else
{
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
//delay(150);
}
// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 2 == 0)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
}
}