1
8int pinA = 3;
9int pinB = 4;
10int
11 encoderPosCount = 0;
12int pinALast;
13int aVal;
14boolean bCW;
15
16 void
17 setup()
18 {
19
20 pinMode (pinA,INPUT);
21
22 pinMode (pinB,INPUT);
23 pinALast = digitalRead(pinA);
24 Serial.begin
25 (9600);
26 Serial.println("BEGIN");
27 Serial.println();
28 }
29
30
31 void loop()
32 {
33 aVal = digitalRead(pinA);
34 if (aVal != pinALast)
35
36 {
37 if (digitalRead(pinB) != aVal)
38 {
39
40 encoderPosCount ++;
41 bCW = true;
42 }
43
44 else
45
46
47 {
48 bCW = false;
49 encoderPosCount--;
50 }
51
52
53 if (bCW)
54 {
55 Serial.println ("Rotate Clockwise");
56
57 }
58
59 else
60
61 {
62 Serial.println("Rotate
63 Counterclockwise");
64 }
65
66 Serial.print("Encoder Count: ");
67
68 Serial.println(encoderPosCount);
69 Serial.println();
70 }
71
72
73 pinALast = aVal;
74 }