Hi team,
I am trying to move four linear actuator connected to their corresponding LAC with a UNO. Actuators are setup in parallel.
System works as expected with this code:
#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
Servo myServo1;
Servo myServo2;
Servo myServo3;
Servo myServo4;
void setup() {
// set servo
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()
{
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);
}
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 );
}
}
They move up and down continuously so all good so far.
The next requirement is to be able to command the position of the actuators to different positions. For starters retracted and fully extended, so I modified the code to have the Serial Monitor capture the commands. The issue I am seeing is that the code seems to run linearly despite the if statements.
#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
Servo myServo1;
Servo myServo2;
Servo myServo3;
Servo myServo4;
int input = 0; //initializing variable to home
void setup() {
// set servo
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){
SetStrokePerc(START_POSITION1);
SetStrokePerc(START_POSITION2);
SetStrokePerc(START_POSITION3);
SetStrokePerc(START_POSITION4);
Serial.println("START_POSITION");
delay(5000); //delay to allow servos to move
}
else if(input = 2){
SetStrokePerc(END_POSITION1);
SetStrokePerc(END_POSITION2);
SetStrokePerc(END_POSITION3);
SetStrokePerc(END_POSITION4);
Serial.println("END_POSITION");
delay(5000); // delay to allow servos to move
}
}
void SetStrokePerc(float strokePercentage)
{
if ( strokePercentage >= 1.0 && strokePercentage <= 99.0 )
{
int usec1 = 1000 + strokePercentage * ( 2000 - 1000 ) / 100.0 ;
int usec2 = 1000 + strokePercentage * ( 2000 - 1000 ) / 100.0 ;
int usec3 = 1000 + strokePercentage * ( 2000 - 1000 ) / 100.0 ;
int usec4 = 1000 + strokePercentage * ( 2000 - 1000 ) / 100.0 ;
myServo1.writeMicroseconds( usec1 );
myServo2.writeMicroseconds( usec2 );
myServo3.writeMicroseconds( usec3 );
myServo4.writeMicroseconds( usec4 );
}
}
The expectation would be for the servos to fully retract if "input" = 1 and extend if "input"= 2.
Any idea of where I am getting it wrong?
I have also tried using Case statements with a similar outcome.
Eventually I will want to be able to have the option to define specific positions so that will be part 3.
Thanks for the help.
A.