Pages: 1 [2]   Go Down
Author Topic: suggestions for Debounce on analog switches  (Read 2310 times)
0 Members and 1 Guest are viewing this topic.
Inland Empire, California, US
Offline Offline
Full Member
***
Karma: 4
Posts: 241
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Inland Empire, California, US
Offline Offline
Full Member
***
Karma: 4
Posts: 241
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

very cool, thanks
Logged

Pages: 1 [2]   Go Up
Jump to: