Yesterday I mentioned I was working to get the encoders working so we can measure the speed the wheels are turning at, I also mentioned I could get the encoders working but not at the same time as the motors.  You’d think that’d be a clue but apparently not!

A bit of background.

I’m using an Arduino Leonardo and a motor shield from Adafruit: 

This shield is brilliant, it allows you to very easily control up to four motors.  I’d already burned out one of the two controllers on the board but I’ll detail that when I get around to controlling the sweeper brush in the future.  Spoiler alert, it used more power than the chip can handle and the magic smoke was released…

Regarding the encoders, in order to count them accurately we use a feature of the Arduino which are interrupts pins.  When a change is detected on these pins, the other functions of your code will be stopped and whatever you assigned to the interrupt will run instead.  In my case I have two interrupts, one for each encoder, each of which incremented a counter.  Measuring how fast that changes gives us the speed.

This worked a treat in isolation but when I used the motor shield it all stalled, I’ve discovered why.

As they are special the Leonardo has a limited number of pins which can be used for interrupts.  0, 1, 2, 3, & 7.  0 and 1 are used for the serial port so I used 2 & 3 instead, I completely forgot these are used by the motor shield!

I switched over to pin 7 and lo and behold it worked!  With the Leonardo is means I can only monitor the speed of one motor which isn’t much use.  Using an Arduino Mega gives us four more to play with on different ports, I’ve one somewhere in my parts boxes so will have to dig it out.

Very glad to at least figure out why this wasn’t working, happy to share that lesson too!