void TTBOUNCE::update(){ uint8_t pinState = digitalRead(_pin); //debounce if (pinState != _currentPinUnstableState){ _timestamp = millis(); } else if(millis() - _timestamp >= _debounceInterval){ if (pinState != _currentPinState){ _currentPinState = pinState; if(read()){ _previousHighStateTime = millis(); } } } _currentPinUnstableState = pinState; //states if (_state == 0){ if (read() == HIGH){ _state = 1; _timestamp = millis(); // remember starting time } } else if (_state == 1){ if (read() == LOW){ _state = 2; } else if ((read() == HIGH) && (millis() > _timestamp + _pressInterval)){ if (_pressFunction) _pressFunction(); _state = 4; } } else if (_state == 2){ if (millis() > _timestamp + _clickInterval || (read() == LOW && !_doubleClickFunction)){ if (_clickFunction) _clickFunction(); _state = 0; } else if (read() == HIGH){ _state = 3; } } else if (_state == 3){ if (read() == LOW){ if (_doubleClickFunction) _doubleClickFunction(); _state = 0; } } else if (_state == 4){ if (read() == LOW){ _state = 0; } } }
void TTBOUNCE::update() { uint8_t pinState = digitalRead(_pin); // debounce if (pinState != _currentPinUnstableState) { _timestamp = millis(); } else if (millis() - _timestamp >= _debounceInterval) { if (pinState != _currentPinState) { _currentPinState = pinState; if (read()) { _previousHighStateTime = millis(); } } } _currentPinUnstableState = pinState; // states switch (_state) { case 0: if (read() == HIGH) { _state = 1; _timestamp = millis(); // remember starting time } break; case 1: if (read() == LOW) { _state = 2; } else if ((read() == HIGH) && (millis() > _timestamp + _pressInterval)) { if (_pressFunction) _pressFunction(); _state = 4; } break; case 2: if (millis() > _timestamp + _clickInterval || (read() == LOW && !_doubleClickFunction)) { if (_clickFunction) _clickFunction(); _state = 0; } else if (read() == HIGH) { _state = 3; } break; case 3: if (read() == LOW) { if (_doubleClickFunction) _doubleClickFunction(); _state = 0; } break; case 4: if (read() == LOW) { if (_releaseFunction) _releaseFunction(); _state = 0; } else { if (_reTickFunction) { if (millis() - _previousReTickTime > _reTickInterval) { _reTickFunction(); _previousReTickTime = millis(); } } } break; } }