Taster mit 50ms entprellung
This commit is contained in:
parent
d296d7b810
commit
9b674f0c56
|
@ -1,18 +1,52 @@
|
||||||
Encoder rotEncoder(2, 3);
|
const int buttonPin = 4;
|
||||||
|
|
||||||
|
int buttonState;
|
||||||
|
int lastButtonState = HIGH;
|
||||||
|
|
||||||
|
unsigned long lastDebounceTime = 0;
|
||||||
|
unsigned long debounceDelay = 50;
|
||||||
|
|
||||||
|
Encoder rotEncoder(3, 2);
|
||||||
|
|
||||||
void Encoder_Init(void) {
|
void Encoder_Init(void) {
|
||||||
|
pinMode(buttonPin, INPUT_PULLUP);
|
||||||
|
|
||||||
Serial.print("Encoder position: ");
|
Serial.print("Encoder position: ");
|
||||||
Serial.println(EncPos);
|
Serial.println(EncPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Encoder_Task(void) {
|
void Encoder_Task(void) {
|
||||||
|
int reading;
|
||||||
|
reading = digitalRead(buttonPin);
|
||||||
|
|
||||||
|
if (reading != lastButtonState) {
|
||||||
|
lastDebounceTime = millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((millis() - lastDebounceTime) > debounceDelay) {
|
||||||
|
if (reading != buttonState) {
|
||||||
|
buttonState = reading;
|
||||||
|
|
||||||
|
if (buttonState == LOW) {
|
||||||
|
Serial.println("Button pressed");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lastButtonState = reading;
|
||||||
|
|
||||||
long newEncPos;
|
long newEncPos;
|
||||||
newEncPos = rotEncoder.read();
|
newEncPos = rotEncoder.read();
|
||||||
|
rotEncoder.write(0);
|
||||||
|
|
||||||
|
|
||||||
if (newEncPos != EncPos) {
|
if (newEncPos > 0) {
|
||||||
Serial.print("Encoder position = ");
|
Serial.println("Encoder right");
|
||||||
Serial.print(newEncPos);
|
|
||||||
Serial.println();
|
|
||||||
EncPos = newEncPos;
|
EncPos = newEncPos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (newEncPos < 0) {
|
||||||
|
Serial.println("Encoder left");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue