Syncing 4 Actuators

Hello,
Recently I have started a project to synchronize 4 Linear Actuators. In my current build I have gotten 2 of them to sync. Now I'm at the point where I need to implement the other two. I was wondering if anyone had any suggestions for how to execute that. I have linked my code with pretty extensive commenting.

My approach was using if statements to determine which was lower in the cycle and just speed it up compared to the one higher in the cycle. Which is simple for two actuators, but if I were to use same method with four actuators then it would be A LOT of if statements. So if anyone has a better approach I would much appreciate the feedback.

Thanks.

/*
Synchronization of Two Linear Actuators
BUILD: V1.0
*/

#include "CytronMotorDriver.h"
#include <stdio.h>

// Configure the motor driver.
CytronMD motor1(PWM_DIR, 3, 2); //PWM = Pin 3, DIR = Pin 2.
CytronMD motor2(PWM_DIR, 5, 2); //PWM = Pin 5, DIR = Pin 2.

//Difference Allowed For Potentiometers to Read "Equal"
const int buffer = 3;

//Initalizing The Potentiometers
int motor1Pot = A1;
int motor2Pot = A2;

//Raw Input Pot Values
int m1PotVal;
int m2PotVal;

//Averaged Pot Values
int m1PotAvg;
int m2PotAvg;

//Mapped Values
int m1PotValMap;
int m2PotValMap;

//Initalizing Toggle Switch 
int switchUp = A9;
int switchDown = A10;

void setup() {
  Serial.begin(9600);
  pinMode(switchUp,INPUT);
  pinMode(switchDown,INPUT);
  pinMode(motor1Pot,INPUT);
  pinMode(motor2Pot,INPUT);
}

void loop(){
  //Read The Potentiometers
  readPots();
  
  //Print Difference in Pots
  Serial.print(abs(m2PotValMap - m1PotValMap));
  Serial.print("    ");
  Serial.print(abs(m1PotValMap - m2PotValMap));
  Serial.print("    ");
  
  //If Swich is Pressed Up
  if(analogRead(switchUp) > 500){
    Up();
  }
  
  //If Switch is Pressed Down
  if(analogRead(switchDown) > 500){
    Down();
  }

  //If the Switch is in Neutral
  if(analogRead(switchDown) < 500 && analogRead(switchUp) < 500){
    motor1.setSpeed(0);
    motor2.setSpeed(0);
  }

  delay(1);
}

void readPots(){
  m1PotVal = 0;
  m2PotVal = 0;

  for(int i=0; i<4; i++){
    //Get Raw Input
    m1PotVal = m1PotVal + analogRead(motor1Pot);
    m2PotVal = m2PotVal + analogRead(motor2Pot);
  }

  m1PotAvg = m1PotVal/4;
  m2PotAvg = m2PotVal/4;

  //Map to Same Scale
  m1PotValMap = map(m1PotAvg, 291, 717, 0, 425);
  m2PotValMap = map(m2PotAvg, 288, 715, 0, 425);

  //Print the Mapped and Average Values
  Serial.print(m1PotValMap);
  Serial.print("    ");
  Serial.print(m2PotValMap);
  Serial.print("    ");
  Serial.print(m1PotAvg);
  Serial.print("    ");
  Serial.println(m2PotAvg);

  delay(1);
}

void Up(){
  //Test Whether Difference in Pots Are Within Buffer
  if(Equal_LR() && Equal_RL()){
    motor1.setSpeed(-240);
    motor2.setSpeed(-240);
    Serial.print("Equal");
  }
  else{
    EqualizeUp(); 
  }
}

void EqualizeUp(){
  if(m1PotValMap > m2PotValMap){
    motor1.setSpeed(-200);//Keep Speed or Slow Down
    motor2.setSpeed(-255);//Move Up Faster
    Serial.print("M1 Is Higher");
  }
  if(m1PotValMap < m2PotValMap){
    motor1.setSpeed(-255);//Move Up Faster
    motor2.setSpeed(-190);//Keep Speed or Slow Down
    Serial.print("M2 Is Higher");
  }
}

void Down(){
  //Test Whether Difference in Pots Are Within Buffer
  if(Equal_LR() && Equal_RL()){
    motor1.setSpeed(240);
    motor2.setSpeed(240);
    Serial.print("Equal");
  }
  else{
    EqualizeDown();
  }
}

void EqualizeDown(){
  if(m1PotValMap > m2PotValMap){
    motor1.setSpeed(255);//Move Down Faster
    motor2.setSpeed(190);//Keep Speed or Slow Down
    Serial.print("M1 Is Higher");
  }
  if(m1PotValMap < m2PotValMap){
    motor1.setSpeed(230);//Keep Speed or Slow Down
    motor2.setSpeed(255);//Move Down Faster
    Serial.print("M2 Is Higher");
  }
}

boolean Equal_LR(){
  return (abs(m2PotValMap - m1PotValMap) < buffer);
}
boolean Equal_RL(){
  return (abs(m1PotValMap - m2PotValMap) < buffer);
}/code]

When you get beyond two it's usually time to use arrays instead of individual variables.

const byte ActuatorCount = 4;
const byte FeedbackPins[ActuatorCount] = {A1, A2, A3, A4};
const byte PWMPins[ActuatorCount]  =  {3, 5, 6, 9};
const byte DirectionPin = 2;
CytronMD motors[ActuatorCount] = 
{
  CytronMD(PWM_DIR, PWMPins[0], DirectionPin),
  CytronMD(PWM_DIR, PWMPins[1], DirectionPin),
  CytronMD(PWM_DIR, PWMPins[2], DirectionPin),
  CytronMD(PWM_DIR, PWMPins[3], DirectionPin)
};

Then you can use loops to find the slowest (lowest going up or highest going down) and slow the others.

  byte currentHighest = 0;
  int highest = analogRead(FeedbackPins[currentHighest]);
  for (byte i=1; i< ActuatorCount; i++)
  {
    int height = analogRead(FeedbackPins[i]);
    if (height > highest)
    {
      currentHighest = i;
      highest = height;
    }
  }
  // Now we know which one is highest and how high it is.  Look for anything lower and slow them down.

johnwasser:
When you get beyond two it's usually time to use arrays instead of individual variables.

const byte ActuatorCount = 4;

const byte FeedbackPins[ActuatorCount] = {A1, A2, A3, A4};
const byte PWMPins[ActuatorCount]  =  {3, 5, 6, 9};
const byte DirectionPin = 2;
CytronMD motors[ActuatorCount] =
{
  CytronMD(PWM_DIR, PWMPins[0], DirectionPin),
  CytronMD(PWM_DIR, PWMPins[1], DirectionPin),
  CytronMD(PWM_DIR, PWMPins[2], DirectionPin),
  CytronMD(PWM_DIR, PWMPins[3], DirectionPin)
};




Then you can use loops to find the slowest (lowest going up or highest going down) and slow the others.



byte currentHighest = 0;
  int highest = analogRead(FeedbackPins[currentHighest]);
  for (byte i=1; i< ActuatorCount; i++)
  {
    int height = analogRead(FeedbackPins[i]);
    if (height > highest)
    {
      currentHighest = i;
      highest = height;
    }
  }
  // Now we know which one is highest and how high it is.  Look for anything lower and slow them down.

Oh I see what your saying. Thats smart! Thanks for the tip.