# Combine two Arduino codes

**URL:** <https://forum.arduino.cc/t/combine-two-arduino-codes/859554>\
**Category:** Programming\
**Created:** [May 9, 2021, 10:03am UTC](https://forum.arduino.cc/t/combine-two-arduino-codes/859554 "2021-05-09T10:03:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![cicerone96](https://avatars.discourse-cdn.com/v4/letter/c/ee59a6/32.png) [@cicerone96](https://forum.arduino.cc/u/cicerone96)\
**Post date:** [May 9, 2021, 10:03am UTC](https://forum.arduino.cc/t/combine-two-arduino-codes/859554/1 "2021-05-09T10:03:00Z")

</div>

Hello,

I'm having issues when combining 2 Arduino codes.  
They work independently, but i can't figure out a way to combine them.

Code 1 : servo-motor control via Android app  
Code 2 : Dc motor control, H bridge via Android app (joystick button)

I combined the Android apps (sliders for the servos and Joystick button for the DC motors)  
The problem I have is:  
When i use the slider (to control servo) the DC motor start running. (and i don't want that).

I can't figure out how to combine them.

Code1:

```cpp
#include <Servo.h> // servo library 
Servo myservo1, myservo2, myservo3, myservo4; // servo name

void setup()
{
  myservo1.attach(3); // attach servo signal wire to pin 9
  myservo2.attach(5);
  myservo3.attach(6);
  myservo4.attach(9);
  //Setup usb serial connection to computer
  Serial.begin(9600);
}

void loop()
{
  //Read from Serial and write to usb serial
  if(Serial.available()>= 2 )
  {
    unsigned int servopos = Serial.read();
    unsigned int servopos1 = Serial.read();
    unsigned int realservo = (servopos1 *256) + servopos;
    Serial.println(realservo);

    if (realservo >= 1000 && realservo <1180) {
      int servo1 = realservo;
      servo1 = map(servo1, 1000, 1180, 0, 180);
      myservo1.write(servo1);
      Serial.println("Servo 1 ON");
      delay(10);
    }
    if (realservo >= 2000 && realservo <2180) {
      int servo2 = realservo;
      servo2 = map(servo2, 2000, 2180, 0, 180);
      myservo2.write(servo2);
      Serial.println("Servo 2 ON");
      delay(10);
    }
    if (realservo >= 3000 && realservo <3180) {
      int servo3 = realservo;
      servo3 = map(servo3, 3000, 3180, 0, 180);
      myservo3.write(servo3);
      Serial.println("Servo 3 ON");
      delay(10);
    }
    if (realservo >= 4000 && realservo <4180) {
      int servo4 = realservo;
      servo4 = map(servo4, 4000, 4180, 0, 180);
      myservo4.write(servo4);
      Serial.println("Servo 4 ON");
      delay(10);
    }
  }
}

```

Code2:

```cpp

#define enA 10
#define in1 4
#define in2 5

#define in3 6
#define in4 7
#define enB 9

int xAxis=140, yAxis=140;

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);

Serial.begin(9600); // Default communication rate of the Bluetooth module

}

void loop() {
  // Default value - no movement when the Joystick stays in the center
  //xAxis = 140;
  //yAxis = 140;

  // Read the incoming data from the Smartphone Android App
  while (Serial.available() >= 2) {
    xAxis = Serial.read();
    delay(10);
    yAxis = Serial.read();
   Serial.print(xAxis);
   Serial.print(",");
   Serial.println(yAxis);
  }
  delay(10);
  
  // Makes sure we receive corrent values

if (xAxis > 130 && xAxis <150 && yAxis > 130 && yAxis <150){Stop();}

if (yAxis > 130 && yAxis <150){    

if (xAxis < 130){turnRight();
motorSpeedA = map(xAxis, 130, 60, 0, 255);
motorSpeedB = map(xAxis, 130, 60, 0, 255);    
}

if (xAxis > 150) {turnLeft();
motorSpeedA = map(xAxis, 150, 220, 0, 255);
motorSpeedB = map(xAxis, 150, 220, 0, 255); 
}

}else{

if (xAxis > 130 && xAxis <150){   

if (yAxis < 130){forword();}
if (yAxis > 150){backword();}

if (yAxis < 130){
motorSpeedA = map(yAxis, 130, 60, 0, 255);
motorSpeedB = map(yAxis, 130, 60, 0, 255); 
}

if (yAxis > 150){
motorSpeedA = map(yAxis, 150, 220, 0, 255);
motorSpeedB = map(yAxis, 150, 220, 0, 255);
 }
 
}else{

if (yAxis < 130){forword();}
if (yAxis > 150){backword();}

if (xAxis < 130){
motorSpeedA = map(xAxis, 130, 60, 255, 50);
motorSpeedB = 255; 
 }
 
if (xAxis > 150){
motorSpeedA = 255;
motorSpeedB = map(xAxis, 150, 220, 255, 50); 
  }
  
 } 
}

   //Serial.print(motorSpeedA);
   //Serial.print(",");
   //Serial.println(motorSpeedA);

analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}

void forword(){Serial.println("forword");
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW); 
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}

void backword(){Serial.println("backword");
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH); 
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}

void turnRight(){Serial.println("turnRight");
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW); 
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}

void turnLeft(){Serial.println("turnLeft");
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH); 
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}

void Stop(){
digitalWrite(in1, LOW);
digitalWrite(in2, LOW); 
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
Serial.println("stop");
}

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 9, 2021, 10:42am UTC](https://forum.arduino.cc/t/combine-two-arduino-codes/859554/2 "2021-05-09T10:42:37Z")

</div>

I don't know what you have tried or what you want the combined code to do but I see an immediate problem

When you use the Servo attach() function it prevents pins 9 and 10 being used as PWM outputs. See [Servo - attach() - Arduino Reference](https://www.arduino.cc/reference/en/libraries/servo/attach/) and in the second sketch you use pin 10 as a PWM output

Start by describing what you want the combined sketch to do, post your best effort at combining the 2 sketches, post any error messages that you get when compiling the combined sketch and if it does compile describe what it does/does not do

---

<div class="post-metadata">

**Author:** ![cicerone96](https://avatars.discourse-cdn.com/v4/letter/c/ee59a6/32.png) [@cicerone96](https://forum.arduino.cc/u/cicerone96)\
**Post date:** [May 9, 2021, 1:35pm UTC](https://forum.arduino.cc/t/combine-two-arduino-codes/859554/3 "2021-05-09T13:35:28Z")

</div>

Yes, i'm aware that the pins are not correct.  
I managed to combine and compile the sketch sucessfully. No errors.  
But the code doesn't do what I need it to.

So the first code, shold only affect the movement of servos.  
The second code just the dc motors.

I have an app. On the top side of the app, i have a joysyick button (app created in app inventor) and on the bottom side i have sliders for the servos.

My problem is, when i move the slider(in android app---to change the servo position) the servo moves where i want it, but the dc motors also start rotating(and that is my issue).

I want the dc motors to rotate only when i'm moving the joystick in app(in code functions are : forward, backward...)

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 9, 2021, 1:37pm UTC](https://forum.arduino.cc/t/combine-two-arduino-codes/859554/4 "2021-05-09T13:37:36Z")

</div>

We cannot see your code. Please post it

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [May 9, 2021, 1:50pm UTC](https://forum.arduino.cc/t/combine-two-arduino-codes/859554/5 "2021-05-09T13:50:22Z")

</div>

Hello  
as best you take a look into the WWW and you will find a lot of tutorial to do this job.

---

<div class="post-metadata">

**Author:** ![slipstick](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@slipstick](https://forum.arduino.cc/u/slipstick)\
**Post date:** [May 9, 2021, 2:10pm UTC](https://forum.arduino.cc/t/combine-two-arduino-codes/859554/6 "2021-05-09T14:10:14Z")

</div>

> [@cicerone96](#):
>
> My problem is, when i move the slider(in android app—to change the servo position) the servo moves where i want it, but the dc motors also start rotating(and that is my issue).

O.k. that describes the problem. Now post the combined code you have that does what you're describing.

Steve

---

<div class="post-metadata">

**Author:** ![cicerone96](https://avatars.discourse-cdn.com/v4/letter/c/ee59a6/32.png) [@cicerone96](https://forum.arduino.cc/u/cicerone96)\
**Post date:** [May 9, 2021, 3:45pm UTC](https://forum.arduino.cc/t/combine-two-arduino-codes/859554/7 "2021-05-09T15:45:27Z")

</div>

OK. So this code does the following:  
1.When slider from app is moved - \> servomotor moves(this is OK)  
2. The DC motors start rotating -without touching the Joystick button in app (this is my issue)

 ![Screenshot_20210509-184317](https://europe1.discourse-cdn.com/arduino/original/4X/7/9/0/790c5b4636288f222f2b18e82b03a84b55ffaaa0.jpeg)

```cpp
#include <Servo.h> // servo library 
Servo myservo1, myservo2, myservo3, myservo4; // servo name
#define enA 10
#define in1 4
#define in2 5

#define in3 6
#define in4 7
#define enB 9

int xAxis=140, yAxis=140;

int motorSpeedA = 0;
int motorSpeedB = 0;

void setup()
{
  myservo1.attach(2); 
  myservo2.attach(3);
  myservo3.attach(8);
  myservo4.attach(11);

pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);

Serial.begin(9600); // Default communication rate of the Bluetooth module

}

void loop()
{
  //Read from Serial and write to usb serial
  if(Serial.available()>= 2 )
  {
     xAxis = Serial.read();
    delay(10);
     yAxis = Serial.read();
   Serial.print(xAxis);
   Serial.print(",");
   Serial.println(yAxis);
     unsigned int servopos = Serial.read();
    unsigned int servopos1 = Serial.read();
    unsigned int realservo = (servopos1 *256) + servopos;
    Serial.println(realservo);

    if (realservo >= 1000 && realservo <1180) {
      int servo1 = realservo;
      servo1 = map(servo1, 1000, 1180, 0, 180);
      myservo1.write(servo1);
      Serial.println("Servo 1 ON");
      delay(10);
    }
    if (realservo >= 2000 && realservo <2180) {
      int servo2 = realservo;
      servo2 = map(servo2, 2000, 2180, 0, 180);
      myservo2.write(servo2);
      Serial.println("Servo 2 ON");
      delay(10);
    }
    if (realservo >= 3000 && realservo <3180) {
      int servo3 = realservo;
      servo3 = map(servo3, 3000, 3180, 0, 180);
      myservo3.write(servo3);
      Serial.println("Servo 3 ON");
      delay(10);
    }
    if (realservo >= 4000 && realservo <4180) {
      int servo4 = realservo;
      servo4 = map(servo4, 4000, 4180, 0, 180);
      myservo4.write(servo4);
      Serial.println("Servo 4 ON");
      delay(10);
    }
    else
    {
      if (xAxis > 130 && xAxis <150 && yAxis > 130 && yAxis <150){Stop();}
    }
  }
  delay(10);

if (yAxis > 130 && yAxis <150){    

if (xAxis < 130){turnRight();
motorSpeedA = map(xAxis, 130, 60, 0, 255);
motorSpeedB = map(xAxis, 130, 60, 0, 255);    
}

if (xAxis > 150) {turnLeft();
motorSpeedA = map(xAxis, 150, 220, 0, 255);
motorSpeedB = map(xAxis, 150, 220, 0, 255); 
}

}else{

if (xAxis > 130 && xAxis <150){   

if (yAxis < 130){forword();}
if (yAxis > 150){backword();}

if (yAxis < 130){
motorSpeedA = map(yAxis, 130, 60, 0, 255);
motorSpeedB = map(yAxis, 130, 60, 0, 255); 
}

if (yAxis > 150){
motorSpeedA = map(yAxis, 150, 220, 0, 255);
motorSpeedB = map(yAxis, 150, 220, 0, 255);
 }
 
}else{

if (yAxis < 130){forword();}
if (yAxis > 150){backword();}

if (xAxis < 130){
motorSpeedA = map(xAxis, 130, 60, 255, 50);
motorSpeedB = 255; 
 }
 
if (xAxis > 150){
motorSpeedA = 255;
motorSpeedB = map(xAxis, 150, 220, 255, 50); 
  }
  
 } 
}

   //Serial.print(motorSpeedA);
   //Serial.print(",");
   //Serial.println(motorSpeedA);

analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}

void forword(){Serial.println("forword");
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW); 
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}

void backword(){Serial.println("backword");
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH); 
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}

void turnRight(){Serial.println("turnRight");
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW); 
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}

void turnLeft(){Serial.println("turnLeft");
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH); 
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}

void Stop(){
digitalWrite(in1, LOW);
digitalWrite(in2, LOW); 
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
Serial.println("stop");
}

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 9, 2021, 3:59pm UTC](https://forum.arduino.cc/t/combine-two-arduino-codes/859554/8 "2021-05-09T15:59:39Z")

</div>

Your code checks to see whether there are at least 2 bytes available in the Serial buffer then reads 4 bytes. What if only 2 are available ?

Take a look at [Serial input basics - updated](http://forum.arduino.cc/index.php?topic=396450) for a smater way to read and parse Serial data

---

<div class="post-metadata">

**Author:** ![slipstick](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@slipstick](https://forum.arduino.cc/u/slipstick)\
**Post date:** [May 9, 2021, 4:03pm UTC](https://forum.arduino.cc/t/combine-two-arduino-codes/859554/9 "2021-05-09T16:03:28Z")

</div>

Did you read post #2? You're trying to do analogWrites to pins 9 and 10 which are disabled when you use Servo.h. You said you knew the pins were wrong but you didn't bother changing them. What's the point of trying to help?

Steve

---

<div class="post-metadata">

**Author:** ![cicerone96](https://avatars.discourse-cdn.com/v4/letter/c/ee59a6/32.png) [@cicerone96](https://forum.arduino.cc/u/cicerone96)\
**Post date:** [May 9, 2021, 5:11pm UTC](https://forum.arduino.cc/t/combine-two-arduino-codes/859554/10 "2021-05-09T17:11:30Z")

</div>

I was not @home, had the code on onedrive. That's why I didn't update it. Sorry

@UKHeliBob , i am too much of a newb to know how to have it another way.  
I thank you for the references you provided

Is there anything that i can do, to this code, apart from the PWM pins to make it work?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 6, 2021, 5:12pm UTC](https://forum.arduino.cc/t/combine-two-arduino-codes/859554/11 "2021-09-06T17:12:05Z")

</div>

Closed on September 6, 2021, 5:12pm UTC

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