Page 1 of 1

Simple routine for a rotary encoder

Posted: Thu Dec 14, 2017 3:24 pm
by Grahamm
Hi
I am trying to experiment with switching a few (say 3 or 4) outputs or LEDs sequentially using a rotary encoder, and although this seems simple, I have not been able to get this to function properly. It may be that the encoder I am using is no good, coupled more likely with my not understanding exactly what to do with respect to reading/writing encoder, checking for changes etc., and the correct sequence for using these component macros. Essentially I want something where if one rotates the encoder clockwise, the first output should switch on. Then say one or two clockwise clicks later, it should switch off and the next output should switch on. This would eventually cycle back to selecting/switching the first output. This process would go in reverse if rotating the encoder counter-clockwise, i.e. outputs would switch in descending order, 4,3,2,1, then back to 4.
I have tried a few different PICs at different frequencies, but I don't think my setup is at fault.
If anyone can give me a few pointers or guidance or sample code to get me going in the right direction, I shall be most grateful.
Cheers
Graham

Re: Simple routine for a rotary encoder

Posted: Fri Dec 15, 2017 9:45 am
by fotios
Hi Graham
The attached fcf simulates the operation of a detent equiped encoder. Is this: http://uk.farnell.com/alps/ec11e18244a5 ... m%2Fsearch
This type is more complex compared to non-detent. In each detent position, Switch A is either HI or LO, while Switch B is ready to change state. Play with switch A to see the result. Change state to switch B, and play again with switch A.

Re: Simple routine for a rotary encoder

Posted: Fri Dec 15, 2017 9:57 am
by LeighM
Hi Graham,
You need to call CheckForChanges() regularly, either in your main loop, or on an interrupt (e.g. every 10mS)
At the start of your program call ResetCounter()
Then in your main loop read the value from ReadCounter() and check if it has gone positive or negative, that will tell you the direction of rotation.
If it has, you can reset it again with ResetCounter() and take the first action with your LEDs.
You could check that actual or absolute value of ReadCounter() if you want to ensure a few clicks, for example

Code: Select all

value =  ReadCounter()
if (value > 2)
  // at least three clicks clockwise
if (value < -2)
  // at least three clicks anti-clockwise
You might find that your encoder gives more than 1 count per click, so just increase the test values.
Hope that helps
Leigh

ps. fotios and I posted simultaneously, my comments relate to the Flowcode rotary encoder component, whereas fotios has a solution that does not require it

Re: Simple routine for a rotary encoder

Posted: Fri Dec 15, 2017 4:29 pm
by Grahamm
Thanks a lot Fotios and Leigh for your pointers and advice. I shall follow your suggestions and see how this pans out. I really appreciate your help.
I shall let you know once I have this working satisfactorily.
Regards
Graham

Re: Simple routine for a rotary encoder

Posted: Tue Dec 19, 2017 2:55 pm
by Grahamm
Hi there
To follow up, I have got this working on a breadboard using Fotios' structure as a basis. I used different ports and chip though and a smaller number of outputs. I now want to try different numbers of clicks to change outputs. I am still having problems using the Flowcode rotary encoder component as per Leigh and have not got it working yet. I shall persevere as essentially it should in theory be easier to use and give a simpler or more easily readable chart structure. I suspect I may be confusing my variables and losing my way.
Cheers
Graham

Re: Simple routine for a rotary encoder

Posted: Tue Dec 19, 2017 5:44 pm
by medelec35
Hi Graham,
Have you taken a look at this Wiki page?
The one to check out is

Code: Select all

Interrupt On Change 
Martin

Re: Simple routine for a rotary encoder

Posted: Thu Dec 21, 2017 9:21 am
by Grahamm
Thanks Martin, I'll check this out. I am sure I will eventually get this working properly.
Cheers
Graham