Some help needed on the logic of encoder direction detection

Hello …

at first :: this is amazing :: great project :: well done :: great potential :: !! ::

i need some help ::
want to build a midi controller , have an encoder ky-040 board , want to detect the direction of the rotation and send when specific CC messages …

i have done so far :: midi is going well thru over USB port, edited some of the code in controlchange lib by awgrover ( you forgot to name the input_CC/input_Val/input_Ch correctly so the compiler gone wild at line 37-39 - inside the node) , but as for nice soft i could do it by myself :slight_smile:

i dont need to count , i just need to send a CCxx when clockwise or CCyy when counterclockwise

the problem i have to put in nodes this logic ::

Hello!

That should be pretty straightforward.


struct State {
    // We need store the last state permanently, so put it in State
    bool lastState;
};

{{ GENERATED_CODE }}

void evaluate(Context ctx) {
    auto state = getState(ctx);

    // Read port numbers from XOD inputs labeled PA and PB (you should create them)
    auto portA = (uint8_t)getValue<input_PA>(ctx);
    auto portB = (uint8_t)getValue<input_PB>(ctx);

    auto currentState = digitalRead(portA);
    if (currentState != state->lastState) {
        if (digitalRead(portB) != state)
            emitValue<output_CW>(ctx, 1); // clockwise pulse on XOD pin CW
        else
            emitValue<output_CCW>(ctx, 1); // counterclockwise pulse on XOD pin CCW
    }

    state->lastState = currentState;
}

I wrote this blindly, without any checks, but I hope you get the idea on how to transofrm the code.

thx for the effort … but how it would like look in nodes ?? i dont wanna touch any code , this is what the xod project is about …

thx .!!

To make it work you have to create a C++ node. That’s easy since you already have the code :slight_smile:

All you need to do is:

  1. Prepare a dummy
  2. Add the not-implemented-in-xod node and copy/paste the code

See Creating Nodes for XOD in C++ for details.

@microsoundfield

And don’t forget to publish (File -> Publish Library) the nodes! :wink:
Thanks in advance!

1 Like

i have some problems with the code …

1 the compiler makes input_input_xy so all the names has to be changed in arduino ide ( no problem )
2 if i get it compiled to a state : no undeclared vars any more i get a warning about compare pointer to number problem:: if (currentState != state->lastState) {

but it let upload , but not working … any input from encoder is not working…

im not the code type any more … it look to me like the lastState var is not set once in the code ?? only defined.

i use boolean input nodes …

Could you send a xodball (export the project) of what you have now so that I can take a look?

i cant attach the file as new user … here some pics::

13![21|157x267]

21

27

hey … did you have a look ?? is this topic dead ??

Hello! It’s very hard to complete a node for hardware you don’t have. I would be able to try implement encoder support as soon as I’ll get one. Not sure when it’d happen. So, if anyone has similar one already, it would be great if you’d take a look.

Hi guys,

fisrt of all I have to say that XOD is wonderfull and I am very thankfull for such an easy to use tool.

microsoundfiled: I have rocegnized, that nkrkv has prepared the C++ code for numeral inputs - numbers of pins, so the issue is with the usage of boolean input nodes. However I was not able to make the script functional. So I have prepared litle bit modificated script for countig encoder pulses. This works quite well. Now its up to you to decide which direction is the schaft moving.

struct State {
bool lastState;
};

{{ GENERATED_CODE }}

void evaluate(Context ctx) {
auto state = getState(ctx);

auto CLK = (bool)getValue<input_CLK>(ctx);  // boolean digital inputs
auto DT = (bool)getValue<input_DT>(ctx);
Number count = getValue<output_OUT>(ctx);   // count declaration

auto currentState = CLK; // actual state store
if ((state->lastState== false) && (currentState == true)) {
    if (DT == false)
        count+=1; // clockwise increment
    else
        count-=1; // counterclockwise decrement
}
state->lastState = currentState;
emitValue<output_OUT>(ctx,count);

}

obrazek

Hope this helps.

STP

Hi again,

There were some issues with the patch I have modified. If you are interested in encoder patch I have prepared, you can download the library.

https://xod.io/libs/escape-ford/encoder-ky040/

Please be considerate, I have no C++ experience.

Thanks to nkrkv for providing the original code!
image

ok!!! THX !! where i can download it ?? github>>NO/ google>>NO … on the xod lib side is an endless loop of links …

///////// ok ok ////// i found it in the add lib searchbox but :: it shows not in the side pannel ?? any ideas ??

acctually it seem to load on add lib, but on the hdd : there is no lib added …

Well, it seems to me, that I have published the library in wrong way. Please be patient, I am a newbie to this. I will try to add next version to the library corectly asap . I will let you know.

Please try to add the library once more. It should be ok.

STP

One year later and I’ve found your library to be very helpful. It works perfectly when debugging (counts increase/decrease as expected, no bouncing; CW/CCW shift true/false accurately; button push = count reset).

However, when I attach an LCD (16x2 i2c) in place of the watch nodes, the output to the screen is not accurate at all (count or direction), button pushes are fine and reset the count to zero.

Would you have any insight on why there would be a difference and how to correct for it?

Thanks!

If LCD display is sometimes correct, that implies it is wired up correctly. Is it just not updating all the time so it lags behind? I can’t think of any other reason it would be working differently than watch nodes…

Can you share your program? Maybe that would reveal something.