Hey!
I need help to get something working smoothly.
I have: 5 servos
5 buttons
The 5 servos move together in 5 different patterns from 13 to 167 degrees. Each pattern is triggered by one of the 5 buttons.
I want to be able to easily modify the code to modulate the speed of the servos. It's ok if a set speed affects all servos at once. (but having them independently modifiable would be crazy!)
Now, I have tried to break the motion of the servos in incrementing steps that I could slow down or speed up by modifying the delay
between each incrementation. I think this is the way to go, but this approach gives me trouble here because the servos don't have a fixed starting point, they start where they were left at at the previous pass.
I tried working it out by making 2 different for loops, and I got lost...
Here's the original code, quite simple, and you'll see how complex it becomes with my non-working solution.
#include <Servo.h>
Servo myservo[5];
int buttonPin[] = {2, 3, 4, 5, 6};
int buttonState[] = {0, 0, 0, 0, 0};
int lastButtonState[] = {0, 0, 0, 0, 0};
int x = 11;
int servoRoll;
int positions[5][5] =
{
{13, 45, 90, 135, 167}, // button0 - servo {0, 1, 2, 3, 4}
{45, 90, 135, 167, 13}, // button1 - servo {0, 1, 2, 3, 4}
{90, 135, 167, 13, 45}, // button2 - servo {0, 1, 2, 3, 4}
{135, 167, 13, 45, 90}, // button3 - servo {0, 1, 2, 3, 4}
{167, 13, 45, 90, 135}, // button4 - servo {0, 1, 2, 3, 4}
};
void setup() {
for (int serv=0;serv<5;serv++){ //attach servos to pins
myservo[serv].attach(x); x--;
}
for(int buttonPinning = 0; buttonPinning < 5; buttonPinning++){ //set buttonPins as inputs
pinMode (buttonPin[buttonPinning], INPUT);
}
}
void moveButton() {
for(int buttonRoll = 0; buttonRoll < 5; buttonRoll++){ //buttonRoll serves as reference for 'lines' of nested array
buttonState[buttonRoll] = digitalRead(buttonPin[buttonRoll]); //reading the buttons
if ((buttonState[buttonRoll] != lastButtonState[buttonRoll])&&(buttonState[buttonRoll] == 1)) { //checking button states
for(servoRoll = 0; servoRoll < 5; servoRoll++){ //servoRoll servers as reference for 'columns' of nested array
myservo[servoRoll].write(positions[buttonRoll][servoRoll]); //tells servos to go to positions
delay(5);
}
}
else{
delay(5);
}
lastButtonState[buttonRoll] = buttonState[buttonRoll]; //sets new 'lastButtonState for the next pass
}
}
void loop(){
moveButton();
}
and now the funky stuff, it still compiles though, and the servos do move slowly, but only 2 or 3 times and in a jerky way...
#include <Servo.h>
Servo myservo[5];
int buttonPin[] = {2, 3, 4, 5, 6};
int buttonState[] = {0, 0, 0, 0, 0};
int lastButtonState[] = {0, 0, 0, 0, 0};
int ServoState[] = {0, 0, 0, 0, 0};
int mover;
int servoRoll;
int buttonRoll;
int lastServoState;
int x = 11 ;
int positions[5][5] =
{
{13, 45, 90, 135, 167}, // button0 - servo {0, 1, 2, 3, 4}
{45, 90, 135, 167, 13}, // button1 - servo {0, 1, 2, 3, 4}
{90, 135, 167, 13, 45}, // button2 - servo {0, 1, 2, 3, 4}
{135, 167, 13, 45, 90}, // button3 - servo {0, 1, 2, 3, 4}
{167, 13, 45, 90, 135}, // button4 - servo {0, 1, 2, 3, 4}
};
void setup() {
for (int serv=0;serv<5;serv++){
myservo[serv].attach(x); x--;
}
for(int buttonPinning = 0; buttonPinning < 5; buttonPinning++){
pinMode (buttonPin[buttonPinning], INPUT);
}
}
void moveButton() {
for(int buttonRoll = 0; buttonRoll < 5; buttonRoll++){
buttonState[buttonRoll] = digitalRead(buttonPin[buttonRoll]);
if ((buttonState[buttonRoll] != lastButtonState[buttonRoll])&&(buttonState[buttonRoll] == 1)){ //
for(servoRoll = 0; servoRoll < 5; servoRoll++){
if(lastServoState <= (positions[buttonRoll][servoRoll])) { //if, lastServoState for positions smaller than...
for(mover = lastServoState; mover <= (positions[buttonRoll][servoRoll]); mover ++){ //I try to use 'mover' as an incrementee to get
myservo[servoRoll].write(mover); //myservo to increment in little steps
delay(20);
}
}
else if(lastServoState > (positions[buttonRoll][servoRoll])) {
for(mover = lastServoState; mover > (positions[buttonRoll][servoRoll]); mover --){ //else, lastServoState for positions bigger than...
myservo[servoRoll].write(mover);
delay(20);
}
}
else{
delay(1);
}
};
}
else{
delay(1);
}
};
lastServoState = (positions[buttonRoll][servoRoll]); //gives lastServoState the value of the last position
lastButtonState[buttonRoll] = buttonState[buttonRoll];
}
void loop() {
moveButton();
}
any help or guidance would be greatly appreciated,
thanks