Combine remote and local control on a robot

I everyone!

I'm building a shop crane that I want to be able to control both wirelessly and localy. I have a 3 axis joystick and a hoist crane wireless controller.

I have a code for the remote and a code for the joystick but can't successfully combine both.

I want to add x, y and z axis from the joystick to the following code:


const int moteurGAv (5);
const int moteurGArr (6);
const int moteurDAv (9);
const int moteurDArr (10);

const int mAv (22);  //foward Remote
const int mArr (24); //backward Remote
const int mGAv (26); //left turn Remote
const int mDAv (28); //right turn Remote



//const int winchUp (30);
//const int winchDown (32);

//const int selRL (34);
//const int onRemote (36);


const unsigned long ACCEL_SPEEDus = 1000; // 3.9 ms between speed increments
const unsigned long ACCEL_SPEEDus2 = 1000;
const unsigned long ACCEL_SPEEDus3 = 12000;
const unsigned long ACCEL_SPEEDus4 = 12000;
const int decelAjs = 50;

unsigned long most_recent_bump_us;
unsigned long most_recent_bump_us2;
unsigned long most_recent_bump_us3;
unsigned long most_recent_bump_us4;



int speed1;
int speed2;
int speed3;
int speed4;
int speed5;
int speed6;
int speed7;
int speed8;



void setup() {
 Serial.begin(9600);

  pinMode(moteurGAv, OUTPUT); 
  pinMode(moteurGArr, OUTPUT);
  pinMode(moteurDAv, OUTPUT); 
  pinMode(moteurDArr, OUTPUT);




  pinMode(mAv, INPUT);   
  pinMode(mArr, INPUT);  
  pinMode(mGAv, INPUT);  
  pinMode(mDAv, INPUT);  

  //pinMode(winchUp, INPUT);    
  //pinMode(winchDown, INPUT);  

  //pinMode(selRL, INPUT);  //selector  Remote / Local

  //pinMode(onRemote, INPUT); // ON button on the Remote 


  
  analogWrite(moteurGAv, 0);
  analogWrite(moteurGArr, 0);
  analogWrite(moteurDAv, 0);
  analogWrite(moteurDArr, 0);
  
  speed1 = 0;
  speed2 = 0;
  speed3 = 0;
  speed4 = 0;
  speed5 = 0;
  speed6 = 0;
  speed7 = 0;
  speed8 = 0;


}


void loop() {
                 if(digitalRead(mAv) == LOW)
                 slowDown();
           
                 if((digitalRead(mAv) == HIGH)&&(speed3 ==0 )&&(speed4 ==0)&&(speed5 <100)&&(speed7 < 100))
                 speedUp();
        

                 if(digitalRead(mArr) == LOW)
                  slowDown2();
                 if((digitalRead(mArr) == HIGH)&&(speed1 == 0)&&(speed2 == 0)&&(speed5==0)&&(speed7==0)) 
                  speedUp2();
        

                                  if(digitalRead(mGAv) == LOW)
                                    slowDown3();
                                 if((digitalRead(mGAv) == HIGH)&&(speed3 == 0)&&(speed4 == 0)&&(speed1==0)&&(speed2==0))
                                  speedUp3();
         

                                 if((digitalRead(mDAv) == LOW) && (digitalRead(mAv)==LOW))
                                   slowDown4();
                                 if((digitalRead(mDAv) == HIGH)&&(speed3 == 0)&&(speed4 == 0)&&(speed1==0)&&(speed2==0))
                                    speedUp4();
         
      delay(5);
      }

      void slowDown() {




        if((speed1 == 0)||(speed2 == 0))
        return; // stopped
        //if(speed2 == 0) return;

        unsigned long t = micros();

        if((speed1 == 255)||(speed2==255)) {
          if (digitalRead(mGAv) == HIGH)
      
          //start of deceleration
          most_recent_bump_us = t;      
        }
        else if(t - most_recent_bump_us < ACCEL_SPEEDus) {
          // not time to bump the speed yet
          return;
        }
        else {
            // this method keeps the rate consistent
            most_recent_bump_us += ACCEL_SPEEDus;      
        }

      
          analogWrite(moteurGAv, --speed1);
         
      
      
          analogWrite(moteurDAv, --speed2);
        
        }

      void speedUp() {
        if((speed1 == 255) || (speed2 == 255))return; // max speed

        unsigned long t = micros();

        if((speed1 == 0)||(speed2 == 0)) {
          // start of acceleration
          most_recent_bump_us = t;      
        }
        else if(t - most_recent_bump_us < ACCEL_SPEEDus) {
          // not time to bump the speed yet
          return;
        }
        else {
            // this method keeps the rate consistent
            most_recent_bump_us += ACCEL_SPEEDus;      
        }

        
          analogWrite(moteurGAv, ++speed1);
        

        
          analogWrite(moteurDAv, ++speed2);
        }
        

  
      void slowDown2() {
        if((speed3 == 0)||(speed4 == 0))
        return; // stopped
        //if(speed2 == 0) return;

        unsigned long t = micros();

        if((speed3 == 255)||(speed4==255)) {
          //start of deceleration
          most_recent_bump_us2 = t;      
        }
        else if(t - most_recent_bump_us2 < ACCEL_SPEEDus2) {
          // not time to bump the speed yet
          return;
        }
        else {
            // this method keeps the rate consistent
            most_recent_bump_us2 += ACCEL_SPEEDus2;      
        }

      
          analogWrite(moteurGArr, --speed3);
      
      
          analogWrite(moteurDArr, --speed4);
        
      }

      void speedUp2() {
        if((speed3 == 255) || (speed4 == 255))return; // max speed

        unsigned long t = micros();

        if((speed3 == 0)||(speed4 == 0)) {
          // start of acceleration
          most_recent_bump_us2 = t;      
        }
        else if(t - most_recent_bump_us2 < ACCEL_SPEEDus2) {
          // not time to bump the speed yet
          return;
        }
        else {
            // this method keeps the rate consistent
            most_recent_bump_us2 += ACCEL_SPEEDus2;      
        }

        
          analogWrite(moteurGArr, ++speed3);
        

        
          analogWrite(moteurDArr, ++speed4);
        

      }

      void slowDown3() { //left turn
        if(speed5 == 0)
        return; // stopped
        //if(speed2 == 0) return;

        unsigned long t = micros();

        if(speed5 == 255) {
          //start of deceleration
          most_recent_bump_us3 = (t/decelAjs);      
        }
        else if(t - most_recent_bump_us3 < ACCEL_SPEEDus3) {
          // not time to bump the speed yet
          return;
        }
        else {
            // this method keeps the rate consistent
            most_recent_bump_us3 += ACCEL_SPEEDus3;      
        }

      
          analogWrite(moteurGArr, --speed6);
      
      
          analogWrite(moteurDAv, --speed5);
        
      }

      void speedUp3() {
        if(speed5 == 255)return; // max speed

        unsigned long t = micros();

        if(speed5 == 0){
          // start of acceleration
          most_recent_bump_us3 = t;      
        }
        else if(t - most_recent_bump_us3 < ACCEL_SPEEDus3) {
          // not time to bump the speed yet
          return;
        }
        else {
            // this method keeps the rate consistent
            most_recent_bump_us3 += ACCEL_SPEEDus3;      
        }

        
          
        

        
          analogWrite(moteurDAv, ++speed5);
          analogWrite(moteurGArr, ++speed6);

      }




      void slowDown4() { //right turn
        if(speed7 == 0)
        return; // stopped
        //if(speed2 == 0) return;

        unsigned long t = micros();

        if(speed7 == 255) {
          //start of deceleration
          most_recent_bump_us4 = (t/decelAjs);      
        }
        else if(t - most_recent_bump_us4 < ACCEL_SPEEDus4) {
          // not time to bump the speed yet
          return;
        }
        else {
            // this method keeps the rate consistent
            most_recent_bump_us4 += ACCEL_SPEEDus4;      
        }

      
          analogWrite(moteurGAv, --speed7);
      
      
          analogWrite(moteurDArr, --speed8);
        
      }

      void speedUp4() {
        if(speed7 == 255)return; // max speed

        unsigned long t = micros();

        if(speed7 == 0) {
          // start of acceleration
          most_recent_bump_us4 = t;      
        }
        else if(t - most_recent_bump_us4 < ACCEL_SPEEDus4) {
          // not time to bump the speed yet
          return;
        }
        else {
            // this method keeps the rate consistent
            most_recent_bump_us4 += ACCEL_SPEEDus4;      
        }

        
          
        

        
          analogWrite(moteurDArr, ++speed8);
          analogWrite(moteurGAv, ++speed7);



}











I took this code from internet that works pretty well but have to add smooth stop / soft start to it and the selector mode function and combine it to the remote code.

#define enA 9

#define in1 4

#define in2 5

#define enB 10

#define in3 6

#define in4 7

int const deadband = 400;

int motorSpeedA = 0;

int motorSpeedB = 0;




void setup() {

  pinMode(enA, OUTPUT);

  pinMode(enB, OUTPUT);

  pinMode(in1, OUTPUT);

  pinMode(in2, OUTPUT);

  pinMode(in3, OUTPUT);

  pinMode(in4, OUTPUT);


 //motorSpeedA.attach(4);  //smoothing program
 //motorSpeedB.attach(5);

}



void loop() {

  int xAxis = analogRead(A0); // Read Joysticks X-axis

  int yAxis = analogRead(A1); // Read Joysticks Y-axis

  int zAxis = analogRead(A2);



  // z-axis used for fast left and right turn control
if (zAxis < 470-deadband) {

    // Set Motor A backward

    digitalWrite(in1, HIGH);

    digitalWrite(in2, HIGH);

    // Set Motor B Forward

    digitalWrite(in3, LOW);

    digitalWrite(in4, LOW);

    // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed

    motorSpeedA = map(zAxis, 470, 0, 0, 255);

    motorSpeedB = map(zAxis, 470, 0, 0, 255);
}

    else if (zAxis > 550 + deadband) {

    // Set Motor A forward

    digitalWrite(in1, LOW);

    digitalWrite(in2, LOW);

    // Set Motor B BACKWARD

    digitalWrite(in3, HIGH);

    digitalWrite(in4, HIGH);

    // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed

    motorSpeedA = map(zAxis, 550, 1023, 0, 255);

    motorSpeedB = map(zAxis, 550, 1023, 0, 255);
   delay(100);

}




else if (470 < zAxis < 550)

  if (yAxis < 470) {

    // Set Motor A backward

    digitalWrite(in1, HIGH);

    digitalWrite(in2, HIGH);

    // Set Motor B backward

    digitalWrite(in3, HIGH);

    digitalWrite(in4, HIGH);

    // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed

    motorSpeedA = map(yAxis, 470, 0, 0, 255);

    motorSpeedB = map(yAxis, 470, 0, 0, 255);
    delay(100);

  }

  else if (yAxis > 550) {

    // Set Motor A forward

    digitalWrite(in1, LOW);

    digitalWrite(in2, LOW);

    // Set Motor B forward

    digitalWrite(in3, LOW);

    digitalWrite(in4, LOW);

    // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed

    motorSpeedA = map(yAxis, 550, 1023, 0, 255);

    motorSpeedB = map(yAxis, 550, 1023, 0, 255);
    delay(100);

  }

  // If joystick stays in middle the motors are not moving

  else {

    motorSpeedA = 0;

    motorSpeedB = 0;
    delay(100);

  }



  // X-axis used for left and right control

  if (xAxis < 470) {

    // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value

    int xMapped = map(xAxis, 470, 0, 0, 255);

    // Move to left - decrease left motor speed, increase right motor speed

    motorSpeedA = motorSpeedA - xMapped;

    motorSpeedB = motorSpeedB + xMapped;

    // Confine the range from 0 to 255

    if (motorSpeedA < 0) {

      motorSpeedA = 0;

    }

    if (motorSpeedB > 255) {

      motorSpeedB = 255;
      delay(100);

    }

  }

  if (xAxis > 550) {

    // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value

    int xMapped = map(xAxis, 550, 1023, 0, 255);

    // Move right - decrease right motor speed, increase left motor speed

    motorSpeedA = motorSpeedA + xMapped;

    motorSpeedB = motorSpeedB - xMapped;

    // Confine the range from 0 to 255

    if (motorSpeedA > 255) {

      motorSpeedA = 255;

    }

    if (motorSpeedB < 0) {

      motorSpeedB = 0;
      delay(100);

    }

  }

  // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)

  if (motorSpeedA < 70) {

    motorSpeedA = 0;

  }

  if (motorSpeedB < 70) {

    motorSpeedB = 0;

  }

  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A

  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
  delay(100);
}


THANKS!!!

Please edit your post to add code tags (select and use "<code_/>" button).

Also, clearly explain the problem. What should the code do, and what does it do instead?

Thanks!

I edited the post, but anti-scam blocked it.

I want to be able to select local mode or remote mode with the selector on the crane and combine both code as the joystick mode also work with the soft start of the remote code.

Thanks for your help!

The code appears to be properly posted, but is very hard to read because of all the unnecessary blank lines and excess spacing.

Please explain the specific problems you are having, rather than presenting a wish list.

first, I can't combine these two because they both return the value to 0 while the input of one of them is 0.
second, I would like to find a way to smooth joystick map for soft start / smooth stop, like the remote program does. I tried to put mapped from the axis in the remote sketch but as they are different scoop it doesn't work.

What are "they"? What is "the value"?

What does this mean?

I tried to put mapped from the axis in the remote sketch

Please take a few moments to read what you have written, and check that it makes sense, before hitting the Reply button.

Forum members don't know anything at all about your project.

a joystick have 2 pots, each one is an axis. I map each Axis 0-1023 to 0-255 to pwm.
I tried to put the result of the map in the fonction of the remote (slowDown & speedUp).

I tried to compare current map from previous map, if current map is > than previous, accelerate, at the rate of the speedUp function but it's going nowhere.

the value 0 is what is ''analogWrite'' to the pin. both program overwrite each other to 0.

I'm sorry if it seems confusing I'm pretty novice with arduino .

That's a wrong approach. Arbitration should occur with the inputs, favoring either remote or local control. After that the handling of the current command is the same for both controls.

how would you do that? I tried to create a function (selector mode),

void selectorMode() {
if (digitalRead(selRL==HIGH))
remoteMode();

if (digitalRead(selRL==LOW))
localMode();

}

then,

void remoteMode() {
(digital read of the inputs options from the remote)
}

... samething with the inputs from the joysticks..

when I compile, every fonction bellow pop as an error.

Please learn to properly format and present your code.

A bit easier:

void selectorMode() {
  if (digitalRead(selRL)==HIGH) //parentheses!!!
    remoteMode();
  else
    localMode();
}

Which error? Please learn to provide error messages.

my bad! but they were correct in the code!

if I remove '' { '' at the end of ''void Loop(){'',

I got ''error: 'slowDownG1' was not declared in this scope''.

Also get the same error for each ''slowDown'' and ''speedUp''.

If I keep it, I got:

error: a function-definition is not allowed here before '{' token.

if I keep ''{ '' to the ''void Loop(){'' and erase both ''{'' and '' }'' from the ''selectorMode() fonction, the code compile but don't work as the selector actualy does nothing, witch return to my initial problem: both loop overwrite each other.

Thanks for your Help I appreciate!

Nobody understands your code, neither the compiler nor me.

Where is "here"?
Please provide a few lines of code around the line with the error message.

int mGav(5);
int mGarr(6);
int mDav(9);
int mDarr(10);


int moteurGAv;
int moteurGArr;
int moteurDAv;
int moteurDArr;
const int mAv(22);   //bouton de marche avant Remote
const int mArr(24);  //bouton de marche arriere Remote
const int mGAv(26);  //bouton de virage a gauche Remote
const int mDAv(28);  //bouton de virage a droite Remote



//const int winchUp (30);
//const int winchDown (32);

//const int selRL (34);
//const int onRemote (36);


const unsigned long ACCEL_SPEEDus = 1000;  // 3.9 ms between speed increments
const unsigned long ACCEL_SPEEDus2 = 1000;
const unsigned long ACCEL_SPEEDus3 = 12000;
const unsigned long ACCEL_SPEEDus4 = 12000;
const int decelAjs = 50;

unsigned long most_recent_bump_us;
unsigned long most_recent_bump_us2;
unsigned long most_recent_bump_us3;
unsigned long most_recent_bump_us4;



int speed1;
int speed2;
int speed3;
int speed4;
int speed5;
int speed6;
int speed7;
int speed8;


int xAxis(97);  // Read Joysticks X-axis
int yAxis(96);
int zAxis(95);

int selRL(42);

int deadband = 150;

void selectorMode();



void setup() {
  Serial.begin(9600);

  pinMode(moteurGAv, OUTPUT);   // PWM sortie moteur Gauche en Rotation Avant
  pinMode(moteurGArr, OUTPUT);  // PWM sortie moteur Gauche en Rotation Arriere
  pinMode(moteurDAv, OUTPUT);   // PWM sortie moteur Droite en Rotation Avant
  pinMode(moteurDArr, OUTPUT);  // PWM sortie moteur Droite en Rotation Arriere


  pinMode(xAxis, INPUT);

  pinMode(yAxis, INPUT);

  pinMode(zAxis, INPUT);


  pinMode(mAv, INPUT);   //bouton de marche avant Remote
  pinMode(mArr, INPUT);  //bouton de marche arriere Remote
  pinMode(mGAv, INPUT);  //bouton de virage a gauche Remote
  pinMode(mDAv, INPUT);  //bouton de virage a droite Remote

  //pinMode(winchUp, INPUT);    //montée du treuil
  //pinMode(winchDown, INPUT);  // descente du treuil

  //pinMode(selRL, INPUT);  //selecteur de poste de controle Remote / Local

  //pinMode(onRemote, INPUT); //bouton ON Remote (permission de marche)


  analogWrite(moteurGAv, 0);
  analogWrite(moteurGArr, 0);
  analogWrite(moteurDAv, 0);
  analogWrite(moteurDArr, 0);

  speed1 = 0;
  speed2 = 0;
  speed3 = 0;
  speed4 = 0;
  speed5 = 0;
  speed6 = 0;
  speed7 = 0;
  speed8 = 0;
}


void loop() 

  void selectorMode() {
    if (digitalRead(selRL) == HIGH)
      RemoteMode();

    if (digitalRead(selRL) == LOW)
      localMode();
  }


  void RemoteMode() {
    if (digitalRead(mAv) == LOW)
      slowDownG1();
    slowDownD1();

    if ((digitalRead(mAv) == HIGH) && (speed3 == 0) && (speed4 == 0))
      speedUpG1();
    speedUpD1();


    if (digitalRead(mArr) == LOW)
      slowDown2();
    if ((digitalRead(mArr) == HIGH) && (speed1 == 0))
      speedUp2();


    if (digitalRead(mGAv) == LOW)
      slowDown3();
    if ((digitalRead(mGAv) == HIGH) && (speed3 == 0) && (speed4 == 0) && (speed1 == 0) && (speed2 == 0))
      speedUp3();


    if ((digitalRead(mDAv) == LOW) && (digitalRead(mAv) == LOW))
      slowDown4();
    if ((digitalRead(mDAv) == HIGH) && (speed3 == 0) && (speed4 == 0) && (speed1 == 0) && (speed2 == 0))
      speedUp4();


    if ((digitalRead(mAv) == LOW) && (digitalRead(mDAv) == LOW))
      slowDownD1();
    slowDownG1();
    if ((digitalRead(mAv) == HIGH) && (digitalRead(mDAv) == HIGH))
      speedUpG1();
    slowDownD1();



    if ((digitalRead(mGAv) == LOW) && (digitalRead(mAv) == LOW))
      slowDownG1();
    slowDownD1();
    if ((digitalRead(mDAv) == HIGH) && (speed3 == 0) && (speed4 == 0) && (speed1 == 0) && (speed2 == 0))
      speedUpD1();
    slowDownG1();

    delay(5);
  }


  void slowDownG1() {




    if (speed1 == 0)
      return;  // stopped
    //if(speed2 == 0) return;

    unsigned long t = micros();

    if (speed1 == 255) {
      if (digitalRead(mGAv) == HIGH)

        //start of deceleration
        most_recent_bump_us = t;
    } else if (t - most_recent_bump_us < ACCEL_SPEEDus) {
      // not time to bump the speed yet
      return;
    } else {
      // this method keeps the rate consistent
      most_recent_bump_us += ACCEL_SPEEDus;
    }


    moteurGAv = --speed1;
  }

  void speedUpG1() {
    if (speed1 == 255) return;  // max speed

    unsigned long t = micros();

    if (speed1 == 0) {
      // start of acceleration
      most_recent_bump_us = t;
    } else if (t - most_recent_bump_us < ACCEL_SPEEDus) {
      // not time to bump the speed yet
      return;
    } else {
      // this method keeps the rate consistent
      most_recent_bump_us += ACCEL_SPEEDus;
    }


    moteurGAv = ++speed1;
  }



  void slowDownD1() {
    if ((speed3 == 0) || (speed4 == 0))
      return;  // stopped
    //if(speed2 == 0) return;

    unsigned long t = micros();

    if (speed2 == 255) {
      //start of deceleration
      most_recent_bump_us2 = t;
    } else if (t - most_recent_bump_us2 < ACCEL_SPEEDus2) {
      // not time to bump the speed yet
      return;
    } else {
      // this method keeps the rate consistent
      most_recent_bump_us2 += ACCEL_SPEEDus2;
    }





    moteurDArr = --speed2;
  }


  void speedUpD1() {
    if (speed2 == 255) return;  // max speed

    unsigned long t = micros();

    if (speed2 == 0) {
      // start of acceleration
      most_recent_bump_us = t;
    } else if (t - most_recent_bump_us < ACCEL_SPEEDus) {
      // not time to bump the speed yet
      return;
    } else {
      // this method keeps the rate consistent
      most_recent_bump_us += ACCEL_SPEEDus;
    }






    moteurDAv = ++speed2;
  }



  void slowDown2() {
    if ((speed3 == 0) || (speed4 == 0))
      return;  // stopped
    //if(speed2 == 0) return;

    unsigned long t = micros();

    if ((speed3 == 255) || (speed4 == 255)) {
      //start of deceleration
      most_recent_bump_us2 = t;
    } else if (t - most_recent_bump_us2 < ACCEL_SPEEDus2) {
      // not time to bump the speed yet
      return;
    } else {
      // this method keeps the rate consistent
      most_recent_bump_us2 += ACCEL_SPEEDus2;
    }


    moteurGArr = --speed3;


    moteurDArr = --speed4;
  }

  void speedUp2() {
    if ((speed3 == 255) || (speed4 == 255)) return;  // max speed

    unsigned long t = micros();

    if ((speed3 == 0) || (speed4 == 0)) {
      // start of acceleration
      most_recent_bump_us2 = t;
    } else if (t - most_recent_bump_us2 < ACCEL_SPEEDus2) {
      // not time to bump the speed yet
      return;
    } else {
      // this method keeps the rate consistent
      most_recent_bump_us2 += ACCEL_SPEEDus2;
    }


    moteurGArr = ++speed3;



    moteurDArr = ++speed4;
  }

  void slowDown3() {  //virage a gauche
    if ((speed2 == 0) || (speed3 == 0))
      return;  // stopped
    //if(speed2 == 0) return;

    unsigned long t = micros();

    if ((speed2 == 100) || (speed3 == 100)) {
      //start of deceleration
      most_recent_bump_us3 = (t / decelAjs);
    } else if (t - most_recent_bump_us3 < ACCEL_SPEEDus3) {
      // not time to bump the speed yet
      return;
    } else {
      // this method keeps the rate consistent
      most_recent_bump_us3 += ACCEL_SPEEDus3;
    }


    moteurGArr = --speed3;


    moteurDAv = --speed2;
  }

  void speedUp3() {
    if ((speed2 == 100) || (speed3 == 100)) return;  // max speed

    unsigned long t = micros();

    if ((speed2 == 0) || (speed3 == 0)) {
      // start of acceleration
      most_recent_bump_us3 = t;
    } else if (t - most_recent_bump_us3 < ACCEL_SPEEDus3) {
      // not time to bump the speed yet
      return;
    } else {
      // this method keeps the rate consistent
      most_recent_bump_us3 += ACCEL_SPEEDus3;
    }






    moteurDAv = ++speed2;
    moteurGArr = ++speed3;
  }




  void slowDown4() {  //virage droite
    if ((speed1 == 0) || (speed4 == 4))
      return;  // stopped
    //if(speed2 == 0) return;

    unsigned long t = micros();

    if ((speed1 == 100) || (speed4 == 100)) {
      //start of deceleration
      most_recent_bump_us4 = (t / decelAjs);
    } else if (t - most_recent_bump_us4 < ACCEL_SPEEDus4) {
      // not time to bump the speed yet
      return;
    } else {
      // this method keeps the rate consistent
      most_recent_bump_us4 += ACCEL_SPEEDus4;
    }


    moteurGAv = --speed1;


    moteurDArr = --speed4;
  }

  void speedUp4() {
    if ((speed1 == 150) || (speed4 == 150)) return;  // max speed

    unsigned long t = micros();

    if ((speed1 == 0) || (speed4 == 0)) {
      // start of acceleration
      most_recent_bump_us4 = t;
    } else if (t - most_recent_bump_us4 < ACCEL_SPEEDus4) {
      // not time to bump the speed yet
      return;
    } else {
      // this method keeps the rate consistent
      most_recent_bump_us4 += ACCEL_SPEEDus4;
    }






    moteurDArr = ++speed4;
    moteurGAv = ++speed1;


    analogWrite(mDarr, moteurDArr);
    analogWrite(mGarr, moteurGArr);
    analogWrite(mDav, moteurDAv);
    analogWrite(mGav, moteurGAv);
  }


  void localMode() {



    digitalRead(xAxis);
    // Read Joysticks Y-axis
    digitalRead(yAxis);

    digitalRead(zAxis);


    int motorSpeedA;
    int motorSpeedAarr;
    int motorSpeedB;
    int motorSpeedBarr;





    // z-axis used for fast left and ri)ght turn control
    if (zAxis < (470 - deadband)) {

      // Set Motor A backward



      // Set Motor B Forward


      // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed

      motorSpeedAarr = map(zAxis, 470, 0, 0, 150);

      motorSpeedB = map(zAxis, 470, 0, 0, 150);
    }

    else if (zAxis > (550 + deadband)) {

      // Set Motor A forward


      // Set Motor B BACKWARD


      // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed

      motorSpeedA = map(zAxis, (550 + deadband), 1023, 0, 150);

      motorSpeedBarr = map(zAxis, (550 + deadband), 1023, 0, 150);
    }




    if (((470 - deadband) < zAxis) && (zAxis < (550 + deadband)))

      if (yAxis < (470 - deadband)) {

        // Set Motor A backward



        // Set Motor B backward


        // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed

        motorSpeedAarr = map(yAxis, (470 - deadband), 0, 0, 255);

        motorSpeedBarr = map(yAxis, (470 - deadband), 0, 0, 255);
      }

    if (yAxis > (550 + deadband)) {

      // Set Motor A forward


      // Set Motor B forward


      // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed

      motorSpeedA = map(yAxis, (550 + deadband), 1023, 0, 255);

      motorSpeedB = map(yAxis, (550 + deadband), 1023, 0, 255);


    }

    // If joystick stays in middle the motors are not moving

    else {

      motorSpeedA = 0;
      motorSpeedAarr = 0;


      motorSpeedB = 0;
      motorSpeedBarr = 0;
    }



    // X-axis used for left and right control

    if (xAxis < (470 - deadband)) {

      // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value

      motorSpeedAarr = map(xAxis, (470 - deadband), 0, 0, 255);



      // Confine the range from 0 to 255

      if (motorSpeedA < 1) {

        motorSpeedA = 0;
      }

      if (motorSpeedB > 255) {

        motorSpeedB = 255;
      }
    }

    if (xAxis > (550 + deadband)) {

      // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value

      motorSpeedBarr = map(xAxis, (550 + deadband), 1023, 0, 255);

      // Move right - decrease right motor speed, increase left motor speed


      // Confine the range from 0 to 255

      if (motorSpeedA > 255) {

        motorSpeedA = 255;
      }

      if (motorSpeedB < 1) {

        motorSpeedB = 0;
      }
    }

    // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)

    if (motorSpeedA < 1) {

      motorSpeedA = 0;
    }

    if (motorSpeedB < 1) {

      motorSpeedB = 0;
    }

    analogWrite(mGav, motorSpeedA);  // Send PWM signal to motor A
    analogWrite(mDarr, motorSpeedAarr);


    analogWrite(mDav, motorSpeedB);  // Send PWM signal to motor B
    analogWrite(mDarr, motorSpeedBarr);

    delay(10);
   
}

Why do you want to define a function within another function?

I strongly suggest that you start learning C or C++ before you start modifying some code.

ino:107:3: error: expected initializer before 'void'
107 | if(digitalRead(mGAv) == LOW)
| ^

exit status 1

Compilation error: expected initializer before 'void'

without the ''{'' of the ''void loop()'' , this is the only error I get.

if I put the ''{'' after ''void loop()'' , I get multiple error. I tought it was missing a ''}'' somewhere at the end of the program but the two sketches works separately.

this is how I combine these:

void loop(){

..(increase indent)..

{ '' sketch 1''}
{ '' sketch 2''}

}

I don't want to define a second function,

if I write:

void digitalRead(selRL),...

 void loop() {

  digitalRead(selRL) {
    if (digitalRead(selRL) == HIGH)
      RemoteMode();

    if (digitalRead(selRL) == LOW)
      localMode();
  }



I got the following error:
Compilation error: expected ';' before '{' token

this is why I was using the selectorMode() fonction.

This code compile as it should.
Not tested yet.

int deadbandX = 350;
int deadbandY = 150;
int deadbandZ = 250;




int mGav(5);
int mGarr(6);
int mDav(9);
int mDarr(10);


int moteurGAv;
int moteurGArr;
int moteurDAv;
int moteurDArr;
const int mAv(22);   //bouton de marche avant Remote
const int mArr(24);  //bouton de marche arriere Remote
const int mGAv(26);  //bouton de virage a gauche Remote
const int mDAv(28);  //bouton de virage a droite Remote



int winchUp (30);
int winchDown (32);
int rlyWUp (31);
int rlyWDown (32);

int selRL (34);
//const int onRemote (36);


const unsigned long ACCEL_SPEEDus = 1000;  // 3.9 ms between speed increments
const unsigned long ACCEL_SPEEDus2 = 1000;
const unsigned long ACCEL_SPEEDus3 = 12000;
const unsigned long ACCEL_SPEEDus4 = 12000;
const int decelAjs = 50;

unsigned long most_recent_bump_us;
unsigned long most_recent_bump_us2;
unsigned long most_recent_bump_us3;
unsigned long most_recent_bump_us4;



int speed1;
int speed2;
int speed3;
int speed4;
int speed5;
int speed6;
int speed7;
int speed8;


int xAxis(97);  // Read Joysticks X-axis
int yAxis(96);
int zAxis(95);





void selectorMode();
void remoteMode();
void localMode();

int motorSpeedA;
int motorSpeedAarr;
int motorSpeedB;
int motorSpeedBarr;

void setup() {
  Serial.begin(9600);

  pinMode(moteurGAv, OUTPUT);   // PWM sortie moteur Gauche en Rotation Avant
  pinMode(moteurGArr, OUTPUT);  // PWM sortie moteur Gauche en Rotation Arriere
  pinMode(moteurDAv, OUTPUT);   // PWM sortie moteur Droite en Rotation Avant
  pinMode(moteurDArr, OUTPUT);  // PWM sortie moteur Droite en Rotation Arriere


  pinMode(xAxis, INPUT);

  pinMode(yAxis, INPUT);

  pinMode(zAxis, INPUT);

 pinMode(selRL,INPUT);

  pinMode(mAv, INPUT);   //bouton de marche avant Remote
  pinMode(mArr, INPUT);  //bouton de marche arriere Remote
  pinMode(mGAv, INPUT);  //bouton de virage a gauche Remote
  pinMode(mDAv, INPUT);  //bouton de virage a droite Remote

  pinMode(winchUp, INPUT);    //montée du treuil
  pinMode(winchDown, INPUT);  // descente du treuil
  pinMode(rlyWUp,OUTPUT);   // relais montée treuil
  pinMode(rlyWDown,OUTPUT);  // relais descente treuil
  
  //pinMode(selRL, INPUT);  //selecteur de poste de controle Remote / Local

  //pinMode(onRemote, INPUT); //bouton ON Remote (permission de marche)


  analogWrite(moteurGAv, 0);
  analogWrite(moteurGArr, 0);
  analogWrite(moteurDAv, 0);
  analogWrite(moteurDArr, 0);

  speed1 = 0;
  speed2 = 0;
  speed3 = 0;
  speed4 = 0;
  speed5 = 0;
  speed6 = 0;
  speed7 = 0;
  speed8 = 0;
}


void loop() {
  
  if ((digitalRead(selRL)==HIGH)||(digitalRead(selRL)==LOW))
    if (digitalRead(selRL) == HIGH)
      remoteMode();

    if (digitalRead(selRL) == LOW)
      localMode();
  else
   analogWrite(mGav,0);
   analogWrite(mGarr,0);
   analogWrite(mDav,0);
   analogWrite(mDarr,0);
   
  

  if (digitalRead(winchUp)==HIGH) 
     analogWrite(rlyWUp,HIGH);
  if (digitalRead(winchUp)==LOW) 
     analogWrite(rlyWUp,LOW);

  if (digitalRead(winchDown)==HIGH) 
     analogWrite(rlyWDown,HIGH);
  if (digitalRead(winchDown)==LOW) 
     analogWrite(rlyWDown,LOW);


  }
  void remoteMode() {
        if (digitalRead(mAv) == LOW)
          slowDownG1();
        slowDownD1();

        if ((digitalRead(mAv) == HIGH) && (speed3 == 0) && (speed4 == 0))
          speedUpG1();
        speedUpD1();


        if (digitalRead(mArr) == LOW)
          slowDown2();
        if ((digitalRead(mArr) == HIGH) && (speed1 == 0))
          speedUp2();


        if (digitalRead(mGAv) == LOW)
          slowDown3();
        if ((digitalRead(mGAv) == HIGH) && (speed3 == 0) && (speed4 == 0) && (speed1 == 0) && (speed2 == 0))
          speedUp3();


        if ((digitalRead(mDAv) == LOW) && (digitalRead(mAv) == LOW))
          slowDown4();
        if ((digitalRead(mDAv) == HIGH) && (speed3 == 0) && (speed4 == 0) && (speed1 == 0) && (speed2 == 0))
          speedUp4();


        if ((digitalRead(mAv) == LOW) && (digitalRead(mDAv) == LOW))
          slowDownD1();
        slowDownG1();
        if ((digitalRead(mAv) == HIGH) && (digitalRead(mDAv) == HIGH))
          speedUpG1();
        slowDownD1();



        if ((digitalRead(mGAv) == LOW) && (digitalRead(mAv) == LOW))
          slowDownG1();
        slowDownD1();
        if ((digitalRead(mDAv) == HIGH) && (speed3 == 0) && (speed4 == 0) && (speed1 == 0) && (speed2 == 0))
          speedUpD1();
        slowDownG1();

        delay(5);
      }


      void slowDownG1() {




        if (speed1 == 0)
          return;  // stopped
        //if(speed2 == 0) return;

        unsigned long t = micros();

        if (speed1 == 255) {
          if (digitalRead(mGAv) == HIGH)

            //start of deceleration
            most_recent_bump_us = t;
        } else if (t - most_recent_bump_us < ACCEL_SPEEDus) {
          // not time to bump the speed yet
          return;
        } else {
          // this method keeps the rate consistent
          most_recent_bump_us += ACCEL_SPEEDus;
        }


        moteurGAv = --speed1;
      }

      void speedUpG1() {
        if (speed1 == 255) return;  // max speed

        unsigned long t = micros();

        if (speed1 == 0) {
          // start of acceleration
          most_recent_bump_us = t;
        } else if (t - most_recent_bump_us < ACCEL_SPEEDus) {
          // not time to bump the speed yet
          return;
        } else {
          // this method keeps the rate consistent
          most_recent_bump_us += ACCEL_SPEEDus;
        }


        moteurGAv = ++speed1;
      }



      void slowDownD1() {
        if ((speed3 == 0) || (speed4 == 0))
          return;  // stopped
        //if(speed2 == 0) return;

        unsigned long t = micros();

        if (speed2 == 255) {
          //start of deceleration
          most_recent_bump_us2 = t;
        } else if (t - most_recent_bump_us2 < ACCEL_SPEEDus2) {
          // not time to bump the speed yet
          return;
        } else {
          // this method keeps the rate consistent
          most_recent_bump_us2 += ACCEL_SPEEDus2;
        }





        moteurDArr = --speed2;
      }


      void speedUpD1() {
        if (speed2 == 255) return;  // max speed

        unsigned long t = micros();

        if (speed2 == 0) {
          // start of acceleration
          most_recent_bump_us = t;
        } else if (t - most_recent_bump_us < ACCEL_SPEEDus) {
          // not time to bump the speed yet
          return;
        } else {
          // this method keeps the rate consistent
          most_recent_bump_us += ACCEL_SPEEDus;
        }






        moteurDAv = ++speed2;
      }



      void slowDown2() {
        if ((speed3 == 0) || (speed4 == 0))
          return;  // stopped
        //if(speed2 == 0) return;

        unsigned long t = micros();

        if ((speed3 == 255) || (speed4 == 255)) {
          //start of deceleration
          most_recent_bump_us2 = t;
        } else if (t - most_recent_bump_us2 < ACCEL_SPEEDus2) {
          // not time to bump the speed yet
          return;
        } else {
          // this method keeps the rate consistent
          most_recent_bump_us2 += ACCEL_SPEEDus2;
        }


        moteurGArr = --speed3;


        moteurDArr = --speed4;
      }

      void speedUp2() {
        if ((speed3 == 255) || (speed4 == 255)) return;  // max speed

        unsigned long t = micros();

        if ((speed3 == 0) || (speed4 == 0)) {
          // start of acceleration
          most_recent_bump_us2 = t;
        } else if (t - most_recent_bump_us2 < ACCEL_SPEEDus2) {
          // not time to bump the speed yet
          return;
        } else {
          // this method keeps the rate consistent
          most_recent_bump_us2 += ACCEL_SPEEDus2;
        }


        moteurGArr = ++speed3;



        moteurDArr = ++speed4;
      }

      void slowDown3() {  //virage a gauche
        if ((speed2 == 0) || (speed3 == 0))
          return;  // stopped
        //if(speed2 == 0) return;

        unsigned long t = micros();

        if ((speed2 == 100) || (speed3 == 100)) {
          //start of deceleration
          most_recent_bump_us3 = (t / decelAjs);
        } else if (t - most_recent_bump_us3 < ACCEL_SPEEDus3) {
          // not time to bump the speed yet
          return;
        } else {
          // this method keeps the rate consistent
          most_recent_bump_us3 += ACCEL_SPEEDus3;
        }


        moteurGArr = --speed3;


        moteurDAv = --speed2;
      }

      void speedUp3() {
        if ((speed2 == 100) || (speed3 == 100)) return;  // max speed

        unsigned long t = micros();

        if ((speed2 == 0) || (speed3 == 0)) {
          // start of acceleration
          most_recent_bump_us3 = t;
        } else if (t - most_recent_bump_us3 < ACCEL_SPEEDus3) {
          // not time to bump the speed yet
          return;
        } else {
          // this method keeps the rate consistent
          most_recent_bump_us3 += ACCEL_SPEEDus3;
        }






        moteurDAv = ++speed2;
        moteurGArr = ++speed3;
      }




      void slowDown4() {  //virage droite
        if ((speed1 == 0) || (speed4 == 4))
          return;  // stopped
        //if(speed2 == 0) return;

        unsigned long t = micros();

        if ((speed1 == 100) || (speed4 == 100)) {
          //start of deceleration
          most_recent_bump_us4 = (t / decelAjs);
        } else if (t - most_recent_bump_us4 < ACCEL_SPEEDus4) {
          // not time to bump the speed yet
          return;
        } else {
          // this method keeps the rate consistent
          most_recent_bump_us4 += ACCEL_SPEEDus4;
        }


        moteurGAv = --speed1;


        moteurDArr = --speed4;
      }

      void speedUp4() {
        if ((speed1 == 150) || (speed4 == 150)) return;  // max speed

        unsigned long t = micros();

        if ((speed1 == 0) || (speed4 == 0)) {
          // start of acceleration
          most_recent_bump_us4 = t;
        } else if (t - most_recent_bump_us4 < ACCEL_SPEEDus4) {
          // not time to bump the speed yet
          return;
        } else {
          // this method keeps the rate consistent
          most_recent_bump_us4 += ACCEL_SPEEDus4;
        }






        moteurDArr = ++speed4;
        moteurGAv = ++speed1;


        analogWrite(mDarr, moteurDArr);
        analogWrite(mGarr, moteurGArr);
        analogWrite(mDav, moteurDAv);
        analogWrite(mGav, moteurGAv);
  }


  void localMode() {



    digitalRead(xAxis);
    // Read Joysticks Y-axis
    digitalRead(yAxis);

    digitalRead(zAxis);








    // z-axis used for fast left turn control
    if (zAxis < (470 - deadbandZ)) {

      // Set Motor A backward



      // Set Motor B Forward


      // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed

      motorSpeedAarr = map(zAxis, 470, 0, 0, 150);

      motorSpeedB = map(zAxis, 470, 0, 0, 150);
    }
       // FAST RIGHT TURN
    else if (zAxis > (550 + deadbandZ)) {

      // Set Motor A forward


      // Set Motor B BACKWARD


      // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed

      motorSpeedA = map(zAxis, (550 + deadbandZ), 1023, 0, 150);

      motorSpeedBarr = map(zAxis, (550 + deadbandZ), 1023, 0, 150);
    }




    if (((470 - deadbandZ) < zAxis) && (zAxis < (550 + deadbandZ)))

      if (yAxis < (470 - deadbandY)) {

        // Set Motor A backward



        // Set Motor B backward


        // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed

        motorSpeedAarr = map(yAxis, (470 - deadbandY), 0, 0, 255);

        motorSpeedBarr = map(yAxis, (470 - deadbandY), 0, 0, 255);

         
      }

      if (yAxis > (550 + deadbandY)) {

      // Set Motor A forward


      // Set Motor B forward


      // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed

        motorSpeedA = map(yAxis, (550 + deadbandY), 1023, 0, 255);

        motorSpeedB = map(yAxis, (550 + deadbandY), 1023, 0, 255);


      }

    // If joystick stays in middle the motors are not moving

    else {

     motorSpeedA = 0;
     motorSpeedAarr = 0;


      motorSpeedB = 0;
      motorSpeedBarr = 0;
    }



        // X-axis used for left and right control
        // RIGHT TURN WHILE FORWARD

        if ((xAxis < (470 - deadbandX)) && (yAxis< (470-deadbandY))) {

          // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value

          motorSpeedAarr = map(xAxis, (470 - deadbandX), 0, 0, 255);



          // Confine the range from 0 to 255

          if (motorSpeedA < 1) {

            motorSpeedA = 0;
          }

          if (motorSpeedB > 255) {

            motorSpeedB = 255;
          }
        }

        if ((xAxis < (470 - deadbandX)) && (yAxis >= (470-deadbandY))) {

            // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value

            motorSpeedA = map(xAxis, (470 - deadbandX), 0, 0, 255);



            // Confine the range from 0 to 255

            if (motorSpeedA < 1) {

              motorSpeedA = 0;
            }

            if (motorSpeedA > 255) {

              motorSpeedA = 255;
            }
          }

        if (xAxis > (550 + deadbandX)) {

          // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value

          motorSpeedBarr = map(xAxis, (550 + deadbandX), 1023, 0, 255);

          // Move right - decrease right motor speed, increase left motor speed


          // Confine the range from 0 to 255

          if (motorSpeedA > 255) {

            motorSpeedA = 255;
          }

          if (motorSpeedB < 1) {

            motorSpeedB = 0;
          }
        }

        // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)

        if (motorSpeedA < 1) {

          motorSpeedA = 0;
        }

        if (motorSpeedB < 1) {

          motorSpeedB = 0;
        }

    analogWrite(mGav, motorSpeedA);  // Send PWM signal to motor A
    analogWrite(mDarr, motorSpeedAarr);


    analogWrite(mDav, motorSpeedB);  // Send PWM signal to motor B
    analogWrite(mDarr, motorSpeedBarr);

    delay(10);
  
   
}

Not working yet.

In C indentation does not produce code blocks, that's what curly braces are for.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.