Correctly using a tactile switch

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
gerry81611
 
Posts: 96
Joined: Wed Jun 11, 2014 2:04 pm

Correctly using a tactile switch

Post by gerry81611 »

Hello,

I have one of these:
https://www.adafruit.com/products/1092

that I am hooking up to a Flora / Gemma and a 24 pixel ring. I want the switch to change light modes on the ring. I have successfully written some code that correctly detects each click (hey, i'm new, small victories).

if i click the switch from low to high "click" is printed once. if i click the switch from high to low, "click" is printed once. however if i hold the switch in the middle and wiggle it, "click" is printed several times. what gives? i'm new to all of this, but is this a job for a pull up resistor or something?

here is my code:

Code: Select all

#include <Adafruit_NeoPixel.h>

#define RING_SIGNAL_PIN 6

Adafruit_NeoPixel strip = Adafruit_NeoPixel(24, RING_SIGNAL_PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

int buttonPin = 9;
int buttonState;
int lastButtonState;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
//  pinMode(buttonPin, INPUT);
//  digitalWrite(buttonPin, HIGH);
  
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
      buttonCheck();
}

void buttonCheck() {
  lastButtonState = buttonState;
  buttonState = digitalRead(buttonPin);
  
  if (buttonState != lastButtonState) {
      Serial.print("click:  time: ");
      Serial.print(millis());
      Serial.print("\n");
  }
}
thank you,


gerry

User avatar
Franklin97355
 
Posts: 23911
Joined: Mon Apr 21, 2008 2:33 pm

Re: Correctly using a tactile switch

Post by Franklin97355 »

The switch is bistable one click is on the next is off but in between the contacts could be in either state so "click, click"

User avatar
gerry81611
 
Posts: 96
Joined: Wed Jun 11, 2014 2:04 pm

Re: Correctly using a tactile switch

Post by gerry81611 »

thanks!

is there anything that I can do I code or hardware to keep the switch from firing in the middle?


g

User avatar
tastewar
 
Posts: 408
Joined: Thu Mar 17, 2011 10:16 am

Re: Correctly using a tactile switch

Post by tastewar »

I think the general idea you're looking for is "debouncing" and in software it generally means ignoring changes of state that are faster than you feel are appropriate, so you associate a timestamp with the most recent change and disallow others that occur "too soon."

User avatar
gerry81611
 
Posts: 96
Joined: Wed Jun 11, 2014 2:04 pm

Re: Correctly using a tactile switch

Post by gerry81611 »

thank you! i've seen that term in sample code and will look into it more, that sounds perfect. i could set it up to ignore clicks unless it had been half a second or more since the last click.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”