Hello,
If I had followed the guidelines better and search the forum more thoroughly, I would have found a library written by Korman that does just that, servo speed modulation.
But still, I again learned a lot from all the people who give you just enough information to give you hope…
here’s my final code:
#include <VarSpeedServo.h>
VarSpeedServo myservo[5];
int ledPin[] = {14, 15, 16, 17, 18};
int buttonPin[] = {2, 3, 4, 5, 6};
int buttonState[] = {0, 0, 0, 0, 0};
int lastButtonState[] = {1, 1, 1, 1, 1};
int servospeeds[] = {10, 20, 30, 40, 50};
int positions[5][5] =
{
{3, 45, 90, 135, 177}, // button0 - servo {0, 1, 2, 3, 4}
{45, 90, 135, 177, 3}, // button1 - servo {0, 1, 2, 3, 4}
{90, 135, 177, 3, 45}, // button2 - servo {0, 1, 2, 3, 4}
{135, 177, 3, 45, 90}, // button3 - servo {0, 1, 2, 3, 4}
{177, 3, 45, 90, 135}, // button4 - servo {0, 1, 2, 3, 4}
};
void setup()
{
int f, g, h;
Serial.begin(9600);
int x = 11;
for (f=0; f<5; f++)
{
myservo[f].attach(x); x--;
}
for(g = 0; g < 5; g++)
{
pinMode (buttonPin[g], INPUT);
}
for(h = 14; h < 19; h++)
{
pinMode (ledPin[h], OUTPUT);
}
}
void moveServo(int pp)
{
int j, m;
for (m = 0; m < 5; m++)
{
digitalWrite(ledPin[m], HIGH);
}
buttonState[pp] = digitalRead(buttonPin[pp]);
if ((buttonState[pp] != lastButtonState[pp])&&(buttonState[pp] == 0))
{
digitalWrite(ledPin[pp], LOW);
delay(50);
for(j = 0; j < 5; j++)
{
myservo[j].slowmove(positions[pp][j], servospeeds[3]);
delay(10);
}
}
lastButtonState[pp] = buttonState[pp];
}
void loop()
{
int p;
for (p = 0; p < 5; p++)
{
moveServo(p);
}
}
thanks again to everyone.
Below is the original post
trying to slow multiple servos with this piece of code. I increment (or decrement) the positions of the servos a step at a time. I try to make a pause between each of these steps.
void moveup(int pp)
{
for(pos[i] = lastPos[i]; pos[i] <= positions[pp][i]; pos[i] ++) // pos = int variable, stores value to be given to servo and
{ // positions = values of end of travel for servos
myservo[i].write(pos[i]); // lastPos = position from where the servo starts
delay(30); // value of [i] is incremented from 0 to 4 from another function
}
}
This code is half-working, I think delay is giving me trouble, it makes servos move to 0 before each move and the servos move one after the other instead of all together, but the speed modulation does work. Then I tried to use the Millis trick
void moveup(int pp)
{
unsigned long currentMillis = millis();
for(pos[i] = lastPos[i]; pos[i] <= positions[pp][i]; pos[i] ++)
{
if(currentMillis - previousMillis > interval) // interval = delay value, anywhere btwn 1 and 100 ms.
{
previousMillis = currentMillis;
myservo[i].write(pos[i]);
}
}
}
but it’s not working. I think the problem is because the Millis trick works when in the void loop(). Here it just makes 1 servo go to pos = 0 and that’s it. Also, I don’t think I can put this function in the void loop() because I use the void loop ‘frequency’ to modulate the argument ‘pp’.
So, what I want is to pause between each increment of the for loop, am I on the right track?
thanks