Simple routine for a rotary encoder

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 7.

Moderator: Benj

Post Reply
Grahamm
Flowcode V4 User
Posts: 10
Joined: Thu Sep 08, 2011 11:53 am
Been thanked: 1 time

Simple routine for a rotary encoder

Post 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

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times

Re: Simple routine for a rotary encoder

Post 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.
Attachments
Grahamm.fcf
(31.79 KiB) Downloaded 298 times
Best Regards FOTIS ANAGNOSTOU

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times

Re: Simple routine for a rotary encoder

Post 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

Grahamm
Flowcode V4 User
Posts: 10
Joined: Thu Sep 08, 2011 11:53 am
Been thanked: 1 time

Re: Simple routine for a rotary encoder

Post 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

Grahamm
Flowcode V4 User
Posts: 10
Joined: Thu Sep 08, 2011 11:53 am
Been thanked: 1 time

Re: Simple routine for a rotary encoder

Post 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

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Simple routine for a rotary encoder

Post 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
Martin

Grahamm
Flowcode V4 User
Posts: 10
Joined: Thu Sep 08, 2011 11:53 am
Been thanked: 1 time

Re: Simple routine for a rotary encoder

Post by Grahamm »

Thanks Martin, I'll check this out. I am sure I will eventually get this working properly.
Cheers
Graham

Post Reply