The first program (T2MultiServo1.ino) is pretty much a copy of the Adafruit code. All I did was to remove the Flasher class since I didn't need it. SIGNAL was changed to ISR since SIGNAL is now deprecated. I also added the external interrupt for the reset button. That code is part of the same Adafruit example. The purpose here was simply to see how the code worked and check out the interface board I built. Although the code is running on a Teensy 2, it will run equally well on an Arduino Uno or a Mega. It WILL NOT run on a Teensy 3 or other ARM-based Arduinos. I'll need to figure out more about the ARM timers and perhaps the interrupt structure. Future work!
- Servos need independent power! I'm using a 6V, 1A wall wart. Ran 6 SG-5010 and 2 SG90 servos simultaneously for a few minutes and WW was just barely warm. I didn't try to find the limits of this setup. It worked for testing.
- I built a simple board to connect power and the control signals. The schematic and a picture are attached. Doesn't get much simpler.
- The two big caps are there to smooth out surges during servo motion.
Adding Control of Angle and Time
In T2MultiServo2.ino, I modified the Sweeper class to allow a move to a specified position at the programmed rate, then stop until directed to a new position. I also added a bit of code in loop() to exercise it. One perhaps interesting detail is the use of elapsedMillis objects to control servo position changes at independent times. The elapsedMillis class is just one more elegant, convenient implementation we can thank Paul Stoffregen for!
T2MultiServo3.ino adds programmable sweep time. Specifying the time allowed for a move to a new angle will be translated to the time interval between incremental moves. The only caveat, and one for which there is *no* checking, is that the move to a new position cannot happen faster than one degree per millisecond. So if we want to move 180 degrees say, in 100 milliseconds, it ain't gonna happen! Not yet anyhow. Since my concern is to move physical objects at reasonable rates, then this restriction is not an issue for me. But since I'm releasing the code to the wild, this problem must be dealt with. More Future Work. It would also be useful to be able to move less than 1 degree per time increment if very slow moves were desired. Right now, such slow moves will appear jerky, but at least they will happen.
Array Control: Enabling Group Moves
If you've followed the details so far, then T2MultiServo4.ino is your reward! It implements group moves of servo motors in a controlled manner. The program should allow you to implement your own design easily. Following is useful information for those who want to know more.
This final version of the Sweeper class requires no arguments to instantiate a Sweeper object. An array of Sweepers can now be created. This keeps the code for creating, initializing, and updating the servos nice and clean. Individual Sweeper objects (servos) are referred to by index. Indexing in this manner is very useful, as illustrated by the functions described below.
This version of the Sweeper class provides separate functions to initialize a move with destination and time, and to enable the move. Doing this allows several moves to be set up, then all started at the same time, for a group move. Now it is likely that this is not necessary in most cases, but I thought it would be nice to have and would eliminate worries caused by starting moves at staggered times. There is also a function that will produce the old behaviour if desired. Another useful function is resetTo() which moves a servo immediately to a desired position. This is most useful at startup to put all the servos into a known state. Note that the motors should be put in this position prior to powering down. Then there will be no sudden jerks as the program starts again.
The changes just described support a "group move" function. A group of servos may be easily defined (as an array), along with a corresponding...Read more »