{"id":3399,"date":"2017-08-02T09:51:50","date_gmt":"2017-08-02T06:51:50","guid":{"rendered":"http:\/\/roboromania.ro\/?p=3399"},"modified":"2018-01-26T10:39:25","modified_gmt":"2018-01-26T07:39:25","slug":"arduino-rotary-encoder-roboromania-tutorial-pentru-incepatori","status":"publish","type":"post","link":"https:\/\/roboromania.ro\/?p=3399","title":{"rendered":"Arduino Rotary Encoder Roboromania Tutorial pentru \u00eencep\u0103tori"},"content":{"rendered":"<h4><span style=\"color: #0000ff;\">Arduino Rotary Encoder Roboromania Tutorial pentru \u00eencep\u0103tori<\/span><\/h4>\n<p><a href=\"http:\/\/roboromania.ro\/wp-content\/uploads\/2018\/01\/default_comp26.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4200\" src=\"http:\/\/roboromania.ro\/wp-content\/uploads\/2018\/01\/default_comp26.gif\" alt=\"default_comp26\" width=\"38\" height=\"26\" \/><\/a><\/p>\n<p>O schema simpla :<\/p>\n<p>Afi\u0219eaz\u0103 pe serial pozi\u021bia encoderului<\/p>\n<p><a href=\"http:\/\/roboromania.ro\/wp-content\/uploads\/2017\/08\/Rotary-Encoder-Arduino-Tutorial-roboromania-Wiring.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-3400\" src=\"http:\/\/roboromania.ro\/wp-content\/uploads\/2017\/08\/Rotary-Encoder-Arduino-Tutorial-roboromania-Wiring-300x260.png\" alt=\"rotary-encoder-arduino-tutorial-roboromania-wiring\" width=\"300\" height=\"260\" srcset=\"https:\/\/roboromania.ro\/wp-content\/uploads\/2017\/08\/Rotary-Encoder-Arduino-Tutorial-roboromania-Wiring-300x260.png 300w, https:\/\/roboromania.ro\/wp-content\/uploads\/2017\/08\/Rotary-Encoder-Arduino-Tutorial-roboromania-Wiring.png 1000w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a> <a href=\"http:\/\/roboromania.ro\/wp-content\/uploads\/2015\/11\/Rotary-Encoder-Arduino-Tutorial-roboromania.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3398\" src=\"http:\/\/roboromania.ro\/wp-content\/uploads\/2015\/11\/Rotary-Encoder-Arduino-Tutorial-roboromania-216x300.jpg\" alt=\"rotary-encoder-arduino-tutorial-roboromania\" width=\"172\" height=\"239\" srcset=\"https:\/\/roboromania.ro\/wp-content\/uploads\/2015\/11\/Rotary-Encoder-Arduino-Tutorial-roboromania-216x300.jpg 216w, https:\/\/roboromania.ro\/wp-content\/uploads\/2015\/11\/Rotary-Encoder-Arduino-Tutorial-roboromania.jpg 230w\" sizes=\"auto, (max-width: 172px) 100vw, 172px\" \/><\/a><\/p>\n<p>\u0218i un cod simplu :<\/p>\n<p><span style=\"color: #0000ff;\">\/\/ Arduino Rotary Encoder Tutorial<\/span><br \/>\n<span style=\"color: #0000ff;\">int encoderCLK = 2;<\/span><br \/>\n<span style=\"color: #0000ff;\">int encoderDT = 4;<\/span><\/p>\n<p><span style=\"color: #0000ff;\">volatile int lastEncoded = 0;<\/span><br \/>\n<span style=\"color: #0000ff;\">volatile long encoderValue = 0;<\/span><\/p>\n<p><span style=\"color: #0000ff;\">long lastencoderValue = 0;<\/span><\/p>\n<p><span style=\"color: #0000ff;\">int lastMSB = 0;<\/span><br \/>\n<span style=\"color: #0000ff;\">int lastLSB = 0;<\/span><\/p>\n<p><span style=\"color: #0000ff;\">void setup() {<\/span><br \/>\n<span style=\"color: #0000ff;\"> Serial.begin (9600);<\/span><\/p>\n<p><span style=\"color: #0000ff;\">pinMode(encoderCLK, INPUT); <\/span><br \/>\n<span style=\"color: #0000ff;\"> pinMode(encoderDT, INPUT);<\/span><\/p>\n<p><span style=\"color: #0000ff;\">digitalWrite(encoderCLK, HIGH); \/\/turn pullup resistor on<\/span><br \/>\n<span style=\"color: #0000ff;\"> digitalWrite(encoderDT, HIGH); \/\/turn pullup resistor on<\/span><\/p>\n<p><span style=\"color: #0000ff;\">\/\/call updateEncoder() when any high\/low changed seen<\/span><br \/>\n<span style=\"color: #0000ff;\"> \/\/on interrupt 0 (pin 2), or interrupt 1 (pin 3) <\/span><br \/>\n<span style=\"color: #0000ff;\"> attachInterrupt(0, updateEncoder, CHANGE); <\/span><br \/>\n<span style=\"color: #0000ff;\"> attachInterrupt(1, updateEncoder, CHANGE);<\/span><br \/>\n<span style=\"color: #0000ff;\">}<\/span><\/p>\n<p><span style=\"color: #0000ff;\">void loop(){ <\/span><br \/>\n<span style=\"color: #0000ff;\"> Serial.println(encoderValue);<\/span><br \/>\n<span style=\"color: #0000ff;\"> delay(10);<\/span><br \/>\n<span style=\"color: #0000ff;\">}<\/span><\/p>\n<p><span style=\"color: #0000ff;\">void updateEncoder(){<\/span><br \/>\n<span style=\"color: #0000ff;\"> int MSB = digitalRead(encoderCLK); \/\/MSB = most significant bit<\/span><br \/>\n<span style=\"color: #0000ff;\"> int LSB = digitalRead(encoderDT); \/\/LSB = least significant bit<\/span><\/p>\n<p><span style=\"color: #0000ff;\">int encoded = (MSB &lt;&lt; 1) |LSB; \/\/converting the 2 pin value to single number<\/span><br \/>\n<span style=\"color: #0000ff;\"> int sum = (lastEncoded &lt;&lt; 2) | encoded; \/\/adding it to the previous encoded value<\/span><\/p>\n<p><span style=\"color: #0000ff;\">if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;<\/span><br \/>\n<span style=\"color: #0000ff;\"> if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue &#8211;;<\/span><\/p>\n<p><span style=\"color: #0000ff;\">lastEncoded = encoded; \/\/store this value for next time<\/span><br \/>\n<span style=\"color: #0000ff;\">}<\/span><\/p>\n<p><span style=\"color: #0000ff;\">\/\/ Arduino Rotary Encoder Tutorial<\/span><\/p>\n<p>Pentru butonul SW folosim exemplu clasic de la Arduino :<\/p>\n<p><span style=\"color: #0000ff;\">\/\/ Button<\/span><br \/>\n<span style=\"color: #0000ff;\">\/\/ set pin numbers:<\/span><br \/>\n<span style=\"color: #0000ff;\">const int buttonPin = 5; \/\/ the number of the pushbutton pin<\/span><br \/>\n<span style=\"color: #0000ff;\">const int ledPin = 13; \/\/ the number of the LED pin<\/span><\/p>\n<p><span style=\"color: #0000ff;\">\/\/ variables will change:<\/span><br \/>\n<span style=\"color: #0000ff;\">int buttonState = 0; \/\/ variable for reading the pushbutton status<\/span><\/p>\n<p><span style=\"color: #0000ff;\">void setup() {<\/span><br \/>\n<span style=\"color: #0000ff;\"> \/\/ initialize the LED pin as an output:<\/span><br \/>\n<span style=\"color: #0000ff;\"> pinMode(ledPin, OUTPUT);<\/span><br \/>\n<span style=\"color: #0000ff;\"> \/\/ initialize the pushbutton pin as an input:<\/span><br \/>\n<span style=\"color: #0000ff;\"> pinMode(buttonPin, INPUT);<\/span><br \/>\n<span style=\"color: #0000ff;\">}<\/span><\/p>\n<p><span style=\"color: #0000ff;\">void loop() {<\/span><br \/>\n<span style=\"color: #0000ff;\"> \/\/ read the state of the pushbutton value:<\/span><br \/>\n<span style=\"color: #0000ff;\"> buttonState = digitalRead(buttonPin);<\/span><\/p>\n<p><span style=\"color: #0000ff;\">\/\/ check if the pushbutton is pressed.<\/span><br \/>\n<span style=\"color: #0000ff;\"> \/\/ if it is, the buttonState is HIGH:<\/span><br \/>\n<span style=\"color: #0000ff;\"> if (buttonState == HIGH) {<\/span><br \/>\n<span style=\"color: #0000ff;\"> \/\/ turn LED on:<\/span><br \/>\n<span style=\"color: #0000ff;\"> digitalWrite(ledPin, HIGH);<\/span><br \/>\n<span style=\"color: #0000ff;\"> } else {<\/span><br \/>\n<span style=\"color: #0000ff;\"> \/\/ turn LED off:<\/span><br \/>\n<span style=\"color: #0000ff;\"> digitalWrite(ledPin, LOW);<\/span><br \/>\n<span style=\"color: #0000ff;\"> }<\/span><br \/>\n<span style=\"color: #0000ff;\">}<\/span><br \/>\n<span style=\"color: #0000ff;\">\/\/ Button<\/span><\/p>\n<p>Succes !<\/p>\n<p>Colectivul Roboromania<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Arduino Rotary Encoder Roboromania Tutorial pentru \u00eencep\u0103tori O schema simpla : Afi\u0219eaz\u0103 pe serial pozi\u021bia encoderului \u0218i un cod simplu : \/\/ Arduino Rotary Encoder Tutorial int encoderCLK = 2; int encoderDT = 4; volatile int lastEncoded = 0; volatile long encoderValue = 0; long lastencoderValue = 0; int lastMSB = 0; int lastLSB = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176],"tags":[],"class_list":["post-3399","post","type-post","status-publish","format-standard","hentry","category-tutoriale-arduino"],"_links":{"self":[{"href":"https:\/\/roboromania.ro\/index.php?rest_route=\/wp\/v2\/posts\/3399","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/roboromania.ro\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/roboromania.ro\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/roboromania.ro\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/roboromania.ro\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3399"}],"version-history":[{"count":3,"href":"https:\/\/roboromania.ro\/index.php?rest_route=\/wp\/v2\/posts\/3399\/revisions"}],"predecessor-version":[{"id":4202,"href":"https:\/\/roboromania.ro\/index.php?rest_route=\/wp\/v2\/posts\/3399\/revisions\/4202"}],"wp:attachment":[{"href":"https:\/\/roboromania.ro\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/roboromania.ro\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/roboromania.ro\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}