is you all know in the arduino IDE there are some example of programming which also include
debounce:
const int buttonPin = 2;
const int ledPin = 13;
int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
buttonState = reading;
}
digitalWrite(ledPin, buttonState);
lastButtonState = reading;
}
and State Change Detection
const int buttonPin = 2;
const int ledPin = 13;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter, DEC);
}
else {
Serial.println("off");
}
}
lastButtonState = buttonState;
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
I was trying to merge both the program together and i came out with this
uint8_t Switch =2;
uint8_t Led =13;
boolean LastSwitchState=HIGH;
boolean LedState=LOW;
unsigned long LastDebounceTime;
void setup()
{
pinMode(Switch,INPUT);
digitalWrite(Switch,HIGH);
pinMode(Led,OUTPUT);
}
void loop()
{
boolean SwitchState=digitalRead(Switch);
if (SwitchState != LastSwitchState)
{
LastDebounceTime=millis();
}
if (millis()-LastDebounceTime>=10UL)
{
if (digitalRead(Switch) ==LOW)
{
LedState = !LedState;
}
}
digitalWrite(Led,LedState);
LastSwitchState = SwitchState;
}
What happen with my code was, the led will light up at random as though the switch have not been debounce man i was frustrated. i tried thinking it over and over still cant see whats the problem i tried changing the variable and the position of each line in the hope of it make sense.
the i remembered one tutorial that i saw long ago, it work perfectly but i try to change it to suit my need.Haha my need is not to use delay and thats it
heres the link to the tutorial in ladyada site Arduino Tutorial - Lesson 5
and if you scroll down or you try to read all the tutorial.( Its a good tutorial btw) you will find this in challenge part 2. and the best thing is that the site make a white back ground and turn the writing white so its kinda invisible unless you try to highlight the white box.
heres the code that i am refering to
int switchPin = 2;
int led1Pin = 12;
int led2Pin = 11;
int led3Pin = 10;
int led4Pin = 9;
int led5Pin = 8;
int val;
int val2;
int buttonState;
int lightMode = 0;
void setup() {
pinMode(switchPin, INPUT);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
pinMode(led5Pin, OUTPUT);
Serial.begin(9600);
buttonState = digitalRead(switchPin);
}
void loop(){
val = digitalRead(switchPin);
delay(10);
val2 = digitalRead(switchPin);
if (val == val2) {
if (val != buttonState) {
if (val == LOW) {
if (lightMode == 0) {
lightMode = 1;
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, HIGH); // blink on!
digitalWrite(led4Pin, HIGH);
digitalWrite(led5Pin, HIGH);
delay(100);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW); // blink off!
digitalWrite(led4Pin, LOW);
digitalWrite(led5Pin, LOW);
delay(100);
} else {
lightMode = 0; // turn light off!
}
}
}
buttonState = val;
}
}
and heres how i think it should be when i try to convert it without using delay() and use millis() instead
/*Declaring Pin Location*/
uint8_t Switch = 2;
uint8_t Led =13;
/* Declaring Fix Variable*/
unsigned long DebounceTime=5;
/* Declaring Variable*/
boolean SwitchState1 = HIGH;
boolean SwitchState2 = HIGH;
boolean LastSwitchState = HIGH;
unsigned long LastDebounce =0;
boolean LedState=LOW;
/* Only run Once during Startup*/
void setup()
{
pinMode(Switch,INPUT);
digitalWrite(Switch,HIGH);
pinMode(Led,OUTPUT);
LastSwitchState = digitalRead(Switch);
}
/*Run Continuoustly*/
void loop()
{
SwitchState1=digitalRead(Switch);
if (SwitchState1 != LastSwitchState)
{
LastDebounce=millis();
}
if ((millis() - LastDebounce) >= DebounceTime)
{
SwitchState2= digitalRead(Switch);
}
if (SwitchState1 == SwitchState2)
{
if (SwitchState2!= LastSwitchState)
{
if ( SwitchState2 == LOW)
{
LedState = !LedState;
}
LastSwitchState = SwitchState2;
}
}
digitalWrite(Led,LedState);
}
Still the outcome is kinda the same. I have also try to change a couple of variable and how my placement of variable and code.
so after this long post of maybe what i could call failure to merge and to debug, I surrender and ask the big guns on why my code didn't work in term of debouncing and state change detecting in both account. Any hints and help is much appreciated and i bow my head in defeat in this simple task that i cant seem to wrap my head around.
edited: I have to delete all the comment so that I can fit all the code and all.