Author Topic: Sample Arduino Code for RGB Rotary Encoder  (Read 3751 times)

0 Members and 1 Guest are viewing this topic.

Offline lyndonasia

  • Contributor
  • Posts: 15
  • Country: hk
  • I am a Canadian expat living in Hong Kong.
Sample Arduino Code for RGB Rotary Encoder
« on: November 23, 2014, 03:18:07 pm »
Hi Everyone,

I just bought one of those SparkFun RGB encoders.  I have never used one before and I wanted to learn more about them.  There were some examples of Arduino code on the the SparkFun product page but I couldn't get any of the examples to work properly.  After quite a bit of searching I managed to find some code which I then heavily modified.  I thought this might be useful if you are planning on getting one of these encoders.



The sample code is attached below.  (Just rename it to "sparkfun_rgb.ino".)

Cheers, Lyndon
« Last Edit: November 23, 2014, 03:22:06 pm by lyndonasia »
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8229
  • Country: 00
Re: Sample Arduino Code for RGB Rotary Encoder
« Reply #1 on: November 24, 2014, 04:46:52 am »
You may find it helpful if you create a .h-.c file so that your code can be more easily incorporated into future projects.

As to your code itself, it uses lots of delays and looping, not the most efficient way to handle encoder reads. The easiest is to use a state machine for this.
================================
https://dannyelectronics.wordpress.com/
 

Offline FreddyVictor

  • Regular Contributor
  • *
  • Posts: 164
  • Country: gb
Re: Sample Arduino Code for RGB Rotary Encoder
« Reply #2 on: November 24, 2014, 09:05:15 pm »
when you have buttons/user input devices, it's best to use interrupts - atmega's can have pin-change interrupts on most pins

no doubt there's probably an arduino library all ready & waiting for you to use (but really this is the sort of stuff you should be learing to do yourself!)
 

Offline lyndonasia

  • Contributor
  • Posts: 15
  • Country: hk
  • I am a Canadian expat living in Hong Kong.
Re: Sample Arduino Code for RGB Rotary Encoder
« Reply #3 on: November 25, 2014, 05:40:00 pm »
Thanks for the comments.  Most of the examples I found for encoders did indeed use interrupts however, none of the samples I found worked at all for this particular encoder.  If I can find an example that works I will post it here.

I think the while { } loops are probably not such a good idea.  When I get some free time, I think I will revise this version to use an interrupt.  Shouldn't be too hard... and also a good exercise as I am really only getting started in programming micros.

Cheers, Lyndon
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf