Go Down

Topic: Debounce + StateChange [SOLVED] (Read 970 times) previous topic - next topic

peetsyo

Jan 30, 2013, 02:22 pm Last Edit: Jan 31, 2013, 02:52 pm by peetsyo Reason: 1
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: [Select]
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;
    }

  }

UKHeliBob

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

PaulS

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.

peetsyo

#3
Jan 30, 2013, 02:58 pm Last Edit: Jan 30, 2013, 03:12 pm by peetsyo Reason: 1
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.


AWOL

Quote
Some people like it on the same line as the statement that it goes with.

They're system-software engineers  8)
Quote
They're weird

Can't argue with that.
"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.

HazardsMind

#5
Jan 30, 2013, 03:49 pm Last Edit: Jan 30, 2013, 04:07 pm by HazardsMind Reason: 1
Try this and note the differences
Code: [Select]
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
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

ash901226

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?

ash901226

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

peetsyo

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.

ash901226

heres one
Code: [Select]

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

illbatt

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 ......

ash901226

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

illbatt

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

peetsyo

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

Code: [Select]
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;}
}
}
}

UKHeliBob

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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up