Go Down

Topic: suggestions for Debounce on analog switches (Read 3695 times) previous topic - next topic

PeterH,
I appreciate your detailed explanation of debounce, but sadly my brain isn't processing it. could I trouble for a raw example of how the program might be set up, (If you have the time). I have tried adapting the arduino example but it returns two values for every press and I only want one.  I would also like it to only return a value once the button has been pressed

majenko

I have just started writing a handy "Average" library.

At the moment it just deals with rolling averages.  I have used it with this piece of analog button debounce code:

Code: [Select]

#include <Average.h>

#define SMOOTH 10

int history[SMOOTH];

int bvals[] = {1023,819,768,683,513,0};

int analogButton(int channel, int *vals, int nvals)
{
  int value;
  int lastValue = 0;
  int l;
  while(1)
  {
    value = rollingAverage(history,SMOOTH,analogRead(channel));
    if(value == lastValue)
    {
      for(l=0; l<nvals; l++)
      {
        if(value == vals[l])
        {
          return l;
        }
      }
    }
    lastValue = value;
  }
}

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  int value;
  static int lastValue = 0;
 
  value = analogButton(0,bvals,6);
  while(lastValue == value)
  {
    value = analogButton(0,bvals,6);
  }
  lastValue = value;
  Serial.println(value);
}


You will need my Average library for this: http://hacking.majenko.co.uk/libs/average


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy