{"id":870,"date":"2018-01-12T20:47:54","date_gmt":"2018-01-12T20:47:54","guid":{"rendered":"https:\/\/zhutarduino.wordpress.com\/?page_id=870"},"modified":"2019-10-20T18:12:19","modified_gmt":"2019-10-20T18:12:19","slug":"learning-arduino-for-beginners-ep27-using-rotary-encoders","status":"publish","type":"page","link":"https:\/\/thezhut.com\/?page_id=870","title":{"rendered":"Learning Arduino for beginners EP#27 using rotary encoders"},"content":{"rendered":"<h2>In this episode we will take a look at how you can use rotary encoders with your Arduino board.<\/h2>\n<p>affiliate links<\/p>\n<h1><a href=\"https:\/\/www.amazon.com\/gp\/search\/ref=as_li_qf_sp_sr_il_tl?ie=UTF8&amp;tag=zhut01-20&amp;keywords=Rotary Encoders&amp;index=aps&amp;camp=1789&amp;creative=9325&amp;linkCode=xm2&amp;linkId=6793709c351acb3ef12d51842b89f59a\" target=\"_blank\" rel=\"noopener noreferrer\">Rotary Encoders<\/a><\/h1>\n<p><iframe loading=\"lazy\" title=\"Learning Arduino for beginners EP#27 using rotary encoders\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/cZzvVVw-IO0?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><strong><a href=\"https:\/\/www.facebook.com\/zarduino\/\">Check us out on Facebook!<\/a><\/strong><\/h1>\n<h2>Schematic:<\/h2>\n<p><a href=\"https:\/\/thezhut.com\/wp-content\/uploads\/2018\/01\/encoder.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-871\" src=\"https:\/\/thezhut.com\/wp-content\/uploads\/2018\/01\/encoder.jpg?w=258\" alt=\"\" width=\"642\" height=\"749\" srcset=\"https:\/\/thezhut.com\/wp-content\/uploads\/2018\/01\/encoder.jpg 819w, https:\/\/thezhut.com\/wp-content\/uploads\/2018\/01\/encoder-258x300.jpg 258w, https:\/\/thezhut.com\/wp-content\/uploads\/2018\/01\/encoder-768x895.jpg 768w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/a><\/p>\n<h2>ARDUINO Code:<\/h2>\n<p>#define led 9<br \/>\n#define encoderPinA 2 \/\/ right<br \/>\n#define encoderPinB 3 \/\/ left<br \/>\n#define clearButton 8 \/\/ switch<br \/>\nint encoderPos = 0; \/\/ a counter for the dial<br \/>\nunsigned int lastReportedPos = 1; \/\/ change management<br \/>\nstatic boolean rotating = false; \/\/ debounce management<br \/>\nboolean A_set = false;<br \/>\nboolean B_set = false;<\/p>\n<p>void setup() {<\/p>\n<p>pinMode(encoderPinA, INPUT_PULLUP); \/\/enabling pullups<br \/>\npinMode(encoderPinB, INPUT_PULLUP);<br \/>\npinMode(clearButton, INPUT_PULLUP);<br \/>\npinMode(led,OUTPUT);<br \/>\nattachInterrupt(0, doEncoderA, CHANGE); \/\/pin 2<br \/>\nattachInterrupt(1, doEncoderB, CHANGE); \/\/pin 3<\/p>\n<p>Serial.begin(9600); \/\/ output<\/p>\n<p>}<\/p>\n<p>void loop() {<br \/>\nrotating = true; \/\/ reset the debouncer<\/p>\n<p>if (lastReportedPos != encoderPos) {<br \/>\nSerial.println(encoderPos);<br \/>\nlastReportedPos = encoderPos;<br \/>\n}<br \/>\nif (encoderPos &gt; 255)<br \/>\n{<br \/>\nencoderPos = 255;<br \/>\n}<br \/>\nif (encoderPos &lt; 0)<br \/>\n{<br \/>\nencoderPos = 0;<br \/>\n}<\/p>\n<p>if (digitalRead(clearButton) == LOW ) {<br \/>\nencoderPos = 0;<br \/>\n}<br \/>\nanalogWrite(led,encoderPos);<br \/>\n}<\/p>\n<p>\/\/ Interrupt on A changing state<br \/>\nvoid doEncoderA() {<br \/>\n\/\/ debounce<br \/>\nif ( rotating ) delay (1); \/\/ wait a little until the bouncing is done<\/p>\n<p>\/\/ Test transition, did things really change?<br \/>\nif ( digitalRead(encoderPinA) != A_set ) { \/\/ debounce once more<br \/>\nA_set = !A_set;<\/p>\n<p>\/\/ adjust counter + if A leads B<br \/>\nif ( A_set &amp;&amp; !B_set )<br \/>\nencoderPos += 10;<\/p>\n<p>rotating = false; \/\/ no more debouncing until loop() hits again<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/ Interrupt on B changing state<br \/>\nvoid doEncoderB() {<br \/>\nif ( rotating ) delay (1);<br \/>\nif ( digitalRead(encoderPinB) != B_set ) {<br \/>\nB_set = !B_set;<br \/>\n\/\/ adjust counter &#8211; 1 if B leads A<br \/>\nif ( B_set &amp;&amp; !A_set )<br \/>\nencoderPos -= 10;<\/p>\n<p>rotating = false;<br \/>\n}<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this episode we will take a look at how you can use rotary encoders with your Arduino board. affiliate links Rotary Encoders Check us out on Facebook! Schematic: ARDUINO Code: #define led 9 #define encoderPinA 2 \/\/ right #define &hellip; <a href=\"https:\/\/thezhut.com\/?page_id=870\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":809,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":"","_links_to":"","_links_to_target":""},"class_list":["post-870","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thezhut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=870"}],"version-history":[{"count":2,"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/870\/revisions"}],"predecessor-version":[{"id":1471,"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/870\/revisions\/1471"}],"up":[{"embeddable":true,"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/809"}],"wp:attachment":[{"href":"https:\/\/thezhut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}