#include <Arduino.h>
#include <RotaryEncoder.h>
#define LED_PIN1 2
#define LED_PIN2 3
#define LED_PIN3 4
#define LED_PIN4 5
#define BUTTON_PIN 7
byte lastButtonState = LOW;
byte ledState = LOW;
#if defined(ARDUINO_AVR_NANO) || defined(ARDUINO_AVR_NANO_EVERY)
// Example for Arduino UNO with input signals on pin 2 and 3
#define PIN_IN1 A2
#define PIN_IN2 A3
#endif
RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::TWO03);
void setup()
{
// Serial.begin(9600);
pinMode(LED_PIN1, OUTPUT);
pinMode(LED_PIN2, OUTPUT);
pinMode(LED_PIN3, OUTPUT);
pinMode(LED_PIN4, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
// Read the current position of the encoder and print out when changed.
void loop()
{
// static int pos = 0;
encoder.tick();
byte buttonState = digitalRead(BUTTON_PIN);
int newPos = encoder.getPosition();
//if (pos != newPos) {
// Serial.print("pos:");
//Serial.print(newPos);
switch (newPos){
case 0:
if (buttonState != lastButtonState) {
lastButtonState = buttonState;
if (buttonState == LOW) {
ledState = (ledState == HIGH) ? LOW: HIGH;
digitalWrite(LED_PIN1, ledState);
}
}
break;
case 2:
// byte buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState) {
lastButtonState = buttonState;
if (buttonState == LOW) {
ledState = (ledState == HIGH) ? LOW: HIGH;
digitalWrite(LED_PIN2, ledState);
}
}
break;
case 4:
if (buttonState != lastButtonState) {
lastButtonState = buttonState;
if (buttonState == LOW) {
ledState = (ledState == HIGH) ? LOW: HIGH;
digitalWrite(LED_PIN3, ledState);
}
}
break;
case 6:
// byte buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState) {
lastButtonState = buttonState;
if (buttonState == LOW) {
ledState = (ledState == HIGH) ? LOW: HIGH;
digitalWrite(LED_PIN4, ledState);
}
}
break;
}
}// loop ()
// The End