Sep 022012
I was curious to see if the new switches were prone to switch bounces. This is why I wrote a quite simple Teensy program that can count switch bounces. The debounce example code that come with the Arduinio IDE was used as a template.
This is quite simple, and just here to make it simple to redo this kind of test when I get new types of switches.
S1 in the schematic is the switch you want to test. The resistor is used as a pull down resistor, to make sure that the input pin is properly grounded when the button is released.
The result for my switches is somewhat unstable. I get just one count most of the times, but sometimes up to 9 bounces. The end result is that I need to debounce them properly to eliminate weird results in my builds.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
// Count bounces // Open the serial monitor in the Arudino IDE, it will be used for count feedback const int buttonPin = 2; // Teensy pin to connect the switch to const int ledPin = 11; // Built in LED for visual feedback int buttonState; int lastButtonState = LOW; long lastDebounceTime = 0; long debounceDelay = 100; // Bounce period set to 0.1s int bounces = 0; int lastLedState = LOW; void setup() { pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); Serial.begin(9600); // Open the serial port to write the bounce counts to } void loop() { int reading = digitalRead(buttonPin); if (reading != lastButtonState) { lastDebounceTime = millis(); bounces++; // Count bounces } if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != lastLedState) { Serial.println(bounces); // Write # bounces to serial console bounces = 0; // Zero the counter lastLedState = reading; } } digitalWrite(ledPin, lastLedState); lastButtonState = reading; } |
