I am trying to program two actuators using an X and Y analog joystick. Everything compiles when i cut out either top (actuator1) or bottom (actuator2) section in the void loop! What am i doing wrong, so it compiles?
const int feedback = A0; //potentiometer from actuator
const int pot = A1; //pot from throttle
const int feedback2 = A2; //potentiometer from actuator
const int pot2 = A3; //pot from throttle
const int enable = 4;
const int PWMA = 3;
const int PWMB = 5;
const int enable2 = 7;
const int PWMC = 6;
const int PWMD = 9;
int actMax = 760;
int actMin = 250;//positions of actuator
int actMax2 = 760;
int actMin2 = 250;//positions of actuator
int potMin = 0;
int potMax = 1023;
int potMin2 = 0;
int potMax2 = 1023;
int precision = 2;//how close to final value to get
int checkingInterval = 50;//how often position is checked (milliseconds)
int precision2 = 2;//how close to final value to get
int checkingInterval2 = 50;//how often position is checked (milliseconds)
int rawcurrentPosition = 0;
int currentPosition = 0;
int rawdestination = 0;
int destination = 0;
int difference = 0;//values for knowing location
int rawcurrentPosition2 = 0;
int currentPosition2 = 0;
int rawdestination2 = 0;
int destination2 = 0;
int difference2 = 0;//values for knowing location
void setup()
{
pinMode(feedback, INPUT);//feedback from actuator
pinMode(pot, INPUT);//feedback from potentiometer
pinMode(feedback2, INPUT);//feedback from actuator
pinMode(pot2, INPUT);//feedback from potentiometer
pinMode(enable, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(PWMB, OUTPUT);//three pins for MegaMoto
pinMode(enable2, OUTPUT);
pinMode(PWMC, OUTPUT);
pinMode(PWMD, OUTPUT);//three pins for MegaMoto
digitalWrite(enable,HIGH);
digitalWrite(enable2,HIGH);
Serial.begin(9600);
}
void loop() //--------------------------------------------------------------------------------------------------------
{
destination = getDestination();
currentPosition = analogRead(feedback);//check where you are
Serial.print("Position ");
Serial.println(analogRead(feedback));
difference = destination - currentPosition;//find out how far you are from the destination
if (currentPosition > destination) pullActuatorUntilStop(destination);// choose what action to take
else if (currentPosition < destination) pushActuatorUntilStop(destination);
else if (difference < precision && difference > -precision) stopActuator();
}//end void loop
int getDestination()
{
rawdestination = analogRead(pot);//read the potentiometer to get the destination
destination = map(rawdestination, potMin,potMax,actMin,actMax);//convert the potentiometer feedback to match the actuator
return(destination);
}//end getDestination
void pushActuatorUntilStop(int destination)
{
destination = getDestination();
int temp = analogRead(feedback);
difference = destination - temp;//check difference to see if continue moving, or stop
while (difference > precision || difference < -precision)
{
destination = getDestination();
temp = analogRead(feedback); //continue checking difference
difference = destination - temp;
pushActuator();
}//end while
delay(25);
stopActuator();
}//end pushActuatorUntilStop
void pullActuatorUntilStop(int destination)
{
destination = getDestination();
int temp = analogRead(feedback); //check difference to see if continue moving, or stop
difference = destination - temp;
while (difference > precision || difference < -precision)
{
destination = getDestination();
temp = analogRead(feedback); //continue checking difference
difference = destination - temp;
pullActuator();
}//end while
delay(25);
stopActuator();
}//end pullActuatorUntilStop
void stopActuator()
{
analogWrite(PWMA,0);
analogWrite(PWMB,0);
}//end stopActuator
void pushActuator()
{
analogWrite(PWMB,255);
analogWrite(PWMA,0);
}//end pushActuator
void pullActuator()
{
analogWrite(PWMB,0);
analogWrite(PWMA,255);
}//end pullActuator
//addon secound acuator //---------------------------------------------------------------------------------------------
{
destination2 = getDestination2();
currentPosition2 = analogRead(feedback2);//check where you are
Serial.print("Position2 ");
Serial.println(analogRead(feedback2));
difference2 = destination2 - currentPosition2;//find out how far you are from the destination
if (currentPosition2 > destination2) pullActuatorUntilStop2(destination2);// choose what action to take
else if (currentPosition2 < destination2) pushActuatorUntilStop2(destination2);
else if (difference2 < precision2 && difference2 > -precision2) stopActuator2();
}//end void loop
int getDestination2()
{
rawdestination2 = analogRead(pot2);//read the potentiometer to get the destination
destination2 = map(rawdestination2, potMin2,potMax2,actMin2,actMax2);//convert the potentiometer feedback to match the actuator
return(destination2);
}//end getDestination2
void pushActuatorUntilStop2(int destination2)
{
destination2 = getDestination2();
int temp2 = analogRead(feedback2);
difference2 = destination2 - temp2;//check difference to see if continue moving, or stop
while (difference2 > precision2 || difference2 < -precision2)
{
destination2 = getDestination2();
temp2 = analogRead(feedback2); //continue checking difference
difference2 = destination2 - temp2;
pushActuator2();
}//end while
delay(25);
stopActuator2();
}//end pushActuatorUntilStop2
void pullActuatorUntilStop2(int destination2)
{
destination2 = getDestination2();
int temp2 = analogRead(feedback2); //check difference to see if continue moving, or stop
difference2 = destination2 - temp2;
while (difference2 > precision2 || difference2 < -precision2)
{
destination2 = getDestination2();
temp2 = analogRead(feedback2); //continue checking difference
difference2 = destination2 - temp2;
pullActuator2();
}//end while
delay(25);
stopActuator2();
}//end pullActuatorUntilStop2
void stopActuator2()
{
analogWrite(PWMC,0);
analogWrite(PWMD,0);
}//end stopActuator2
void pushActuator2()
{
analogWrite(PWMD,255);
analogWrite(PWMC,0);
}//end pushActuator2
void pullActuator2()
{
analogWrite(PWMD,0);
analogWrite(PWMC,255);
}//end pullActuator2