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

I am trying to come up with a debounce for an analog switch. So far, the program I have written seems to agree with my logic about 66% of the time. I want to think that the other 33% the time it is taking false readings from a loose breadboard and bad connections. Also, on occasion, it will continue to spit out zeros until I input a solid reading. In any case, I would appreciate some advise to improve this.

Code:
const int  buttonPin = A0;  
int buttonstateCounter = 0;
int lastanalogvalue = 0;
int analogvalue = 0;
int difference;
long previousMillis = 0;
long interval = 1000;
 
void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
}


void loop() {    
 
  analogvalue = analogRead(buttonPin); // read the pushbutton input pin
  difference = abs(lastanalogvalue - analogvalue);
  if (difference > 5 ) { // button has either been pressed or released
  
      buttonstateCounter++;
      if (buttonstateCounter % 2 != 0)// button pressed, take value
     {
       unsigned long currentMillis = millis();
       if(currentMillis - previousMillis > interval)
       {
         previousMillis = currentMillis;
         Serial.println (analogvalue);
       }
     }        
  lastanalogvalue = analogvalue;  
  }
}

and here is serial monitor output (anything less the ~160 is some sort of error):

804
980
694
172
0
0
11
12
178
382
587
980
14
586
177
382
« Last Edit: May 28, 2012, 01:52:43 pm by seanz2003 » Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6132
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What do you mean by "analog switch"?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

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

You really should think before creating your subject...

Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 65
Posts: 2107
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I agree, was posting the same thing, lol


* Capture.PNG (3.7 KB, 593x73 - viewed 34 times.)
Logged


Inland Empire, California, US
Offline Offline
Sr. Member
****
Karma: 6
Posts: 279
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, sorry about that. I must clarify... I should have said  that there are multiples switches connected to one analog pin through their own unique resistor. I am trying to use one analog pin to do different things. the rest of this program has already been posted multiple times on this forum so I didn't want people to get sick of seeing it. ("Oh, this guy again...")
 The increments between button values  is approximately 150, though some are more than 200 because of the values of the resistors I had on hand.
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6132
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In that case, it appears you are leaving the pin floating when no button is pressed.  You should fix that with hardware, not software.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Inland Empire, California, US
Offline Offline
Sr. Member
****
Karma: 6
Posts: 279
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Truly sorry, I did not mean to be offensive :/ does that happen with the titles very often? only with newbies right
« Last Edit: May 28, 2012, 02:22:30 pm by seanz2003 » Logged

Inland Empire, California, US
Offline Offline
Sr. Member
****
Karma: 6
Posts: 279
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

and I should have stated that the analog pin has a 10K ohm pull down resistor. boy I am on a roll...
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6132
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

and I should have stated that the analog pin has a 10K ohm pull down resistor.
You might want to draw up a schematic and post it.  Again, the behavior you describe suggests the pin is floating.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

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

A circuit diagram would be nice.

This is how I do my analogue switches:



All the resistors have the same (10K) value.  I get:

All off - 1024
S1 on - 820
S2 on - 768
S3 on - 683
S4 on - 513
S5 on - 0

Combinations take the lowest value.

Then I average the results of my reading - depending on the program I am using that might just be do a number of reads and average them, or do a constant rolling average of the reads, using say 10 data points.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want to debounce it then simplest general algorithm is to wait until it has provided consistent values for some period. You will have to decide how long that is - for example 0.1 seconds might be appropriate.

To implement that you would do your resistance comparisons to convert the input analog value to one of the switch positions, presumably held as an enum.

Your debounce algorithm takes a raw switch position as an input and produces a debounced switch position as the output.
It uses a state variable recording the input switch position at the last sample, and the time when the input switch position last changed, and the current debounced position.

Each time you read the analog value and convert it to a raw switch position, apply your debounce algorithm to it:

  • If the raw position is the same as the previous sample and the last change happened long enough ago, the debounced state changes to the new position.
  • If the current position is different to the previous position, record the new position and note the time when this change happened.
  • Record the raw position for next time.
  • Return the debounced position.

Note that the debounce algorithm treats the 'switch not pressed' state exactly the same as any other state - you have to take your hand off the switch long enough for your debounced state to change to 'not pressed' before you tell your application the switch has stopped being pressed.

You would put this debouncing code in front of the code discussed on your other thread which looks for changes in the debounced state.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Inland Empire, California, US
Offline Offline
Sr. Member
****
Karma: 6
Posts: 279
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How do i post an image? i see the button but how do i use it?
Logged

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

I'm not sure.  I host the images on my own web server and just paste in the URL inside [img] tags.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6132
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Reply to this thread.  click "Additional Options..."  And then "Attach"
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Inland Empire, California, US
Offline Offline
Sr. Member
****
Karma: 6
Posts: 279
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


 ah it worked. I know this hardware set up works , i have used it with other examples without any issues with floating pins. for instance this code works very reliably:
Code:
const int sensorPin = A0;
const int ledPin = 9;
const int threshold1 = 50;   // an arbitrary threshold level that's in the range of the analog input
const int threshold2 = 200;
const int threshold3 = 400;
const int threshold4 = 540;
const int threshold5 = 640;
const int threshold6 = 740;
const int threshold7 = 900;
int sensorValue = 0;
const int sensorMin = 0;      // sensor minimum, discovered through experiment
const int sensorMax = 1023;

  void setup() {
  // turn on LED to signal the start of the calibration period:
    Serial.begin(9600);
    pinMode(sensorPin, INPUT);
  }
  void loop() {
    sensorValue = analogRead(sensorPin);
    Serial.print(sensorValue);
    int range = map(sensorValue, sensorMin, sensorMax, 0, 9);
    delay(500);
    switch (range) {
      case 0:    //
        Serial.println("0"); //0-102
        break;
      case 1:    // 54k ohm - 175
        Serial.println("r");
        break;
      case 2:    // 205-306
        Serial.println("0");
        break;
      case 3:    // 307-409 18.5k ohm 378
        Serial.println("c");
        break;
      case 4:    // 410-511
        break;
      case 5:    // 512-613 8.5k ohm 579
        Serial.println("e");
        break;
      case 6:    // 614-716 5.5k ohm 682
        Serial.println("s");
        break;
      case 7:    // 717-818 3k ohm 807
        Serial.println("f");
        break;
      case 8:    // 819-920 1.2k ohm 913
        Serial.println("ph adjust");
        break;
      case 9:    // 921-1023 0 ohm
        Serial.println("auto");
        break;
        
    }
    }
but nevertheless, it is a valid consideration. it could also be floating from bad connections.
« Last Edit: May 28, 2012, 03:39:23 pm by seanz2003 » Logged

Pages: [1] 2   Go Up
Jump to: