First, I want to thank you for reading this post. I understand that it is a simple/common problem with a lot of literature on it, and I don’t take your time lightly. Thank you. I have screamed over this (but just once.)
The Goal:
I am trying to write a low-latency, de-bounced button routine in Arduino. I want to write to the serial port, so that a second Processing program can read that serial information and trigger various sounds in real time, accordingly.
What I’ve tried:
I’ve tried a NUMBER of debouncing variations, including, but not limited to, the solutions here:
(I have to de-URL these, as this is my first post, and the bb won’t let me)
playground/Code/Debounce
playground/Learning/SoftwareDebounce
playground/Code/Buttons
and in the book “Getting Started witH Arduino”
It seems no matter what I do, I end up with two problems
a. Latency in the Serial.printing (regardless of de-bounce, actually)
b. Unresponsiveness of the button.
Latency goes away, IF I take away all debouncing, and just read the status of the pin, and then Serial.print…
My system: OS 10.5.6, Duemilanove, Arduino 015.
Any help would be greatly appreciated.
Here is the code to my current “solution”, which is a solution the same way that golf is a sport. Which is to say, kinda, but not really.
(No offense to Golfers, I understand that it is very difficult.)
#define LED 13
#define BUTTON 7
#define BOUNCE_PERIOD 150
long last_read_time;
long last_value;
int val = 0;
void setup()
{
Serial.begin(115200);
pinMode(LED,OUTPUT);
pinMode(BUTTON,INPUT);
}
void loop()
{
// If we are past our bounce period
if (millis() - last_read_time > BOUNCE_PERIOD){
// Check the button
val = digitalRead(BUTTON);
// If the button has changed since last bounce period
if (val != last_value){
digitalWrite(LED,val);
if (val == HIGH) Serial.print("7");
last_read_time = millis();
last_value = val;
}
}
}