#define ENCODER_A 2//엔코더a
#define ENCODER_B 3//엔커더b
#define ENCODER_SW 4//스위치
volatile int cnt = 0;
int lastA;
void setup() {
pinMode(ENCODER_A, INPUT);
pinMode(ENCODER_B, INPUT);
pinMode(ENCODER_SW, INPUT);
Serial.begin(115200);
lastA = digitalRead(ENCODER_A);//엔코더 A의 초기값설정
}
void loop() {
int newA = digitalRead(ENCODER_A);
if (newA != lastA) {//엔코더 A에 변화가 있으면
lastA = newA;//이전 엔코더 A의 상태 업데이트
int newB = digitalRead(ENCODER_B);
if (newA == LOW && newB == HIGH) {
cnt++;
Serial.println(cnt);
Serial.println("Rotated clockwise ⏩");
}
if (newA == LOW && newB == LOW) {
cnt--;
Serial.println(cnt);
Serial.println("Rotated counterclockwise ⏪");
}
}
}