Contact debounce (millis) plus delayed off state


hi, use david mellis's code success debounce:

code: [select]

  // read ldra , debounce
  int readingldra = digitalread(ldra);
  if (readingldra != laststateldra) {
    lastdebouncetimeldra = millis();
  }
  if ((millis() - lastdebouncetimeldra) > debouncedelay) {
    if (readingldra != stateldra) {
      stateldra = readingldra;
    }
  }
laststateldra = readingldra;



but when want introduce within routine delayed output when debounced input goes high -> low hit brick wall. here 1 of attempts:

code: [select]

  // read ldra , debounce
  int readingldra = digitalread(ldra);
  if (readingldra != laststateldra) {
    lastdebouncetimeldra = millis();
  }
  if ((millis() - lastdebouncetimeldra) > debouncedelay) {
        if (readingldra != stateldra) {
          lastoffdelayldra = millis();}
     
        if (readingldra == low) {
          stateldra = readingldra;}
        else {
           if ((millis()-lastoffdelayldra) > (offdelay)) {
          stateldra = readingldra;}
        }
  }


the purpose debounce contact both l->h h->l (mellis's code) want introduce delay (offdelay) between input (readingldra) , output (stateldra) when input goes h-l.

what wrong in code? appears if stateldra never changes h l.

you need separate state change detection/debounce code "use state change purpose" code.

you detest state change occurred, , state change occurred reasonable amount of time after last state change. if case, set flag.

later, check flag, , use fact state changed, immediately, or later, depending on when state changed.

if explained why want bizarre, perhaps more provided.


Arduino Forum > Using Arduino > Programming Questions > Contact debounce (millis) plus delayed off state


arduino

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino Uno not uploading

Indesign and MathType fonts