-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskech-rotaryencoding.ino
63 lines (57 loc) · 1.45 KB
/
skech-rotaryencoding.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
int pinA=2;
int pinB=4;
int preValA=-1;
int preValB=-1;
int rotation=0;
int JudgeHighLow(int a){
if(a==HIGH)return 1;
else return 0;
}
int JudgeRotate(int a,int b, int preA, int preB){
if(preA==0 && preB==0){
if(a==1 && b==0)return 1;
else if(a== 0 && b==1)return -1;
else return 0;
}
if(preA==1 && preB==0){
if(a==1 && b==1)return 1;
else if(a== 0 && b==0)return -1;
else return 0;
}
if(preA==1 && preB==1){
if(a==0 && b==1)return 1;
else if(a== 1 && b==0)return -1;
else return 0;
}
if(preA==0 && preB==1){
if(a==0 && b==0)return 1;
else if(a== 1 && b==1)return -1;
else return 0;
}
}
void setup() { //一回だけ実行
pinMode(13, OUTPUT); //LEDを接続した13番ピンを出力用に設定する
Serial.begin(9600);
pinMode(pinA, INPUT);
pinMode(pinB, INPUT);
preValA=JudgeHighLow(digitalRead(pinA));
preValB=JudgeHighLow(digitalRead(pinB));
}
void loop() { //{}内を無限ループで実行
//digitalWrite(13, HIGH);
int valA=JudgeHighLow(digitalRead(pinA));
int valB=JudgeHighLow(digitalRead(pinB));
rotation += JudgeRotate(valA, valB, preValA, preValB);
if (rotation<0) rotation+=5000;
else rotation%=5000;
Serial.println(rotation, DEC);
preValA=valA;
preValB=valB;
/*
Serial.write('0' + valA);
Serial.write(' ');
Serial.write('0' + valB);
Serial.write(n);
*/
//delay(500); //1000ms(1秒)待ちます
}