Pages: [1]   Go Down
Author Topic: Debounce + StateChange [SOLVED]  (Read 889 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good day,
I am a newbie and a bit stuck with my basic code.
The code works fine, but debounceDelay does not have any effect. If i put long debounceDelay = 1000; the code does not wait 1000ms , but executes instantly. What am I missing ?

Many thanks in advance guys !

Code:
const int buttonPin = 2;  
const int ledPin =  13;      

int currState = 0;            
int preState = 0;  
int reading = 0;
long lastDebounceTime = 0;  
long debounceDelay = 150;    
 


void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  reading = digitalRead(buttonPin);
 
 if (reading != preState) {
     if ((millis() - lastDebounceTime) > debounceDelay){
     Serial.println("delay finished");
     currState = reading;
     }
   lastDebounceTime = millis();
 }
  

  if (currState != preState){
     if(preState == LOW){
     digitalWrite(ledPin, HIGH);
     delay(500);
     digitalWrite(ledPin, LOW);
     delay(500);
     digitalWrite(ledPin, HIGH);
     delay(500);
     digitalWrite(ledPin, LOW); }
     preState = currState;
     }

   }
 
« Last Edit: January 31, 2013, 08:52:52 am by peetsyo » Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4403
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How is the button wired ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50907
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Using names that appear to go together, like currState and prevState makes it a lot easier to follow the code than names like reading and lastButtonState.

There are two recognized styles for where the { goes. Some people like it on the same line as the statement that it goes with. They're weird. Some people prefer it on the next line, indented the same amount as the statement that it goes with. They're smart.

There is exactly one recognized style for there the } goes. On a line all by itself, properly indented.

I suggest that you adopt a recognized style for your code.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks ,
button is wired like that with 10k resistor.  (also modified the code to look good)
Is it something to do with code sequence ?
The deBounceDelay comparison goes before it resets lastDebounceTime ? So basically lastDebounceTime = millis(); should be written before the comparison, although it cannot.

« Last Edit: January 30, 2013, 09:12:46 am by peetsyo » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26631
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Some people like it on the same line as the statement that it goes with.
They're system-software engineers  smiley-cool
Quote
They're weird
Can't argue with that.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Queens, New York
Offline Offline
Faraday Member
**
Karma: 108
Posts: 3785
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this and note the differences
Code:
void loop() {
  reading = digitalRead(buttonPin);
 
  if (reading != preState) {
     lastDebounceTime = millis();
     preState = reading;
     }
 
  if ((millis() - lastDebounceTime) > debounceDelay) {
      Serial.println("delay finished");

      if(reading == LOW){
      digitalWrite(ledPin, HIGH);
      delay(500);
      digitalWrite(ledPin, LOW);
      delay(500);
      digitalWrite(ledPin, HIGH);
      delay(500);
      digitalWrite(ledPin, LOW);
    }
  }
}
Edit, forgot to change currstate to prestate
« Last Edit: January 30, 2013, 10:07:11 am by HazardsMind » Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

before i did a debounce and state change post.... so if you did serch you would find the answer to your question very easily....
but since i am in a very good mood since all that help me understand what the hell is debounce and state change does is in this post i ask them if they dont mind me sending you all the code that they help me finish?
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

never mind i saw hazard mind post .... its the same thing,
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't know where I am doing the mistake ,but I cannot get it to work
Goal is to make the led blink only when "Debounced" NO contact goes from OPEN -> CLOSED.
If contact goes from CLOSED -> OPEN , led remains LOW.
Like simple automation.
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

heres one
Code:
uint8_t Switch = 2;
uint8_t Led =13;

boolean LedState =LOW;
int SwitchState =0;
int SwitchDebounce;
int LastSwitchState=HIGH;
int LastSwitchDebounce=LOW;

unsigned long LastDebounceTime = 0;
unsigned long DebounceDelay = 50;

void setup()
{
  pinMode(Switch,INPUT);
  digitalWrite(Switch,HIGH);
  pinMode(Led,OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  int CurrentSwitch = digitalRead(Switch);
  if (CurrentSwitch != LastSwitchDebounce)
  {
    LastDebounceTime = millis();
  }
  if ((millis() - LastDebounceTime) > DebounceDelay)
  {
    SwitchState = digitalRead(Switch);
    if (SwitchState != LastSwitchState)
    {
      if (SwitchState == LOW)
      {
        LedState = !LedState;
        (LedState)?Serial.println("Led is On"):Serial.println("Led is Off");
      }
    }
    LastSwitchState=SwitchState;
  }
  digitalWrite(Led,LedState);
  LastSwitchDebounce = CurrentSwitch;
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

why do you use "complicated code" for a simple task.

If you need to "debounce" you can simply read the value, wait and read again (thats what you do anyway).
If the value is the same both times use it otherwise discard it.

Example:

bValue1 = analogRead(pBUTTONhb);
delay(50);
bValue2 = analogRead(pBUTTONhb);

if bValue1 == bValue2 ......
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

illbatt yes you could, btw is that from the ladyada 5th lesson tutorial?

anyway what this code did is that it will reach almost instantaneously so it wont make you wait in your brief window of opening for you to be able to control your circuit
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, thats my own (have never read that tutorial).
Did'nt understand what you mean in your last sentence.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for watching, i got the solution.
When switch goes on, blinks twice.

Code:
const int buttonPin = 2;   
const int ledPin =  13;     

int currState = 0;           
int preState = 0;   
int reading = 0;
int prepreState =0;
long lastDebounceTime = 0; 
long debounceDelay = 1500;   
 
void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  reading = digitalRead(buttonPin);
 
 if (reading != currState) {
   lastDebounceTime = millis();
   currState = reading;
 }
   
  if (reading != preState) {
 if ( millis() - lastDebounceTime > debounceDelay ){
    if (reading == HIGH){
      if (preState == LOW){
        digitalWrite(ledPin, HIGH);
        delay(500);
        digitalWrite(ledPin, LOW);
        delay(500);
        digitalWrite(ledPin, HIGH);
        delay(500);
        digitalWrite(ledPin, LOW);
        preState = reading;
      }
    }
  else  {preState = reading;}
}
}
}
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4403
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

delay() does what it says.  It delays execution of the next instruction.  That may or may not be important, but in some circumstances it is important not to do block loop() from executing as often as possible such as when reading a sensor, hence the 'Blink without delay' technique which allows loop() to run continuously and not be blocked which gives the effect of doing 2 (or more) things at once.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pages: [1]   Go Up
Jump to: