Hi, Thanks for sharing. It didn't work for me.

How is your encoder wired up? Make sure there are pullup resistors on each of P8_07 and P8_08.

If you want to visualise the signals from encoders you could run the scope_analog example and temporarily hook up the encoder pins to the analog inputs (or adjust the example to display digital inputs instead). With pullups, you should see alternating patterns of high and low when you turn the encoder.

Thanks for the help. Of course the pull-up resistors missing!! The example works. It needs to add the line:
#include <Bela.h>
and the cleanup function. Now I will try to send the values via osc.

Great! I edited the example above to fix those issues.

2 years later

It is safe to use an external pull-up, no risk of short. You want the external pull-xx to be significantly smaller than the internal one. I am not sure what the value is for the internal one, but we normally use pull-ups in the range of 10k or less, maybe go for 2k2 to be on the safe side.

20 days later

is there any obvious reason why I can get this to work with pins 9_12 / 9_14 but NOT with pins 8_08 / 8_07 ?

    2 months later

    soeren is there any obvious reason why I can get this to work with pins 9_12 / 9_14 but NOT with pins 8_08 / 8_07 ?

    sorry for the late answer. I have no idea, it should work the same on any input

    phevosSo Is it possible to use @bela_robert 's C++ example for using rotary encoders in pure data?

    Yes, here is an overview on how to integrate C++ and Pd: https://github.com/BelaPlatform/Bela/wiki/Puredata-and-C--

    You can probably use Robert's code and add one line to send a message to Pd every time the encoder value changes:

    libpd_float(receiverName, encoder0Pos);

    where receiverName is the name of a receiver you have in PureData to which you want to send a message.
    Something like this:

    char receiverName[] = "encoder";
    ...
    void render(...)
    {
    ...
    	for(unsigned int n=0; n<context->digitalFrames; n++){
    	    if ((encoder0PinALast == LOW) && (status == HIGH)) {
    	      	if (digitalRead(context, n, encoder0PinB) == LOW) {
    	        	encoder0Pos--;
    	      	} else {
    	        	encoder0Pos++;
    	      	}
                    libpd_float(receiverName, encoder0Pos); // send to Pd
    	    }
    ...
        }
    }
    ...

    and in Pd you would have an object [receive encoder].

    phevosSo And if i do so, will the schematic shown in this link work ?

    I cannot see the link just now, but the schematics there were ok.

    Hello everyone,
    I hooked up a rotary encoder as shown in here
    http://farhek.com/jd/c1f1971/studio-rotary-encoders/31y79t/
    Then I compiled @bela_robert code ,but nothing was printed out. I temporally switched from digital to analog pins so I can use the scope example to visualise the incoming signal , it responded to push and rotation as expected, so i guess there is nothing wrong with the hardware . Am I missing something with the code? Shouuld i modify it?

    P.S. I used the pins P8_08 and P8_07 for the rotary.

    what do you mean nothing "was printed out"? The code should print something regardless of whether the encoders are read correctly or not.

    4 years later

    Hi,
    i hope it's okay to resurrect the thread.
    i copied roberts code into the custom cpp and it seems to work fine and the console prints out the proper encoder value
    but if i add the messages to PureData like this:

    libpd_float("encoder",-1); // send to Pd

    it stops working as soon as i touch the encoder
    (i'm sending -1 and 1 to puredata because i'm only looking for the change, not the absolute value in PD)

    //ENCODER STUFF
    		for(unsigned int n=0; n<context->digitalFrames; n++){
    	    status = digitalRead(context, n, encoder0PinA);
    	    if ((encoder0PinALast == LOW) && (status == HIGH)) {
    	      	if (digitalRead(context, n, encoder0PinB) == LOW) {
    	        	encoder0Pos--;
    	        	libpd_float("encoder",-1); // send to Pd
    	      	} else {
    	        	encoder0Pos++;
    	        	libpd_float("encoder",1); // send to Pd
    	      	}
    	      	
    	      
    	    }
    	    encoder0PinALast = status;
    
    	    // Increment a counter on every frame
    	    gCount++;
    		
    	    // Print a message every quarter second indicating the current encoder count
    	    if(gCount % (int)(context->audioSampleRate*gInterval) == 0) {
    		   gSecondsElapsed += gInterval;
    		   rt_printf("Encoder reading: %d\n",encoder0Pos);
    	    }
    	}
    	
    	//ENCODER STUFF

    the console gives me the following messages:

    Encoder reading: 0
    Encoder reading: 0
    Encoder reading: 0
    Encoder reading: 0
    Encoder reading: 0
    Segmentation fault
    Makefile:606: recipe for target 'runide' failed
    make: *** [runide] Error 139
    Bela stopped

    yes of course.

    i took the render.cpp i found here: https://github.com/BelaPlatform/Bela/blob/master/core/default_libpd_render.cpp
    and added the parts from your an roberts posts with slight editing myself. i'm quite new to c++ so i'm taking it slow, and probalby put stuff where it does not belong 😉
    everything added by me is commented with ENCODER STUFF

    i uploaded the file here, because the forum software seems to get upset if i try to post the whole thing(maybe to long?) or attach it.

    https://1drv.ms/u/s!AowD4e_FB7oPhJ1Dtnlz1J5Bzmjemg?e=qin0nD

    and here is a a screenshot of the PD file. its just a simple test thing for the multichannel output:

    alt text

    HA solved it... by pasting the code i inadvertently also pasted the
    return true;

    in the setup routine, and put that right at the front... so i guess i completely skipped the rest of the setup?
    seems to be working now i think...

    well spotted! Btw, for pasting code on the forum, you should be enclosing it within triple backticks (```)

    7 months later

    Hi, I took the rotary encoder example code and put it into the render.cpp example.
    In order to receive the value in PD I have to remove "return true;" from the bool setup section otherwise I get a segmentation fault and Bela stops running.

    My problem is that removing "return true;" triggers the following errors:
    Failed call to wrap_bind: 17 File exists
    Unable to create AuxTaskNonRT WSServer_5432 pipe p_WSServer_5432: (-1) Unknown error -1
    Failed call to
    wrap_bind: 17 File exists
    Unable to create AuxTaskNonRT WSClient_scope_data pipe p_WSClient_scope_data: (-1) Unknown error -1
    Failed call to __wrap_bind: 17 File exists
    Unable to create AuxTaskNonRT WSClient_scope_control pipe p_WSClient_scope_control: (-1) Unknown error -1

    Additionally, I am not able to stop Bela because I get the following errors:

    make: *** [runide] Terminated
    Error closing pipeSocket: 9 Bad file descriptor
    Error closing pipe_fd: 9 Bad file descriptor
    Error closing pipeSocket: 9 Bad file descriptor
    Error closing pipe_fd: 9 Bad file descriptor
    Error closing pipeSocket: 9 Bad file descriptor
    Error closing pipe_fd: 9 Bad file descriptor
    Error while sending to pipe from WSServer_5432: (9) Bad file descriptor (size: 1)
    Error while sending to pipe from WSClient_scope_data: (9) Bad file descriptor (size: 1)
    Error while sending to pipe from WSClient_scope_control: (9) Bad file descriptor (size: 1)

    Does anyone have any ideas why that happens and how to solve it?

    You can access the code with the link below.

    Everything I added is under //Encoder Modification.

    Thank you in advance!

      LEHELTOROK n order to receive the value in PD I have to remove "return true;" from the bool setup section otherwise I get a segmentation fault and Bela stops running.

      yeah not a workaround that was ever going to work really ... there must be some real issue there that you are just somehow hiding.

      You should not return early from setup() as it will skip all the Pd initialisation and subsequently crash. Your issue is that you are trying to instantiate another Scope object, when there is already one used by Pd. You should be getting the inputs into Pd and scope them from there by sending to the [dac~ 27 28 29 30] channels if you need to. Otherwise, if you want to use your own gScope in C++, then you should comment out all the instances of scope in the .cpp file.