Go Down

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

seanz2003

May 28, 2012, 07:57 pm Last Edit: May 28, 2012, 08:52 pm by seanz2003 Reason: 1
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: [Select]

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

James C4S

What do you mean by "analog switch"?
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

majenko

You really should think before creating your subject...

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

pYro_65

I agree, was posting the same thing, lol

seanz2003

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.

Go Up