Debounce + StateChange [SOLVED]

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 !

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

   }

How is the button wired ?

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.

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.

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

They're system-software engineers 8)

They're weird

Can't argue with that.

Try this and note the differences

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

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?

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

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.

heres one

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

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

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

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

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

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

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.