I´m finding hard to post code of what I´m doing, or even ask quentions about it, I´m a bit confused..
So, basically what I would like to do is, to use the function servoControl(), to control all the servos in my sketch. The way it is now, I can control just one servo.
I can make it control more servos but it will take a lot of code, and I know that if this could become class based it would be easier and very straightforward to use.
I´m using the Metro library to update the servo destination values while they need to be updated.. If you think I should use another library please advice.
// this sketch cycles three servos at different rates
// =========================== Servo library
#include <ServoTimer2.h> // the servo library
// define the pins for the servos
#define servo1Pin 2
ServoTimer2 servo1; // declare variables for up to eight servos
ServoTimer2 servoName;
int actualPos;
int dest;
int inc;
// ========================= Metro library
// http://www.arduino.cc/playground/Code/Metro
#include <Metro.h>
int servo1_duration = 4000;
Metro servo1_metro = Metro(servo1_duration);
int servo1_control = 0;
int servoControl_duration = 99999;
Metro servoControl_ID = Metro(servoControl_duration);
// ============================== Setup()
void setup() {
servo1.attach(servo1Pin); // attach a pin to the servos and they will start pulsing
}
// =============================== Loop()
void loop() {
if (servoControl_ID.check() == 1) {
servoControl(servoName, dest, inc);
}
if (servo1_metro.check() == 1) {
if (servo1_control == 0) {
servoControl(servo1, 750, 50);
servo1_control++;
}
else if (servo1_control == 1) {
servoControl(servo1, 1500, 25);
servo1_control++;
}
else {
servoControl(servo1, 2200, 4);
servo1_control = 0;
}
}
}
// =============== servoControl()
void servoControl (ServoTimer2 _servoName, int _dest, int _inc) {
// activate the metro for position update
servoIntervalReset(10);
servoName = _servoName;
int val = servoName.read();
dest = _dest;
inc = _inc;
int newPos;
if (val > dest) {
newPos = val - inc;
} else if (val < dest) {
newPos = val + inc;
} else {
servoIntervalReset(99999);
}
servoName.write(newPos);
}
// ============== servoIntervalReset()
// metro reset function
void servoIntervalReset(int duration) {
servoControl_ID.interval(duration);
servoControl_ID.reset();
}