- [Show page]
- [Old revisions]
- [[unknown link type]]
- []
Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
hardware:arduino [2015/03/04 17:35] admin |
hardware:arduino [2015/03/04 18:07] (current) admin [Rotary encoders] |
||
|---|---|---|---|
| Line 19: | Line 19: | ||
| * direct attach to two (2) interrupt pins, keyword: passive waiting | * direct attach to two (2) interrupt pins, keyword: passive waiting | ||
| * using external IO with counter, keyword: any time baby, any time :) | * using external IO with counter, keyword: any time baby, any time :) | ||
| + | |||
| + | Huge note: dont forget 10k pull up resistors. | ||
| ad 1) | ad 1) | ||
| - | The easiest and most straight forward method. The idea is: we are actively checking (sample rate) 200x per second (200Hz -> 5ms, T = 1/f) falling edge of pinA | + | The easiest and most straight forward method. The idea is: we are actively detecting falling edge of pinA |
| {{:hardware:rotary_encoder_phase.jpg|}} | {{:hardware:rotary_encoder_phase.jpg|}} | ||
| Line 28: | Line 30: | ||
| <code> | <code> | ||
| - | if ((currentTime - loopTime) > 5)){ | + | |
| encoder_A = digitalRead(pin_A); // Read encoder pins | encoder_A = digitalRead(pin_A); // Read encoder pins | ||
| encoder_B = digitalRead(pin_B); | encoder_B = digitalRead(pin_B); | ||
| Line 44: | Line 46: | ||
| </code> | </code> | ||
| - | ad 2) | + | An example with active checking but only 200x per second. |
| + | Sample rate 200x per second (200Hz -> 5ms, T = 1/f). Useful only if you know exactly which rotary encoder you have! Some starts with 24 pulses per revolution, some have 1024. | ||
| + | |||
| + | <code> | ||
| + | #define TIMESLOT 5 | ||
| + | currentTime = millis(); | ||
| + | if ((currentTime - loopTime) > TIMESLOT)){ | ||
| + | [....some code....] | ||
| + | lastTime=currentTime; | ||
| + | } | ||
| + | </code> | ||
| + | So you dont have to read it like an idiot 1000000x times per second. | ||
| + | |||
| + | ad 2) Using interrupts to read a rotary encoder is a perfect job for interrupts because the interrupt service routine (a function) can be short and quick, because it doesn't need to do much. | ||
| + | |||
| + | <code> | ||
| + | #define encoderPinA 2 | ||
| + | #define encoderPinB 4 | ||
| + | |||
| + | volatile int encoderPos = 0; | ||
| + | |||
| + | void setup() { | ||
| + | attachInterrupt(0, MyInterrupt, CHANGE); | ||
| + | } | ||
| + | |||
| + | MyInterrupt() { | ||
| + | if (digitalRead(encoderPinA) == digitalRead(encoderPinB)) { | ||
| + | encoderPos++; | ||
| + | } else { | ||
| + | encoderPos--; | ||
| + | } | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | You can print the value **outside** not inside the MyIntterupt() routine, which should be as fast&small as possible. | ||
| + | <code> | ||
| + | void loop() { | ||
| + | Serial.println (encoderPos, DEC); | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | the word //volatile// is **very** important :-). | ||
hardware/arduino.1425486914.txt.gz · Last modified: 2015/03/04 17:35 by admin


