Here is a quick write up on how to use the tymkrs “Turn Me” rotary encoder. This supports the “push down” feature of the tymkrs kit.
Fritzing Rotary Encoder Part: Rotary Encoder with Knob bth.fzpz
Fritzing Project: RotaryEncoderDemo.fzz
Arduino (1.5) project: RotaryEncoderDemo.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
64
65
|
/* Read Quadrature Encoder * Connect Encoder to Pins encoder0PinA, encoder0PinB, and +5V. * Sketch by max wolf / www.meso.net * v. 0.1 - very basic functions - mw 20061220 * Sketch updated by Brooke Hedrick / www.millamilla.com * v. 0.2 - Added the "S" pin for encoders with push-down support - bth 09292013 * - Put a limit of 255 and 0 in place. It rolls over automatically at the limits - bth 09292013 * */ int val; int encoder0PinA = 3; int encoder0PinB = 4; int encoder0PinS = 5; int encoder0PosUp = 0; int encoder0PosDown = 0; int encoder0PinALast = LOW ; int n = LOW ; int encoder0UpDown = LOW ; void setup () { pinMode (encoder0PinA, INPUT ); pinMode (encoder0PinB, INPUT ); pinMode (encoder0PinS, INPUT ); Serial.begin (9600); } void loop () { encoder0UpDown = digitalRead (encoder0PinS); n = digitalRead (encoder0PinA); if ((encoder0PinALast == LOW ) && (n == HIGH )) { //Serial.print("up down:"); //Serial.println(encoder0UpDown); if ( digitalRead (encoder0PinB) == LOW ) { if (encoder0UpDown == LOW ) { encoder0PosUp--; if (encoder0PosUp < 0) { encoder0PosUp = 255; } } else { encoder0PosDown--; if (encoder0PosDown < 0) { encoder0PosDown = 255; } } } else { if (encoder0UpDown == LOW ) { if (encoder0PosUp > 255) { encoder0PosUp = 0; } } else { encoder0PosDown++; if (encoder0PosDown > 255) { encoder0PosDown = 0; } } } Serial.print(encoder0PosUp); Serial.print( " " ); Serial.println(encoder0PosDown); } encoder0PinALast = n; } |
For more detail: Using the tymkrs “Turn Me” with an Arduino