const int L_LED = 13;
const int encoderA = 2;
const int encoderB = 3;
const int SW = 4;
int lastStateA;
int lastStateSW;
long cnt = 0;
void setup() {
pinMode(L_LED, OUTPUT);
pinMode(encoderA, INPUT);
pinMode(encoderB, INPUT);
pinMode(SW, INPUT_PULLUP); //pinMode(SW, INPUT);
lastStateA = digitalRead(encoderA);
lastStateSW = digitalRead(SW);;
Serial.begin(9600);
}
void loop() {
int StateA = digitalRead(encoderA);
if (StateA != lastStateA) { // 엔코더A의 상태가 변하면
int StateB = digitalRead(encoderB);
if (StateA == LOW && StateB == HIGH) {
cnt++;
Serial.println("Rotated clockwise ⏩");
Serial.println(cnt);
}
if (StateA == LOW && StateB == LOW) {
cnt--;
Serial.println("Rotated counterclockwise ⏪");
Serial.println(cnt);
}
lastStateA = StateA;
}
int sw_state = digitalRead(SW);
if(lastStateSW != sw_state)
{
if(sw_state == LOW)
{
cnt = 0;
Serial.println("cnt Reset");
Serial.println(cnt);
}
lastStateSW = sw_state;
}
}