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);
laststateA = digitalRead(encoderA);
laststateSW = digitalRead(SW);
Serial.begin(9600);
}
void loop() {
int stateA = digitalRead(encoderA);
if(stateA != laststateA){
int stateB = digitalRead(encoderB);
if(stateA == LOW && stateB == HIGH){
cnt++;
Serial.print(" Rotated clockwise << ");
Serial.print(cnt);
}
if(stateA == LOW && stateB == LOW){
cnt--;
Serial.print(" Rotated clockwise << ");
Serial.print(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);
}
else
{
Serial.println("now sw is opend!")
}
laststateSW = SW_state;
}
}
//counterwise 반시계가 되는거 고치기
//실제 dc모터가아니라 이런 현상 발생