Hi, I have problem with setting speed in my 12 servos. I don’t want to use delay() function, but timers.
My programs on Arduino using messenger library to read 12 values of angles, and currently, I’m controlling speed of move servos by sending this values at some frequency. It’s not reliable, because I have to use pause() function in program on PC who is sending messages, so sometimes something goes wrong.
I want to send example angles 130 20 30 40 50 33 etc. and Arduino set them with some speed (defined by frequency set in timer.attach() ). With one servo function using timers looks like this:
static byte angle = 0;
// connect function running every 0,02s
timer.attach(0, 20, servoMove);
// timer update
But how to create the same with 12 servos :~ ? Servos must be set in similar time (they move part of legs in my robot)
My current function looks like this:
int angle ;
for (int i=0;i<12;i=i++)
angle[i] = message.readInt(); // function from Messenger library
if ((angle[i]>180) || (angle[i] < 0))
else if (angle[i] == servos[i].read())
Serial.print("won't write angle number( ");
Serial.println(") = ");
Serial.print("written angle number (");
Serial.print(") = ");
Are all 12 servos supposed to complete a move at the same time?
I think the parts of servos (6 even and 6 odd) should complete their move at the same time, because it's hexapod robot, so one part of servos is responsible for moving leg up and the second is rotating leg.
But the angles for this part will be the same (sometimes in the opposite direction) because all legs are moving in the same way (only sometimes different when the body rotate)
I don't see that a timer is necessary, then. You have a state machine, with two states - waiting for step data and stepping.
If you are stepping, there is a time that the last increment was made and a time between increments. Use millis() to record when the last increment was made and to determine if it is time to increment again. Use floats to determine the angle that each servo rotates at each increment (based on the current angle and new angle for each servo, determine the largest change required. One servo rotates this amount; all the others rotate smaller amounts).
Each servo needs to advance some amount at each iteration. If one servo is to rotate 20 degrees, and another rotates 10 degrees, there will be 20 iterations. One servo rotates 1.0 degrees per iteration, while the other rotates 0.5 degrees per iteration. For each iteration, you need to determine if it is time to perform the iteration. If it is, you need to determine the new value for each servo for the iteration, based on the current position and the increment per iteration. Apply the new angle to each servo when it is time, and record the time.
Have you considered using the servo library? Might be ab easier place to start.