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");
}
}
gerry