01-03-2018, 12:40 AM
Trenutno koristim ovakav debounce, ja sam zadovoljan, mislim radi dobro za dosadašnje potrebe:
.
Code:
const int buttonPin1 = 10;
const int buttonPin2 = 13;
volatile byte state = LOW;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonStateInk;
int lastButtonState1 = LOW;
int buttonStateDek;
int lastButtonState2 = LOW;
unsigned long lastDebounceTime = 0; // poslednje vreme kada je tater pritisnut
unsigned long debounceDelay = 50; // debounce vreme u milisekundama
void setup() {
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop(){
int v1 = digitalRead(buttonPin1);
int v2 = digitalRead(buttonPin2);
//radi Debounce tastera za inkremniranje
if (v1 != lastButtonState1) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (v1 != buttonStateInk) {
buttonStateInk = v1;
if (buttonStateInk == HIGH) vrijeme++;
}
}
//radi Debounce tastera za dekremntiranje
if (v2 != lastButtonState2) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (v2 != buttonStateDek) {
buttonStateDek = v2;
if (buttonStateDek == HIGH) vrijeme--;
}
}
lastButtonState1 = v1;
lastButtonState2 = v2;
}