When I use your library I only get zero returned from the getCountsM1 function. When I use interrupts I get multiple counts per interrupt (edit: because I am getting noise on my 5V rail; edit again: was coming from all 3 of my SharpIRs).
In short, I believe that the encoder library does not have code to work with the Mega2560 processor to set the interrupt pins in the “enable_interrupts_for_pin” function. I will see if I can make the changes but it is out of my area of expertise.
If anyone wants to join in on the fun I think these are 2 good starting points:
Or, are we somehow using the software incorrectly?
I’m on a Mac, so we just downloaded the latest zip file, and I moved everything in the src directory over to my Documents/Arduino/libraries/ folder and restarted the Arduino software.
As I stated. That seemed to work fine when we were building and running on the Uno - we got back counts as expected (using just the first chunk of code Ted included above).
When we tried the same code on the Mega, (and yes, we set the target board to the Mega) we get nothing back - always 0.
Additionally, it would be nice if the Encoder stuff was available in an arduino-specific library as well (like the QTR Line sensors do) so it would be easier to use them on Arduino…
Ok, some more detective work has at least shed some light onto why this wasn’t working on the Mega…
You guys are making use of the Pin Change Interrupt capabilities of the AVR chip. Apparently the Mega does this a little differently due to the number of IO ports it supports.
I did find some information in the “Practical Arduino” book on how this is done on the Mega, and I also found a library called PinChangeInt that does this sort of thing on the Uno/Duem board’s ATmega328p chips, but doesn’t have the support for the Mega 2560’s chip.
So it looks like it’s just a matter of adding the support to the the library…
Maybe this can be of help? I found that some other people have had a simmialr issue with the NewSoftSerial and pin change interrupts on the Mega 2560. Apparently not all pins on the Mega can be used with the interrupts.
One guy has modified the code for the NewSoftSerial to work with the Mega. Maybe these modifications can be implemented for the wheel encoders too?
I tried your changes but I don’t get any output from the encoders using the same sketch I posed in the first post in this thread. It uses 2,3,18,19. I am guessing your code doesn’t support those pins? Which pins should I use with your code? Could you post a working sample sketch?
The thing with the ATMega2560 is that not all pins are connected to Pin Change Interrupts. The Arduino pins that are connected are (and can be used with the modifications I made): 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69.
Still didn’t work for me. I used mapped pins 36,37 and 40,41.
As an aside, do you have any code for computing speed from encoders? Seems like it would be simple but the timing between pulses seems to be random with a steady speed for me.
Just going on my understanding here, but slowly rotate the wheel, see when you get the pulse that causes the increment to occur.
Once you know that, you measure the angular change on your wheel per pulse.
Then it’s just some basic trig - angle/360 = x/circumference of your wheel. Solve for X and that tells you the linear distance that should be covered when the wheel pulses once.
For speed, look at the time difference between the last measurement taken and now and the pulse count to get the speed. V= Distance/Time
Has anyone tried using the default PololuWheelsEncoder.cpp/h with the Arduino Uno Revision 3???
Similar symptoms to the Mega’s problem. Further testing has even weirder things happening like:
Wheels stop responding to PWM signal from Arduino (through Dual Channel Motor Driver Carrier). Wiggling the wheels somehow starts it up again. Seems like somethign to do with interrupt mappings?
All other pins has weird behaviors (IE: A working ultrasonic module + code would not work when motors are also connected to MCS. Again “wiggling” somehow fixes things).
Lastly, what exactly do I have to rewrite in the PololuWheelsEncoder source do I have to do to get it work with the Arduino Uno Revision 3?
@makopack
I actually first saw your post on the Arduino forums?
Did you a have any luck in getting everything working? What fixed it? (I’m assuming you probably changed some of the interrupt->pin mappings).