Let's Hook a Rotary Encoder Up To My Pet Arduino (Part 2)
Vložit
- čas přidán 19. 11. 2013
- This is part two of the series on Rotary Encoders with Arduino. I'm testing the concept of adding this feature to my Morse Code Decoder project.
Here is the sketch that I used in this demo:
Create a folder called: Rotary_Encoder_Demo
Download the following file and save it in that folder
drive.google.com/folderview?i... - Věda a technologie
This is part two of the series on Rotary Encoders with Arduino. I'm testing the concept of adding this feature to my Morse Code Decoder project.
Here is the sketch that I used in this demo:
-* Rotary Encoder Demo
Budd Churchward WB7FHC
11-20-13
*-
const byte pinA = 2; -- encoder pin A to Arduino pin 2 which is also interrupt pin 0 which we will use
const byte pinB = 3; -- encoder pin B to Arduino pin 3 which is also interrupt pin 1 but we won't use it
byte state = 0; -- will store two bits for pins A & B on the encoder which we will get from the pins above
int level = 0; -- a value bumped up or down by the encoder
-* For demo purposes we will create an array of these binary digits *-
String bits[] = {-00-,-01-,-10-,-11-};
-* A truth table of possible moves 1 for clockwise
-1 for counter clockwwise 0 for error - keybounce *-
int bump[] = {0,0,-1,1};
void setup(){
pinMode(pinA,INPUT); -- reads Pin A of the encoder
pinMode(pinB,INPUT); -- reads Pin B of the encoder -* Writing to an Input pin turns on an internal pull up resistor *-
digitalWrite(pinA,HIGH);
digitalWrite(pinB,HIGH); -* Set up to call our knob function any time pinA rises *-
attachInterrupt(0,knobTurned,RISING); -- calls our 'knobTurned()' function when pinA goes from LOW to HIGH level = 50; -- a value to start with -* Set up for using the on-screen monitor *-
Serial.begin(115200); -- make sure your monitor baud rate matches this
Serial.println(-Encoder Ready-);
Serial.print(-level = -);
Serial.println(level); -- to remind us where we're starting
}
void loop(){
-* main programming will go here later for now we'll just do nothing until we get an interrupt *-
}
void knobTurned(){
-* AH HA! the knob was turned *-
state = 0; -- reset this value each time
state = state + digitalRead(pinA); -- add the state of Pin A
state --= 1; -- shift the bit over one spot
state = state + digitalRead(pinB); -- add the state of Pin B -* now we have a two bit binary number that holds the state of both pins 00 - something is wrong we must have got here with a key bounce 01 - sames as above - first bit should never be 0 10 - knob was turned backwards 11 - knob was turned forwards -* *- We can pull a value out of our truth table and add it to the current level *-
level = level + bump[state]; -* Let's see what happened *-
Serial.print(bits[state] + - -); -- show us the two bits
Serial.print(bump[state],DEC); -- show us the direction of the turn
Serial.print(- -);
Serial.println(level); -- show us the new value
}
Budd
Am a VERY new Arduino user...having trouble loading sketch for demo... am using Arduino 1.0.6 is that the problem? Seems to not like comment punctuation.
73
Gary N8WTT
Gary Schultz
Sorry about that, Gary. I was new to posting text on CZcams when I put that sketch up there. CZcams does a funny thing. The correct comment marker is a slant followed by and asterisk and the beginning and an asterisk followed by a slash at the end.
CZcams doesn't like us to use these symbols! So it changed the slashes to hyphens.
While in your IDE type a CTRL-F. When the find window opens, Put *- in the find box and put * and a slash mark in the replace box. Then hit 'Replace All'
Do it again with -* in the top and slash mark * in the replace box.
Then I think you will be good to go.
Gave your suggestions a try, still running into errors..... Can you please
send the file to my ARRL email? ******@****
Gary Schultz
Google and CZcams mask out your email address so I can't read it. Give me your call sign. I know the rest.
I have edited the description for this video so you may be able to download the file from the link I provided there.
You can email me at wb7fhc [you know the rest]
IF anyone has trouble with the download on the youtube page here is the code. make sure to save it as Rotary_Encoder_Demo and place it in a folder with that name as well.
const byte pinA = 2; // encoder pin A to Arduino pin 2 which is also interrupt pin 0 which we will use
const byte pinB = 3; // encoder pin B to Arduino pin 3 which is also interrupt pin 1 but we won't use it
byte state = 0; // will store two bits for pins A & B on the encoder which we will get from the pins above
int level = 0; // a value bumped up or down by the encoder
/* For demo purposes we will create an array of these binary digits */
String bits[] = {"00","01","10","11"};
/* A truth table of possible moves
1 for clockwise
-1 for counter clockwwise
0 for error - keybounce */
int bump[] = {0,0,-1,1};
void setup(){
pinMode(pinA,INPUT); // reads Pin A of the encoder
pinMode(pinB,INPUT); // reads Pin B of the encoder
/* Writing to an Input pin turns on an internal pull up resistor */
digitalWrite(pinA,HIGH);
digitalWrite(pinB,HIGH);
/* Set up to call our knob function any time pinA rises */
attachInterrupt(0,knobTurned,RISING); // calls our 'knobTurned()' function when pinA goes from LOW to HIGH
level = 50; // a value to start with
/* Set up for using the on-screen monitor */
Serial.begin(115200); // make sure your monitor baud rate matches this
Serial.println("Encoder Ready");
Serial.print("level = ");
Serial.println(level); // to remind us where we're starting
}
void loop(){
/* main programming will go here later
for now we'll just do nothing until we get an interrupt */
}
void knobTurned(){
/* AH HA! the knob was turned */
state = 0; // reset this value each time
state = state + digitalRead(pinA); // add the state of Pin A
state
Thank you, Budd! Simple to understand and works! I've been bouncing around Google and CZcams for an hour trying to find a straightforward example like this.
What an awesome video! Clear and concise. Subscribed!
Hi Budd buddy... thanks for this tutorial... Im on the trak to make my own controler... will share as soon is ready...
keep it up!!!! Cheers... XD
thanks mate, elegant code :)
very useful video. thanks a lot
Awsome! Thank you!
Budd, great tutorial...thank you very much. My project needs 6 rotary encoders so I need to attach each one to its own interrupt pin (unique pin)?
I have a question what kind of knobs do you use with a rotary encoder can you use just regular potentiometer knobs of do you have to use special ones, and does the encoder you used have a build in button?
Budd, have you made any instructional video on using the 567 tone decoder chip?
cool stuff
thanks! Very helpful
Hi, the rotary encoder you use looks of high quality. Where can I find high quality 24 pulse rotary encoders with a push switch??
Nice! Thanks
Hi Budd. I'm searching for a way to control an encoder equipped DC motor with an Arduino. Can you advise if the Arduino is capable of reading an encoder at a high rate? I'm looking to control distance primarily then speed. A home position would be established by an end stop switch. I have a motor controller (previously purchased) I'm considering to use. It has two sets of servo pins, can these be repurposed to read the encoder? Thanks.
Hey Budd
Can't get the code to work.....
Hello,
no matter which way i turn my encoder the value only increases. Any help?
Budd, I can't get your code to compile?
Very nice explanation.
PU2CLR.
Ricardo.
Hello Budd, This is the exact code i am looking for for a project I'm working on but, I can't seem to get it to work. I have taken out all comments and tried to correct compile errors but, to no avail. Your help, or anybody that is on this thread, is needed. Cheers.
This is amazing. Can you post a ino file instead of pde file. Thanks in advance
simple question.... why do you call it your "pet" arduino" ?
now i want to play with rotary encoders >.>
btw, you might want to change the step slightly, considering how far you have to go from one end to another it doesn't lead to rapid tuning >.>
Excellent video this is precisely what I was looking for . One thing not mentioned was,are the capacitors value Could you tell me what they are.
+SnapTowels 0.01 mF
+Budd Churchward Thank you for the info
Very good tutorial but very frustrating trying to get your sketch. The drive.google link gives me a .pde file...what is that all about? Can't use it as a sketch. I went to your google+ account...way too much stuff at random and I paged down and down and down...way too much stuff to look through...Google+ what a mess they offer us...so I'm stuck...no sketch and don't know how to get it.
+Ralph Freshour This is one of my older videos. If you email me at: wb7fhc@arrl.com, I will find the code and send it to you. I probably have a few versions you can experiment with.
Ralph Freshour A .pde file IS a sketch file. It's the old version. Arduino can convert it to an .ino file automatically.
The arduino is hanging after working for sometime. After that, I want to reset the arduino to work again. It will again hang working sometime.
Of course I can't know for sure what is happening with your sketch, but I do know that when we run interrupts other procedures that use timing like delay( ) and millis( ) can have problems.
Budd Churchward
Now solved. As per the Arduino documentation, RS232 operations inside Interrupt code is not recommended. I change the code to display the output into a LCD. Now its working perfectly, except some debounce problem - It's not a problem.
Anish Vargheese great to hear. If you ever run across anything that would handle flow control on the RS232 let me know. I haven't had time to look myself yet, but it would help in a project that will be coming up later.
Budd Churchward I am developing a VFO using Arduino. The rotary encoder is used to change the frequency. I will publish the code after completing the project. I fork your code to used with PIC Micro. Available at tyni.in/TIRlz7h
Wow thanks a lot'' you are a genius
hmm i dont understand it quite yet, the serial monitor gives a 00 for every knob turn like this:
00 0 50
10 -1 49
00 0 49
10 -1 48
00 0 48
10 -1 47
00 0 47
11 1 48
00 0 48
11 1 49
00 0 49
11 1 50
00 0 50
10 -1 49
00 0 49
10 -1 48
Enhanced Pyrotechnics Did you put capacitors across the terminals on the encoder? Your numbers look like mine when I remove them.
Enhanced Pyrotechnics Did you put capacitors across the terminals on the encoder? Your numbers look like mine when I remove them.
Budd Churchward I watched another guy demo the encoder and say that the .01 did not work so well for bounce, so he kicked it up to .1. is there a down side to what he did?
I had a play with the code, and managed to get it working.. Needed hardware debounce on the encoder outputs.. 0.01uf Cap to ground, 4k7 resistors to the arduino, all simple stuff..
Anyway, here's a linky to the working code .
www.dropbox.com/sh/rnnfz42nzweizz7/AACZYL_mrbOI59f5F7GDkPMoa?dl=0
Thanks for sharing the code, Paul. I've used the capacitors to help with the debounce but I have not tried adding the resistors. I will give that a go.
Thanks for the link man;)
Budd,
Great job. Nice tutorial. Very clear. I had some problems with the code posted in the comments here so I re-did a bit of it and put it into a "gist" on github. It compiles now and I hope to test the code soon.
Here is the gist:
gist.github.com/dmccreary/fe578928cb480de4c9b9