This is a continuation of my previous post. When we left off, I was trying to figure out which pins on the Sparkfun SevSeg display I could repurpose for my own purposes as digital out pins.
I did some research on the ATMega chip, and it is pretty clear that most of the pins on the top row of the SevSeg could be used for other purposes. Having identified which (Arduino) pins were connected to the display, I was able to figure out which pins were connected to the top row of solder points by using a simple programs that displayed a number on the LCD while simultaneously toggling that pins state to high and low. I had some trial and error — for example my multimeter takes about 4 seconds to register a change in voltage and my initial script was changing the voltages faster than that… at first it seemed like nothing was happening! But finally I figured out the pin numbers for the solder points. Only after I did all that did I look in the header file provided by the Sparkfun “board” for the SevSeg and see that they’d provided convenient macros for those points! All in all I found 6 available digital I/O pins.
I decided to connect five pins to the Easy Driver — Enable, Step, Dir, MS1 and MS2. I left MS3 disconnected. This will allow me to control the direction of rotation, turn off power to the motor, and use half, quarter and one-eight stepping modes for slow speeds.
That left one pin for a button. I verified that I could make inputs work by simply connecting one output pin to one input pin by sliding a resistor between the two solder points. When I programmatically set one of them to output HIGH while putting the other into INPUT_PULLUP, I verified that I could read a state change.
Next I soldered connections between the buttons and the EasyDriver and the SevSeg. I’m also using the EasyDriver as a power supply for the SevSeg.
I found some example code for how to drive the stepper motor by sending alternating HIGH and LOW pulses to the STEP pin, each with some delay. This worked perfectly, on the first try. However, when I tried adding the digital display to show how many rotations are left, I ran into a problem. The SevSeg.cpp uses delays internally to rapidly illuminate each digit in succession. I also needed carefully timed delays between each STEP I send to the stepper. Arduino doesn’t have any threading support. I was facing having to build some kind of scheduler to allow these two tasks to do small bits of work offset by time delays. Ugh.
But then I discovered the clever pt.h “threads” library by Adam Dunkels. This imposes some pretty strict limitations on your “C” code — e.g. locals are not persistent across WAITs. But the solution to that was the (normally frowned upon) practice of using static/globals for everything! The original SevSeg library was built around a C++ object, which I had to refactor to be a static “C” function — easily done once I made all its member fields public (another no-no, normally!).
The end result: I can control both the motor and the display simultaneously without one (significantly) interfering with the other.
And here is a demo of the finished project: