Combine two Arduino codes

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:

#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:



#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");
}

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

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

We cannot see your code. Please post it

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

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

Steve

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)

#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");
}

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 for a smater way to read and parse Serial data

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

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?