1 | /* |
---|
2 | * Rotary-Encoder Sketch mit Interrupt Nr. 2 |
---|
3 | * beachte: Bei schnellen Vorgaengen verlangsamt der Serielle-Monitor den Sketch! |
---|
4 | * Wenn der Button (Switch) zum Rueckstellen ins Programm eingebaut wird, muss |
---|
5 | * man den INPUT mit PULLUP versehen! |
---|
6 | * |
---|
7 | * read a rotary encoder with interrupts |
---|
8 | Encoder hooked up with common to GROUND, |
---|
9 | encoder0PinA to pin 2, encoder0PinB to pin 3 |
---|
10 | it doesn't matter which encoder pin you use for A or B |
---|
11 | |
---|
12 | uses Arduino pull-ups on A & B channel outputs |
---|
13 | turning on the pull-ups saves having to hook up resistors |
---|
14 | to the A & B channel outputs |
---|
15 | */ |
---|
16 | |
---|
17 | #define encoder0PinA 2 |
---|
18 | #define encoder0PinB 3 |
---|
19 | |
---|
20 | volatile int encoder0Pos = 0; |
---|
21 | |
---|
22 | void setup() { |
---|
23 | |
---|
24 | pinMode(encoder0PinA, INPUT); |
---|
25 | pinMode(encoder0PinB, INPUT); |
---|
26 | |
---|
27 | attachInterrupt(0, doEncoder, CHANGE); // encoder pin on interrupt 0 - pin 2 |
---|
28 | Serial.begin (9600); |
---|
29 | Serial.println("Start des Programms"); |
---|
30 | } |
---|
31 | |
---|
32 | void loop(){ |
---|
33 | |
---|
34 | } |
---|
35 | } |
---|
36 | void doEncoder() { |
---|
37 | /* If pinA and pinB are both high or both low, it is spinning |
---|
38 | * forward. If they're different, it's going backward. |
---|
39 | */ |
---|
40 | if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) { |
---|
41 | encoder0Pos++; |
---|
42 | } else { |
---|
43 | encoder0Pos--; |
---|
44 | } |
---|
45 | Serial.print(" "); |
---|
46 | Serial.println (encoder0Pos); |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | |
---|