I am trying to get 2 actuators to move and the same time/speed/length. If they are not at the same stroke I want them to equalize(one of the actuator expand/contract to the other). I am close but it is turning the motor off and on too quickly in order to achieve this goal. I want to smooth this out and get it to be more precise. here is the code. And an early thanks for those who help
//Variables
int Act0Up = 2; // Sends signal to Coil side of relay to move actuator 0 UP
int Act1Up = 3; // Sends signal to Coil side of relay to move actuator 1 UP
int Act0Down = 4; // Sends signal to Coil Side of relay to move actuator 0 DOWN
int Act1Down = 5; // Sends signal to Coil Side of relay to move actuator 1 DOWN
int KeyUp = 6; // Reads signal from keylock switch. If high it will be used to move the actuators UP
int KeyDown = 7; // Reads signal from keylock switch. If high it will be used to move the actuators DOWN
int ActPot0 = A0; // Reads signal from potentiometer in actuator 0 (left side when looking at back of trailer).
int ActPot1 = A1; // Reads signal from potentiometer in actuator 1 (right side when looking at back of trailer).
int ActPot0Val; // Variable to save the value of the potentiaometer from ActPot0
int ActPot1Val; // Variable to save the value of the potentiaometer from ActPot1
int ActPot0MapVal; // Variable to store the new map value of the potentiameter
int ActPot1MapVal; // Variable to store the new map value of the potentiameter
int LimitUp; // Limit to be set to stop the actuators from extending to far
int LimitDown; // Limit to be set to stop the actuators from retracting to far
int i=0;
void setup() {
pinMode(Act0Up, OUTPUT);
pinMode(Act1Up, OUTPUT);
pinMode(Act0Down, OUTPUT);
pinMode(Act1Down, OUTPUT);
pinMode(KeyUp, INPUT);
pinMode(KeyDown, INPUT);
digitalWrite(Act0Up, LOW);
digitalWrite(Act1Up, LOW);
digitalWrite(Act0Down, LOW);
digitalWrite(Act1Down, LOW);
digitalWrite(KeyUp, LOW);
digitalWrite(KeyDown, LOW);
ReadPots();
Serial.begin(9600);
delay(250);
}
void loop() {
Serial.print("i value : ");
Serial.println(i);
ReadPots();
if(digitalRead(KeyUp) == HIGH){
Up();
}
else {
digitalWrite(Act1Up, LOW);
digitalWrite(Act0Up, LOW);
}
if(digitalRead(KeyDown) == HIGH){
Down();
}
else{
digitalWrite(Act1Down, LOW);
digitalWrite(Act0Down, LOW);
}
}
void ReadPots() {
for (i = 1; i < 4; ++i){
ActPot0Val = ActPot0Val + analogRead(ActPot0);
ActPot1Val = ActPot1Val + analogRead(ActPot1);
}
ActPot0Val = ActPot0Val / 4;
ActPot1Val = ActPot1Val / 4;
Serial.print("Actuator0 AVG: ");
Serial.println(ActPot0Val); // Prints to the Serial Monitor the value of the POT 0
Serial.print("Actuator1 AVG: ");
Serial.println(ActPot1Val); // Prints to the Serial Monitor the value of the POT 1
delay(25);
ActPot0MapVal = map(ActPot0Val, 77, 990, 0, 255);
ActPot1MapVal = map(ActPot1Val, 80, 994, 0, 255);
Serial.print("Actuator0 MV: ");
Serial.println(ActPot0MapVal); // Prints to the Serial Monitor the mapped value of the POT 0
Serial.print("Actuator1 MV: ");
Serial.println(ActPot1MapVal); // Prints to the Serial Monitor the mapped value of the POT 1
delay(25);
}
void Up(){ // If the Up keylock switch is activated then it will move the actuators Up
if (ActPot0MapVal != ActPot1MapVal){
EqualizeUP();
}
else if(ActPot0MapVal == ActPot1MapVal){
digitalWrite(Act1Down, LOW);
digitalWrite(Act0Down, LOW);
digitalWrite(Act1Up, HIGH);
digitalWrite(Act0Up, HIGH);
delay(5);
}
}
void Down(){ // If the Down keylock switch is activated then it will move the actuators Down
if (ActPot0MapVal != ActPot1MapVal){
EqualizeDOWN();
}
else if(ActPot0MapVal == ActPot1MapVal){
digitalWrite(Act1Up, LOW);
digitalWrite(Act0Up, LOW);
digitalWrite(Act1Down, HIGH);
digitalWrite(Act0Down, HIGH);
delay(5);
}
}
void EqualizeUP(){
if(ActPot0MapVal > ActPot1MapVal){
digitalWrite(Act0Up, HIGH);
digitalWrite(Act1Up, LOW);
delay(5);
}
if(ActPot0MapVal < ActPot1MapVal){
digitalWrite(Act0Up, LOW);
digitalWrite(Act1Up, HIGH);
delay(5);
}
}
void EqualizeDOWN(){
if(ActPot0MapVal > ActPot1MapVal){
digitalWrite(Act1Down, HIGH);
digitalWrite(Act0Down, LOW);
delay(5);
}
if(ActPot0MapVal < ActPot1MapVal){
digitalWrite(Act1Down, LOW);
digitalWrite(Act0Down, HIGH);
delay(5);
}
}