Folks, I have read and tested several press/hold examples posted by others, and most of them work as advertised, yet don't do exactly what I would like to do. Also, some seem more complicated than they need to be. So, I tried my hand at it, but am beating my head against the wall at this point trying to determine why what I have doesn't work...
So, the simple objective is to have a button which, when you hold it down for 3 seconds, stores a sensor setpoint, and if you press it more quickly, it drives a motor to that previously stored setpoint. I have working motor code and sensor modules, but the press/hold portion just isn't working. The results are spurious so I'm thinking it's a debounce issue, perhaps on the release rather than the press, since I do have some treatment on the press, but can't figure what I might need on the release.
Here's just the simple press/hold with a serial print of "short" and "long" to represent the system behavior:
const int A = 2; // Button A on pin 2
const long Bounce = 20; // Debounce time in milliseconds
const long Interval = 3000; // Time threshold for button "hold"
int StateA = HIGH; // Current state of button A
int PrevA = HIGH; // Previous state of button A
long DnTimeA = 0; // When did button A get pushed down
void setup() {
Serial.begin(9600);
pinMode(A, INPUT);
digitalWrite(A,HIGH); // Note that buttons will be "LOW" when pushed
}
void loop() {
StateA = digitalRead(A);
if (StateA==LOW && PrevA==HIGH && millis() - DnTimeA > Bounce) {
DnTimeA = millis();
}
if (StateA==HIGH && PrevA==LOW && millis() - DnTimeA < Interval) {
Serial.print("SHORT");
Serial.println();
}
if (StateA==HIGH && PrevA==LOW && millis() - DnTimeA > Interval) {
Serial.print("LONG");
Serial.println();
}
PrevA = StateA;
}
Here, you're detecting the initial press of the key; you're not interested in any previous timings, just in setting the time that you detected the key press.
I can't help thinking here, that after a press (say at time 100), and then it bounces (say at time 101), you have now set the "PrevA = StateA;". So, this is still counted as a short press. Also, since you have set PrevA back to HIGH, it won't detect longer bounces.
Your debounce only detects a HIGH/LOW bounce, not a LOW/HIGH bounce.
@AWOL - thanks for your reply. I'm not certain if you are telling me that my code is trying to pay attention to previous states and it should not be, or if I'm not, and I should
The pseudo code I was attempting to implement is:
If the button is down (first or any time) record dntime// if first time, millis is always > dntime because setup to 0
If the button is continuously held, State and Prev == LOW/LOW // hence nothing in the loop will catch
When the button is released one or the other of the following if's will catch and get "short" or "long"
I may have a logic error here, but I definitely think I have a bounce, because structured button pushing gives different results every time I start the project.
I can say too that I'm having a heck of a time orienting my pea brain to the idea of using the internal pullup resistors and thinking of LOW as pushed. I keep running in circles when trying to step through the code mentally. Is there no stepthrough capabilities with the arduino IDE?