Go Down

Topic: Spotting a semantic issue in a debounce (Read 761 times) previous topic - next topic

Paul Beaudet

I'm having a hard time finding out why this will give the correct values for presses, but not releases and holds.
I ether missed an important concept or am having a timing issue.

Code: [Select]
//getvalueTesting
//4 buttons wired pins 2-5
//pin to tactile button to 330k to ground
//uses internal resistence to VCC
//
//goal is an expample that gathers debounced state information
//and combines every switch state within a time window
//into an int value
//ie, buttonstate[4]={1,2,5,1} --> int=1251

byte buttons[]={
  2,3,4,5};
#define NUMBUTTONS sizeof(buttons)

#define BOUNCE 15// time to debounce
#define REGULAR 225
boolean regularEvent[]={
false,false,false,false};

#define HOLD 450
boolean holdEvent[]={
false,false,false,false};

#define LONG 900
boolean longEvent[]={
false,false,false,false};

#define WINDOW 1000

long past=-1;
long diff=-1;
int windowCount=0;

unsigned long releaseTime[]= {
  -1,-1,-1,-1};
unsigned long pressTime[]= {
  -1,-1,-1,-1};
boolean value[]= {
  LOW,LOW,LOW,LOW};
boolean lastValue[]= {
  LOW,LOW,LOW,LOW};
 
boolean pressed[]= {
  false,false,false,false};
 

void setup()
{
  Serial.begin(9600);
  delay(2000);
  //wait to bring up serial monitor
  //------------input setting
  for (byte set=0;set<NUMBUTTONS;set++)
  {
    //sets the button pins
    pinMode(buttons[set],INPUT);
    digitalWrite(buttons[set],HIGH);
  }
}

void loop()
{
  while(windowCount<=WINDOW)
  // the time window is for the purpose of compiling a
  // unique value from the button over a specified period of time 
  {
    unsigned long currentTime=millis();
    diff=currentTime-past;
    windowCount+=diff;
    for(int i=0;i<NUMBUTTONS;i++)
    {
      value[i]=digitalRead(buttons[i]);
      if(value[i]==LOW && lastValue[i]==HIGH && (millis()-releaseTime[i]) > BOUNCE)
      {
        pressTime[i] = millis();
        pressed[i]=true;
        regularEvent[i]=false;
        holdEvent[i]=false;
        longEvent[i]=false;
        Serial.print(i);
        Serial.println("-pressed");
      }
      else if (value[i] == HIGH && lastValue[i] == LOW && (millis() - pressTime[i]) > BOUNCE)
      {
        if(pressed[i])
          {       
            releaseTime[i] = millis();
            Serial.print(value[i]);
            //!! I only get the value 1-released
            //inconceivable!
            //I dont think this means what I think it means
            Serial.println("-Release");
          }
      }
      if (value[i]==LOW && (millis()-pressTime[i]) >= REGULAR)
      {
        if(!regularEvent[i])
        {
          //quick press
          regularEvent[i]=true;
          //!!only getting 0-regular press in output
          Serial.print(value[i]);
          Serial.println("-regular press");
        }
        if((millis()-pressTime[i])>= HOLD)
        {
          if (!holdEvent[i])
          {
            holdEvent[i]=true;
            //!!only getting 0-hold event in output
            Serial.print(value[i]);
            Serial.println("-hold event");
          }
        }
      }
      lastValue[i]=value[i];
    }
    past=currentTime;
  }
  //--outside time window
  Serial.print("window-");
  Serial.println(windowCount);
  windowCount=0;
}

with that code, here is what a sample of the output looks like this
Code: [Select]
window-1001
3-pressed
0-regular press
0-hold event
window-1001
1-Release
window-1001

fine, except the 0 and 1 button were never pressed

more description of purpose is in the code
I tried the bounce library... and I just found that I probably need get more familiar with the core concepts then try to take a shortcut. (and it was working inconsistently but that would be a topic for another thread)

Any ideas on why holds and releases result in static values?


johncc


//pin to tactile button to 330k to ground


Do the button inputs work reliably with that high a resistor in there?

Paul Beaudet


Do the button inputs work reliably with that high a resistor in there?

they have thus far as far as I can tell and I've done a bit of testing

oh... wait that's a typo they are 330om not 330k

PeterH


I'm having a hard time finding out why this will give the correct values for presses, but not releases and holds.


Your code dealing with windowCount looks wierd. Why don't you just calculate the difference between now and when the window started? WHy calculate elapsed time per loop and then add them up?

In the context of a system where nothing changes for long intervals (relative to the window size) when is the 'window time' supposed to start?

Exactly what is the code trying to do? It seems to be dealing with press, release,hold, bounce, regular and with flags and times associated with various of those, but it's not obvious how the final output value is supposed to be related to the physical button presses and releases. Ignoring the code itself, and assuming I know nothing about your project, what is this code trying to do?
I only provide help via the forum - please do not contact me for private consultancy.

Paul Beaudet

#4
Feb 01, 2013, 10:49 pm Last Edit: Feb 01, 2013, 11:01 pm by Paul Beaudet Reason: 1

Your code dealing with windowCount looks wierd. Why don't you just calculate the difference between now and when the window started? WHy calculate elapsed time per loop and then add them up?

it has more to do with the creating a chord then the debounce, sorry its kinda misleading for this question.
Quote

In the context of a system where nothing changes for long intervals (relative to the window size) when is the 'window time' supposed to start?

I see what you are getting at there, I didn't think about it because in my last implementation it worked its self out somehow. The window is supposed to start when values start being detected. For right now this works well enough for me test things out. Just shows whats falling within the window.

Quote
Exactly what is the code trying to do? It seems to be dealing with press, release,hold, bounce, regular and with flags and times associated with various of those, but it's not obvious how the final output value is supposed to be related to the physical button presses and releases. Ignoring the code itself, and assuming I know nothing about your project, what is this code trying to do?

the goal is to debounce over multiple button instances first and foremost then also flag different press types and seed* the press type based on other button states
The later two objectives I'm working on currently and not in the op

*- if the other buttons are active, the monitored button's reported value is augmented by this to report a unique value. That unique value becomes a key in a chord

the code is basically trying to prove that I can debounce multiple buttons and construct an int "chordValue" from it

Go Up