2020-10-22 21:30:21 +02:00
|
|
|
const int buttonPin = 4;
|
|
|
|
|
|
|
|
int buttonState;
|
|
|
|
int lastButtonState = HIGH;
|
|
|
|
|
|
|
|
unsigned long lastDebounceTime = 0;
|
|
|
|
unsigned long debounceDelay = 50;
|
|
|
|
|
|
|
|
Encoder rotEncoder(3, 2);
|
2020-10-14 00:09:12 +02:00
|
|
|
|
|
|
|
void Encoder_Init(void) {
|
2020-10-22 21:30:21 +02:00
|
|
|
pinMode(buttonPin, INPUT_PULLUP);
|
|
|
|
|
2020-10-14 00:09:12 +02:00
|
|
|
Serial.print("Encoder position: ");
|
|
|
|
Serial.println(EncPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Encoder_Task(void) {
|
2020-10-22 21:30:21 +02:00
|
|
|
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;
|
|
|
|
|
2020-10-14 00:09:12 +02:00
|
|
|
long newEncPos;
|
|
|
|
newEncPos = rotEncoder.read();
|
2020-10-22 21:30:21 +02:00
|
|
|
rotEncoder.write(0);
|
|
|
|
|
2020-10-14 00:09:12 +02:00
|
|
|
|
2020-10-22 21:30:21 +02:00
|
|
|
if (newEncPos > 0) {
|
|
|
|
Serial.println("Encoder right");
|
2020-10-14 00:09:12 +02:00
|
|
|
EncPos = newEncPos;
|
|
|
|
}
|
2020-10-22 21:30:21 +02:00
|
|
|
|
|
|
|
if (newEncPos < 0) {
|
|
|
|
Serial.println("Encoder left");
|
|
|
|
}
|
2020-10-14 00:09:12 +02:00
|
|
|
}
|