johnwasser:
SetStrokePerc(START_POSITION1);
SetStrokePerc(START_POSITION2);
SetStrokePerc(START_POSITION3);
SetStrokePerc(START_POSITION4);
Serial.println("START_POSITION");
delay(10000);
SetStrokePerc(END_POSITION1);
SetStrokePerc(END_POSITION2);
SetStrokePerc(END_POSITION3);
SetStrokePerc(END_POSITION4);
Serial.println("END_POSITION");
delay(10000);
Why call SetStrokePerc() four times when SetStrokePerc() sends each value to all four servos?!? If you EVER want the four servos to be in different positions you will need to change "SetStrokePerc()" so it knows which servo you are talking about. One way to do that is to make your own kind of 'Servo' which adds the "SetStrokePerc()" function to the Servo class.
#include <Servo.h>
class MyKindOfServo : public Servo
{
public:
void SetStrokePerc(float strokePercentage)
{
if ( strokePercentage >= 1.0 && strokePercentage <= 99.0 )
{
int usec = 1000 + strokePercentage * ( 2000 - 1000 ) / 100.0;
writeMicroseconds(usec);
}
}
};
MyKindOfServo myServo1;
MyKindOfServo myServo2;
MyKindOfServo myServo3;
MyKindOfServo myServo4;
#define PIN_SERVO1 (9)
#define PIN_SERVO2 (8)
#define PIN_SERVO3 (7)
#define PIN_SERVO4 (6)
#define START_POSITION1 (1) //min 1
#define START_POSITION2 (1) //min 1
#define START_POSITION3 (1) //min 1
#define START_POSITION4 (1) //min 1
#define END_POSITION1 (80) //max 80
#define END_POSITION2 (80) //max 80
#define END_POSITION3 (80) //max 80
#define END_POSITION4 (80) //max 80
int input = 0; //initializing variable to home
void setup()
{
// set servo pins
myServo1.attach(PIN_SERVO1);
myServo2.attach(PIN_SERVO2);
myServo3.attach(PIN_SERVO3);
myServo4.attach(PIN_SERVO4);
//open serial window for log
Serial.begin(9600);
}
void loop()
{
Serial.println("Action? (1, 2)");
input = Serial.read();
Serial.println(input); // for troubleshooting of what is the current command
//delay(10001);
if (input == '1')
{
myServo1.SetStrokePerc(START_POSITION1);
myServo2.SetStrokePerc(START_POSITION2);
myServo3.SetStrokePerc(START_POSITION3);
myServo4.SetStrokePerc(START_POSITION4);
Serial.println("START_POSITION");
delay(5000); //delay to allow servos to move
}
if (input == '2')
{
myServo1.SetStrokePerc(END_POSITION1);
myServo2.SetStrokePerc(END_POSITION2);
myServo3.SetStrokePerc(END_POSITION3);
myServo4.SetStrokePerc(END_POSITION4);
Serial.println("END_POSITION");
delay(5000); // delay to allow servos to move
}
}
Very good point. I guess I had not gotten that far. Clearly this is a requirement.
What I tried to do as I was troubleshooting the code is take the actual command definitions out of the loop. It did not seem to make sense to have them in there when in fact, in the loop what I am looking for is the the system to continuously keep asking what I want to do next. This has fixed the continuous movement that I was observing on the HW.
Here is what I came up with:
//Initialize libraries and variables
#include <Servo.h>
#define PIN_SERVO1 (9)
#define PIN_SERVO2 (8)
#define PIN_SERVO3 (7)
#define PIN_SERVO4 (6)
#define START_POSITION1 (1) //min 1
#define START_POSITION2 (1) //min 1
#define START_POSITION3 (1) //min 1
#define START_POSITION4 (1) //min 1
#define END_POSITION1 (80) //max 80
#define END_POSITION2 (80) //max 80
#define END_POSITION3 (80) //max 80
#define END_POSITION4 (80) //max 80
//String input = "H";
int input = 2;
int Target11 (1);
float strokePercentage;
//Define Servos
Servo myServo1;
Servo myServo2;
Servo myServo3;
Servo myServo4;
void setup() {
// set servo pins
myServo1.attach(PIN_SERVO1);
myServo2.attach(PIN_SERVO2);
myServo3.attach(PIN_SERVO3);
myServo4.attach(PIN_SERVO4);
//open serial window for log
Serial.begin(9600);
//define actions
Home();
Target();
Max();
}
void Home(){
SetStrokePerc(START_POSITION1);
SetStrokePerc(START_POSITION2);
SetStrokePerc(START_POSITION3);
SetStrokePerc(START_POSITION4);
Serial.println("Moving to Home");
}
void Target(){
Serial.println("Servo1-1:?");
Target11 = Serial.read();
Serial.print(Target11); //for TS purposes
SetStrokePerc(Target11);
Serial.println("Moving to Target");
}
void Max(){
SetStrokePerc(END_POSITION1);
SetStrokePerc(END_POSITION2);
SetStrokePerc(END_POSITION3);
SetStrokePerc(END_POSITION4);
Serial.println("Moving to Max");
}
void loop(){
//while (input != "Stop"){
Serial.println("Action? (H, M, T, Stop)");
input = Serial.read();
Serial.print(input); //Command confirmation
delay(8000);
if (input == 1){
Home();}
if (input == 2){
Max();}
if (input == 3){
Target();}
}
void SetStrokePerc(float strokePercentage)
{
if ( strokePercentage >= 1.0 && strokePercentage <= 99.0 )
{
int usec = 1000 + strokePercentage * ( 2000 - 1000 ) / 100.0 ;
myServo1.writeMicroseconds( usec );
myServo2.writeMicroseconds( usec );
myServo3.writeMicroseconds( usec );
myServo4.writeMicroseconds( usec );
}
}
What I am observing with this code which has me puzzled is what seems to be random positions. ie. when powered it might to to Max while no command has been given, or when I request move to max or a given position it does not seem to take the command despite it clearly recording the value.
Will try to incorporate your above suggestion to control independent servos in my next iteration.
Thanks for the help on this guys!
A.