arduino
arduino copied to clipboard
The full step is detected differently than described
First of all - using a state machine for this task is an elegant solution I must say. I tested the code and it looks like it detects full step when both pins are high contrary to written that the step is detected when both pins go low:
CCW: CW:
10 01
00 00
01 10
11 <- this state returns DIR_CCW 11 <- this state returns DIR_CW
Maybe I`m doing something wrong or I have different kind of rotary encoder but for me the sequence is like this:
CCW: CW:
10 01
11 11
01 10
00 00
Here is the state table that works for me:
const unsigned char ttable[7][4] = {
// R_START
{R_START, R_CW_BEGIN, R_CCW_BEGIN, R_START},
// R_CW_FINAL
{R_START | DIR_CW, R_START, R_CW_FINAL, R_CW_NEXT},
// R_CW_BEGIN
{R_START, R_CW_BEGIN, R_START, R_CW_NEXT},
// R_CW_NEXT
{R_START, R_CW_BEGIN, R_CW_FINAL, R_CW_NEXT},
// R_CCW_BEGIN
{R_START, R_START, R_CCW_BEGIN, R_CCW_NEXT},
// R_CCW_FINAL
{R_START | DIR_CCW, R_CCW_FINAL, R_START, R_CCW_NEXT},
// R_CCW_NEXT
{R_START, R_CCW_FINAL, R_CCW_BEGIN, R_CCW_NEXT},
};
Maybe this helps someone else...
@iamanonymus What kind of encoder were you using?
Sorry for late response :) I think I used this one: https://www.ebay.com/itm/1PCS-KY-040-Rotary-Encoder-Module-Brick-Sensor-Development-For-Arduino/381375070024?hash=item58cbb9bf48:g:6aIAAOSwq5lTmbF7
@iamanonymus Interesting. I use the same encoder, and I don't have the problem you described. Maybe there are different versions of this encoder being sold under the same name.
It could just be that you have the two output pins swapped?
This is the sequence defined in Your code state table for full step CCW direction (just go through on paper an check): 00 - R_START 10 - R_CCW_BEGIN 00 - R_CCW_NEXT 01 - R_CCW_FINAL 11 - R_START | DIR_CCW
This is stated in the documentation (Rotary.cpp/README.md)
Step1 0 0
1/4 1 0
1/2 1 1
3/4 0 1
Step2 0 0
- My setup works as described in the documentation;
- This could probably be because the ground and VCC is swapped, swapping signal wires would just make it go the other way around;
For me the problem was that steps were registered mid-turn.
Either way I think You should fix at least documentation, because it does not match code..