{"id":17962,"date":"2022-08-08T16:12:14","date_gmt":"2022-08-08T09:12:14","guid":{"rendered":"https:\/\/www.makeriot2020.com\/?p=17962"},"modified":"2022-08-08T16:12:20","modified_gmt":"2022-08-08T09:12:20","slug":"using-a-rotary-encoder","status":"publish","type":"post","link":"https:\/\/www.makeriot2020.com\/index.php\/2022\/08\/08\/using-a-rotary-encoder\/","title":{"rendered":"Using a Rotary Encoder"},"content":{"rendered":"\n<p>As part of an ongoing project, I recently designed an expander card for my ESP-12E I2C Base. I am referring to this device( <a href=\"http:\/\/makeriot2020.com\/index.php\/2022\/07\/27\/atmega-328p-based-pwm-controller-card\/\" target=\"_blank\" rel=\"noopener\">Atmega 328P Base PWM Controller Card<\/a>). At the time of writing that article, I have not released any of the code for the project. This is a very short post, showing one possible way to implement a rotary encoder onto that particular device. (It can also be adapted for other devices, of course)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Arduino Style Code for using a rotary encoder<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Constants and Variables\nconst int encFWD = 8;\nconst int encREV = 7;\nint aState;\nint aLastState;\nint encDir;\nint encTurned = LOW;\nint encLastState;\nint encValue = 0;\nint lastEncValue;\nconst int encInc = 10;\n\nunsigned long lastEncDebounce = 0;\nunsigned encDebounceDelay = 50;\nconst int encBtn = 9;\nint encButtonState;\nint lastEncBtnState = LOW;\nint EncBtnValue = LOW;\nint encBtnState;\n\nvoid setup() {\n  \/\/Rotary Encoder\n  pinMode(encFWD,INPUT_PULLUP);\n  pinMode(encREV,INPUT_PULLUP);\n  pinMode(encBtn,INPUT_PULLUP);\n  \/\/ Init the pins in UNPUT Pullup Mode\n  encTurned = LOW; \/\/ Flag for encoder\n\n  encLastState = digitalRead(encFWD);\n  \/\/Serial\n  Serial.begin(115200);\n  \/\/Status LED on D13\n  pinMode(13,OUTPUT);\n  digitalWrite(13,LOW);\n}\n\nvoid loop() {\n  lastEncValue = encValue;\n \/\/Handle the Encoder Push Button\n encBtnState = digitalRead(encBtn);\n if (encBtnState != lastEncBtnState) {\n    lastEncDebounce = millis();\n }\n if ((millis() - lastEncDebounce) > encDebounceDelay) {\n    if (encBtnState != encButtonState) {\n        encButtonState = encBtnState;\n        if (lastEncBtnState == LOW) {\n          EncBtnValue = !EncBtnValue; \/\/ Toggle the button Value\n        }\n    }\n }\n lastEncBtnState = encBtnState;\n \/\/ Handle the Rotary Encoder Dial\n aState = digitalRead(encFWD);\n if (aState != aLastState) {\n    if (digitalRead(encREV) != aState) {\n       if (encTurned == LOW) {\n          encLastState = encTurned;\n          encTurned = HIGH; \/\/ Set Flag\n\/\/ Setting this flag will get rid of double value entries caused by contact\n\/\/ bounce inside the encoder. I found it easier to implement this way\n\/\/ as opposed to using software debouncing as with the button\n\n       } else {\n          encTurned = LOW; \/\/ Set Flag low\n\/\/ This will ensure that the value is increased only once per \"click\"\n       }\n       if ((encValue &lt; 300) &amp;&amp; (encDir == 0)){\n          if ((encLastState == LOW) &amp;&amp; (encTurned == HIGH)){\n            encValue = encValue + encInc;\n            encDir = 1;\n          }\n       }\n      \n    } else {\n      if (encTurned == LOW) {\n        encLastState = encTurned;\n        encTurned = HIGH;  \n      } else {\n        encTurned = LOW;\n      }\n      if ((encValue > 0) &amp;&amp; (encDir == 0)){\n          if ((encLastState == LOW) &amp;&amp; (encTurned == HIGH)){\n            encValue = encValue - encInc;\n            encDir = 2;\n          }\n      }\n    }\n    encLastState = encTurned;\n}\naLastState = aState;\nencDir = 0;\n\/\/ Print Some Status\nif (encValue != lastEncValue) {\n  Serial.print(\"Encoder Value Changed from \");\n  Serial.print(lastEncValue);\n  Serial.print(\" to \");\n  Serial.println(encValue);\n}\ndigitalWrite(13,EncBtnValue);\n\n\n\n}<\/code><\/pre>\n\n\n\n<p>I hope that this will be useful to somebody. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>As part of an ongoing project, I recently designed an expander card for my ESP-12E I2C Base. I am referring to this device( Atmega 328P Base PWM Controller Card). At the time of writing that article, I have not released any of the code for the project. This is a very short post, showing one &hellip; <a href=\"https:\/\/www.makeriot2020.com\/index.php\/2022\/08\/08\/using-a-rotary-encoder\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using a Rotary Encoder&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":17859,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[6,42,81],"class_list":["post-17962","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-arduino","tag-esp32","tag-esp8266"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/posts\/17962","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/comments?post=17962"}],"version-history":[{"count":0,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/posts\/17962\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/media\/17859"}],"wp:attachment":[{"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/media?parent=17962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/categories?post=17962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/tags?post=17962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}