1

Topic: Problem controlling motor with slider and servo with joystick

So I am trying to control a DC motor speed using a slider and a 180 deg micro servo using the joystick. The servo works perfectly using the joystick but whenever I use the slider to control the motor speed the servo starts moving erratically even though there's nothing to link the slider and the servo. Also moving the slider doesn't make the motor spin initially but as the slider reaches the end the motor starts to spin along with the servo going crazy. I am a beginner in this field so I may be missing out something pretty basic. Thanks for your help.

/*
   -- New project --
   
   This source code of graphical user interface 
   has been generated automatically by RemoteXY editor.
   To compile this code using RemoteXY library 2.4.3 or later version 
   download by link http://remotexy.com/en/library/
   To connect using RemoteXY mobile app by link http://remotexy.com/en/download/                   
     - for ANDROID 4.5.1 or later version;
     - for iOS 1.4.1 or later version;
    
   This source code is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.    
*/

//////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////

// RemoteXY select connection mode and include library 
#define REMOTEXY_MODE__HARDSERIAL

#include <RemoteXY.h>
#include <Servo.h>

// RemoteXY connection settings 
#define REMOTEXY_SERIAL Serial
#define REMOTEXY_SERIAL_SPEED 9600


// RemoteXY configurate  
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
  { 255,4,0,0,0,30,0,10,13,0,
  5,17,44,16,23,23,31,28,8,4,
  16,79,8,10,42,120,26,1,5,6,
  19,27,31,13,1,66,0 };
  
// this structure defines all the variables and events of your control interface 
struct {

    // input variables
  int8_t joystick_1_x; // =-100..100 x-coordinate joystick position 
  int8_t joystick_1_y; // =-100..100 y-coordinate joystick position 
  int8_t slider_1; // =0..100 slider position 
  uint8_t button_1; // =1 if button pressed, else =0 

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0 

} RemoteXY;
#pragma pack(pop)

/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////

int acc = 0;
int brake = 0;
int turn = 0;
int servoPin = 6;
int angle = 90;

int en = 9;
int in1 = 8;
int in2= 7;

Servo servo;

void setup() 
{
  RemoteXY_Init (); 
  
  
  // TODO you setup code
   pinMode(en,OUTPUT);
  pinMode(in1,OUTPUT);
  pinMode(in2,OUTPUT);
  pinMode(servoPin,OUTPUT);
  servo.attach(servoPin);

  analogWrite(en, 0);
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  angle = 90;
  servo.write(angle);
}

void loop() 
{ 
  RemoteXY_Handler ();
  
  
  // TODO you loop code
  // use the RemoteXY structure for data transfer
  // do not call delay() 

  acc = RemoteXY.slider_1;
  brake = RemoteXY.button_1;
  turn = RemoteXY.joystick_1_x;

  if(turn<0){
    angle = ((-0.3*turn)+90);
    servo.write(angle);
  }else if(turn>0){
    angle = (90-(0.3*turn));
    servo.write(angle);
  }else{
    angle = 90;
    servo.write(angle);
  }

  if(acc>0){
    analogWrite(en,map(acc,0,100,0,255));
    digitalWrite(in1,HIGH);
    digitalWrite(in2,LOW);
  }else{
    analogWrite(en,0);
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
  }

  if(brake == 1){
    analogWrite(en,255);
    digitalWrite(in1,LOW);
    digitalWrite(in2,HIGH);
  }
}