My favorites | Sign in
Project Home Downloads Issues Source
Checkout   Browse   Changes    
 
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
/**
* A simple incremental Rotary Encoder decoder example
* andrew@rocketnumbernine.com
* http://www.rocketnumbernine.com/2010/03/06/decoding-a-rotary-encoder/
* use freely
*/
#include <util/delay.h>

volatile uint8_t pinValues[2] = {0,0};
volatile int position = 0

void setup()
{
Serial.begin(19200);
pinMode(2, INPUT); // 2 Encoder pins as inputs
pinMode(3, INPUT);
// enable interrupts on those two pins:
PCICR |= (1 << PCIE2);
PCMSK2 |= (1 << PCINT18) | (1 << PCINT19);
sei();
}

ISR(PCINT2_vect)
{
_delay_ms(1);
int pin0 = digitalRead(2);
int pin1 = digitalRead(3);
if (pin0 != pinValues[0]) {
rotary_encoder_change(0, pin0);
} else if (pin1 != pinValues[1]) {
rotary_encoder_change(1, pin1);
}
}

void rotary_encoder_change(uint8_t changedPin, uint8_t value)
{
pinValues[changedPin] = value;
position += ((pinValues[0] == pinValues[1]) ^ changedPin) ? 1 : -1;
}

void loop()
{
Serial.println(position);
delay(100);
}





Change log

r4 by and...@rocketnumbernine.com on Mar 6, 2010   Diff
fixed url in comment
Go to: 
Project members, sign in to write a code review

Older revisions

r3 by and...@rocketnumbernine.com on Mar 6, 2010   Diff
first version
All revisions of this file

File info

Size: 977 bytes, 50 lines
Powered by Google Project Hosting