Combining Arduino Codes for school project

Hi,
I am working on a school project. I need guidance for combining these two codes
Code 1

char t;
int left1 = 10;
int left2 = 11;
int right1 = 12;
int right2 = 13; 
int enA = 5;
int enB = 6;

void setup() {
  pinMode(left1, OUTPUT);
  pinMode(left2, OUTPUT);
  pinMode(right1, OUTPUT);
  pinMode(right2, OUTPUT);
  Serial.begin(9600);

   analogWrite(enA, 255);
  analogWrite(enB, 255);
  
//  digitalWrite(enA, 0);
  digitalWrite(left1, LOW);
  digitalWrite(left2, LOW);
  
  //digitalWrite(enB, 0);
  digitalWrite(right1, LOW);
  digitalWrite(right2, LOW);
}
 
void loop()
{
  while (Serial.available() > 0)
  {
  t = Serial.read();
  Serial.println(t);
  }
 
if(t == '1'){            //forward
//  analogWrite(enA, 200);
  //analogWrite(enB, 200);
  
  digitalWrite(left1, HIGH);
  digitalWrite(left2, LOW);
  
  digitalWrite(right1, HIGH);
  digitalWrite(right2, LOW);

  delay(300);
  digitalWrite(left1, LOW);
  digitalWrite(left2, LOW);
  
  digitalWrite(right1, LOW);
  digitalWrite(right2, LOW);
  t = '5';
  
}
 
else if(t == '2'){      //reverse
  digitalWrite(left1, LOW);
  digitalWrite(left2, HIGH);

  digitalWrite(right1, LOW);
  digitalWrite(right2, HIGH);

  delay(300);
  digitalWrite(left1, LOW);
  digitalWrite(left2, LOW);
  
  digitalWrite(right1, LOW);
  digitalWrite(right2, LOW);
  t = '5';
}
 
else if(t == '3'){      //right
  digitalWrite(left1, LOW);
  digitalWrite(left2, LOW);

  digitalWrite(right1, HIGH);
  digitalWrite(right2, LOW);

  delay(300);
  digitalWrite(left1, LOW);
  digitalWrite(left2, LOW);
  
  digitalWrite(right1, LOW);
  digitalWrite(right2, LOW);
  t = '5';
}
 
else if(t == '4'){      //left
  digitalWrite(left1, HIGH);
  digitalWrite(left2, LOW);

  digitalWrite(right1, LOW);
  digitalWrite(right2, LOW);

  delay(300);
  digitalWrite(left1, LOW);
  digitalWrite(left2, LOW);
  
  digitalWrite(right1, LOW);
  digitalWrite(right2, LOW);
  t = '5';
}
 
else if(t == '5'){      //STOP 
  digitalWrite(left1, LOW);
  digitalWrite(left2, LOW);

  digitalWrite(right1, LOW);
  digitalWrite(right2, LOW);
}
//delay(100);
}

Code 2:

// Motor A
 
int enA = 5;
int in1 = 13;
int in2 = 12;
 
// Motor B
 
int enB = 6;
int in3 = 11;
int in4 = 10;
 
// Joystick Input
 
int joyVert = A0; // Vertical  
int joyHorz = A1; // Horizontal
 
// Motor Speed Values - Start at zero
 
int MotorSpeed1 = 0;
int MotorSpeed2 = 0;
 
// Joystick Values - Start at 512 (middle position)
 
int joyposVert = 512;
int joyposHorz = 512;  
 
 
void setup()
 
{
 
  // Set all the motor control pins to outputs
 
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
   
  // Start with motors disabled and direction forward
  
  // Motor A
  
  /*digitalWrite(enA, LOW);
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  
  // Motor B
  
  digitalWrite(enB, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  */
}
 
void loop() {
  joytstick();
  // Read the Joystick X and Y positions
}
void joytstick() {
  joyposVert = analogRead(joyVert); 
  joyposHorz = analogRead(joyHorz);
 
  // Determine if this is a forward or backward motion
  // Do this by reading the Verticle Value
  // Apply results to MotorSpeed and to Direction
 
  if (joyposVert < 460)
  {
    // This is Backward
 
    // Set Motor A backward
 
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
 
    // Set Motor B backward
 
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
 
    //Determine Motor Speeds
 
    // As we are going backwards we need to reverse readings
 
    joyposVert = joyposVert - 460; // This produces a negative number
    joyposVert = joyposVert * -1;  // Make the number positive
 
    MotorSpeed1 = map(joyposVert, 0, 460, 0, 255);
    MotorSpeed2 = map(joyposVert, 0, 460, 0, 255);
 
  }
  else if (joyposVert > 564)
  {
    // This is Forward
 
    // Set Motor A forward
 
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
 
    // Set Motor B forward
 
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
 
    //Determine Motor Speeds
 
    MotorSpeed1 = map(joyposVert, 564, 1023, 0, 255);
    MotorSpeed2 = map(joyposVert, 564, 1023, 0, 255); 
 
  }
  else
  {
    // This is Stopped
 
    MotorSpeed1 = 0;
    MotorSpeed2 = 0; 
 
  }
  
  // Now do the steering
  // The Horizontal position will "weigh" the motor speed
  // Values for each motor
 
  if (joyposHorz < 460)
  {
    // Move Left
 
    // As we are going left we need to reverse readings
 
    joyposHorz = joyposHorz - 460; // This produces a negative number
    joyposHorz = joyposHorz * -1;  // Make the number positive
 
    // Map the number to a value of 255 maximum
 
    joyposHorz = map(joyposHorz, 0, 460, 0, 255);
        
 
    MotorSpeed1 = MotorSpeed1 - joyposHorz;
    MotorSpeed2 = MotorSpeed2 + joyposHorz;
 
    // Don't exceed range of 0-255 for motor speeds
 
    if (MotorSpeed1 < 0)MotorSpeed1 = 0;
    if (MotorSpeed2 > 255)MotorSpeed2 = 255;
 
  }
  else if (joyposHorz > 564)
  {
    // Move Right
 
    // Map the number to a value of 255 maximum
 
    joyposHorz = map(joyposHorz, 564, 1023, 0, 255);
        
 
    MotorSpeed1 = MotorSpeed1 + joyposHorz;
    MotorSpeed2 = MotorSpeed2 - joyposHorz;
 
    // Don't exceed range of 0-255 for motor speeds
 
    if (MotorSpeed1 > 255)MotorSpeed1 = 255;
    if (MotorSpeed2 < 0)MotorSpeed2 = 0;      
 
  }
 
 
  // Adjust to prevent "buzzing" at very low speed
 
  if (MotorSpeed1 < 8)MotorSpeed1 = 0;
  if (MotorSpeed2 < 8)MotorSpeed2 = 0;
 
  // Set the motor speeds
 
  analogWrite(enA, MotorSpeed1);
  analogWrite(enB, MotorSpeed2);
 
}

Do you want someone to do it for you or are you going to post your best efforts at combining those codes and have others help you? I'm sure if you wait around long enough someone, like gcjr, will just do it for you but what did you learn?

In a first approach you can rename the setup() and loop() functions of both modules and write a new program containing

void setup() {
  setup1();
  setup2();
}
void loop() {
  loop1();
  loop2();
}

Then fix all errors with the combined code.

1 Like

when i see threads asking how to combine two different "codes", it seems there's a fundamental lack of understanding of how to write programs using sub-functions and if the OP actually understands them

i doubt creating a setup1/2() and loop1/2() would work in this case considering that there are pins in common between the two

in this case, it looks like both programs control a motor. Serial monitor input is used as commands in one and a joystick in the other. there's a lot of overlap between the two

not sure it makes sense to combine them -- wouldn't the joystick override any serial commands?

Hi, @muneeb2006ch
Welcome to the forum.
Thanks for using code tags , it makes reading code so much better.

Can you tell us what each code does and what each circuit is that goes with the code?
What do you want the combined code to do?

Tom... :smiley: :+1: :coffee: :australia:

@TomGeorge The first code uses hc 06 module to connect to phone. It is used for wireless control, while the second code uses wired joystick to control it.

I dont think so that joystick will overide the srial monitor. beside that i will use a switch that will switch power bw bluetooth module and joystick

I am not asking for code, I wan to learn how can u combine them

See post 3 on one way to combine the code.

1 Like

there are routines to control the motor in both. you should have a single set of routines, perhaps just one with arguments for direction and speed that can be invoked when using the joystick or serial commands.

does this make sense to you?

Hi,
Did you write these two codes?
Do you know how the codes work?

Can you please tell us your electronics, programming, arduino, hardware experience?

Tom..... :smiley: :+1: :coffee:

Hi, The first code is written by me. But the second one was taken from internet because I am short on time for submission of project.

I have made many projects like line following car, smartphone controlled car via Bluetooth, vocal command following car and some related ones.
I also made a prototype for smart glasses, its design was not successful.

Can u give an example?

If you do a first guessing how combining the codes might work the other users can see what your actual level of programming-knowledge is.
This enables to adapt the explanations to your knowledge-level.
It doesn't help much if somebody write to you

"take out setup() and loop() in one of the two codes and then integrate the rest of the code accordingly"

that would be a bit short for a beginner

Well if you have done multiple projects and still yet don't know what the real purpose of setup() and loop() is ....
How many days is beeing "short on time"?

best regards Stefan

Well I tried it but it says loop2 not identified...

int enA = 5;
int right2 = 13;
int right1 = 12;

// Motor B

int enB = 6;
int left2 = 11;
int left1 = 10;

// Joystick Input
int joyVert = A0; // Vertical
int joyHorz = A1; // Horizontal

// Motor Speed Values - Start at zero

int MotorSpeed1 = 0;
int MotorSpeed2 = 0;

// Joystick Values - Start at 512 (middle position)

int joyposVert = 512;
int joyposHorz = 512;


char t;
/*int left1 = 10;
int left2 = 11;
int right1 = 12;
int right2 = 13;
int enA = 5;
int enB = 6;  */
void setup() {
  setup1();
  setup2();
}
void loop() {
  loop1();
  loop2();
}
void setup1() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(right2, OUTPUT);
  pinMode(right1, OUTPUT);
  pinMode(left2, OUTPUT);
  pinMode(left1, OUTPUT);
}
void setup2() {
  pinMode(left1, OUTPUT);
  pinMode(left2, OUTPUT);
  pinMode(right1, OUTPUT);
  pinMode(right2, OUTPUT);
  Serial.begin(9600);

  analogWrite(enA, 255);
  analogWrite(enB, 255);

  //  digitalWrite(enA, 0);
  digitalWrite(left1, LOW);
  digitalWrite(left2, LOW);

  //digitalWrite(enB, 0);
  digitalWrite(right1, LOW);
  digitalWrite(right2, LOW);
}
void loop1() {
  joyposVert = analogRead(joyVert);
  joyposHorz = analogRead(joyHorz);

  // Determine if this is a forward or backward motion
  // Do this by reading the Verticle Value
  // Apply results to MotorSpeed and to Direction

  if (joyposVert < 460)
  {
    // This is Backward

    // Set Motor A backward

    digitalWrite(right2, LOW);
    digitalWrite(right1, HIGH);

    // Set Motor B backward

    digitalWrite(left2, LOW);
    digitalWrite(left1, HIGH);

    //Determine Motor Speeds

    // As we are going backwards we need to reverse readings

    joyposVert = joyposVert - 460; // This produces a negative number
    joyposVert = joyposVert * -1;  // Make the number positive

    MotorSpeed1 = map(joyposVert, 0, 460, 0, 255);
    MotorSpeed2 = map(joyposVert, 0, 460, 0, 255);

  }
  else if (joyposVert > 564)
  {
    // This is Forward

    // Set Motor A forward

    digitalWrite(right2, HIGH);
    digitalWrite(right1, LOW);

    // Set Motor B forward

    digitalWrite(left2, HIGH);
    digitalWrite(left1, LOW);

    //Determine Motor Speeds

    MotorSpeed1 = map(joyposVert, 564, 1023, 0, 255);
    MotorSpeed2 = map(joyposVert, 564, 1023, 0, 255);

  }
  else
  {
    // This is Stopped

    MotorSpeed1 = 0;
    MotorSpeed2 = 0;

  }

  // Now do the steering
  // The Horizontal position will "weigh" the motor speed
  // Values for each motor

  if (joyposHorz < 460)
  {
    // Move Left

    // As we are going left we need to reverse readings

    joyposHorz = joyposHorz - 460; // This produces a negative number
    joyposHorz = joyposHorz * -1;  // Make the number positive

    // Map the number to a value of 255 maximum

    joyposHorz = map(joyposHorz, 0, 460, 0, 255);


    MotorSpeed1 = MotorSpeed1 - joyposHorz;
    MotorSpeed2 = MotorSpeed2 + joyposHorz;

    // Don't exceed range of 0-255 for motor speeds

    if (MotorSpeed1 < 0)MotorSpeed1 = 0;
    if (MotorSpeed2 > 255)MotorSpeed2 = 255;

  }
  else if (joyposHorz > 564)
  {
    // Move Right

    // Map the number to a value of 255 maximum

    joyposHorz = map(joyposHorz, 564, 1023, 0, 255);


    MotorSpeed1 = MotorSpeed1 + joyposHorz;
    MotorSpeed2 = MotorSpeed2 - joyposHorz;

    // Don't exceed range of 0-255 for motor speeds

    if (MotorSpeed1 > 255)MotorSpeed1 = 255;
    if (MotorSpeed2 < 0)MotorSpeed2 = 0;

  }


  // Adjust to prevent "buzzing" at very low speed

  if (MotorSpeed1 < 8)MotorSpeed1 = 0;
  if (MotorSpeed2 < 8)MotorSpeed2 = 0;

  // Set the motor speeds

  analogWrite(enA, MotorSpeed1);
  analogWrite(enB, MotorSpeed2);

}
}
void loop2() {
  while (Serial.available() > 0)
  {
    t = Serial.read();
    Serial.println(t);
  }

  if (t == '1') {          //forward
    //  analogWrite(enA, 200);
    //analogWrite(enB, 200);

    digitalWrite(left1, HIGH);
    digitalWrite(left2, LOW);

    digitalWrite(right1, HIGH);
    digitalWrite(right2, LOW);

    delay(300);
    digitalWrite(left1, LOW);
    digitalWrite(left2, LOW);

    digitalWrite(right1, LOW);
    digitalWrite(right2, LOW);
    t = '5';

  }

  else if (t == '2') {    //reverse
    digitalWrite(left1, LOW);
    digitalWrite(left2, HIGH);

    digitalWrite(right1, LOW);
    digitalWrite(right2, HIGH);

    delay(300);
    digitalWrite(left1, LOW);
    digitalWrite(left2, LOW);

    digitalWrite(right1, LOW);
    digitalWrite(right2, LOW);
    t = '5';
  }

  else if (t == '3') {    //right
    digitalWrite(left1, LOW);
    digitalWrite(left2, LOW);

    digitalWrite(right1, HIGH);
    digitalWrite(right2, LOW);

    delay(300);
    digitalWrite(left1, LOW);
    digitalWrite(left2, LOW);

    digitalWrite(right1, LOW);
    digitalWrite(right2, LOW);
    t = '5';
  }

  else if (t == '4') {    //left
    digitalWrite(left1, HIGH);
    digitalWrite(left2, LOW);

    digitalWrite(right1, LOW);
    digitalWrite(right2, LOW);

    delay(300);
    digitalWrite(left1, LOW);
    digitalWrite(left2, LOW);

    digitalWrite(right1, LOW);
    digitalWrite(right2, LOW);
    t = '5';
  }

  else if (t == '5') {    //STOP
    digitalWrite(left1, LOW);
    digitalWrite(left2, LOW);

    digitalWrite(right1, LOW);
    digitalWrite(right2, LOW);
  }
}

?

I have a week, I am new in this field, last projects were assisted by my dad, He is a hobbyist, but this time its not possible.
Basically I am not a computer science student so debugging is a hard challenge.

describe in your own words:

what is void setup() used for?

what is void loop() used for?

Is there a way I can connect two Arduino's to l298n motor driver, If yes then the problem will be resolved?

Void setup is the code that runs only once when powered on.
while void loop is the part of code that runs repeatedly.