When two different set of codes are combined, it don't work

Hi, I am currently doing this for my Final Year Project. My robot has 8 RC servo motors; 4 continuous rotation and 4 180 degrees servo motor. I programmed the continuous rotation motors and 180 degrees servo motor separately. and when i combined both code into one, it didn't work.

Can anyone help me to solve the problem as my servo motors are going crazy and not reacting according to how i move my joystick. I am using 6V, 1A to power my robot.

Please help.

xpot1 and ypot1 refers to x-axis and y-axis of joystick 1
because i have 2 joysticks.

Thanks!

#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo myservo1; // create servo object to control a servo
Servo myservo2;
Servo myservo3;
Servo myservo4;

int xpot1=0; // analog pin used to connect the potentiometer
int ypot1=1;
int xpot2=2;
int ypot2=3;
int startval=90;
int val=90; // variable to read the value from the analog pin
int val2=90;
int val3=90;
int value;
int servodelay=10;
void setup()
{
pinMode(ypot2, INPUT);
servo1.attach(6);
servo2.attach(5);
servo3.attach(10);
servo4.attach(9);
Serial.begin(9600);
myservo1.detach(); // attaches the servo on pin 9 to the servo object
myservo2.detach();
myservo3.detach();
myservo4.detach();
}
void loop()
{
//ypot2=analogRead(A3);
value=analogRead(ypot2);
value=map(value,0,1023,0,179);

myservo1.write(startval);
myservo2.write(val);
myservo3.write(val2);
myservo4.write(val3);
xpot1=analogRead(A0);
ypot1=analogRead(A1);
xpot2=analogRead(A2);
Serial.print("xpot1,X-axis: ");
Serial.print(analogRead(xpot1));
Serial.print("\n");
Serial.print("ypot1,Y-axis: ");
Serial.print(analogRead(ypot1));
Serial.print("\n");
Serial.print("xpot2,X-axis: ");
Serial.print(analogRead(xpot2));
Serial.print("\n");
{
if(value>120){
servo1.writeMicroseconds(900);
servo2.writeMicroseconds(900);
servo3.writeMicroseconds(1100);
servo4.writeMicroseconds(1100);
}
else{
servo1.writeMicroseconds(1100);
servo2.writeMicroseconds(1100);
servo3.writeMicroseconds(900);
servo4.writeMicroseconds(900);
}

Serial.println(value);
delay(1000);
}
{
if (xpot1>620)
{
myservo1.attach(3);
delay(servodelay);
startval++;
}
if (xpot1<580)
{
myservo1.attach(3);
delay(servodelay);
startval--;
}

if (xpot2>630)
{
myservo2.attach(11);
delay(servodelay);
val++;
}
if (xpot2<600)
{
myservo2.attach(11);
delay(servodelay);
val--;
}

if (ypot1>560)
{
myservo3.attach(13);
delay(servodelay);
val2++;
}
if (ypot1<450)
{
myservo3.attach(13);
delay(servodelay);
val2--;
}

if(ypot1>560)
{
myservo4.attach(2);
delay(servodelay);
val3--;
}
if (ypot1<450)
{
myservo4.attach(2);
delay(servodelay);
val3++;
}

if (startval<0 && val<0 && val2<0 && val3<0)
{
delay(servodelay);
startval=0;
val=0;
val2=0;
val3=0;
}
if (startval>150 && val>150 && val2>150 && val3>150)
{
delay(servodelay);
startval=150;
val=150;
val2=150;
val3=150;
}
}
}

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo myservo1;  // create servo object to control a servo
Servo myservo2;
Servo myservo3;
Servo myservo4;

Pretty dumb names for the instances. Surely the servos aren't just sitting on a table doing nothing. The instance names should reflect what the instance does.

If you have kids, you would not name them boy1, boy2, girl2, etc., would you?

  myservo1.detach();  // attaches the servo on pin 9 to the servo object
  myservo2.detach();
  myservo3.detach();
  myservo4.detach();

The detach() method MOST CERTAINLY DOES NOT attach a servo to anything.

Also you are reading the analog value of ypot2, which is assigned to digital pin 3. I seriously doubt that is what you intend.

Ugi

PaulS:
If you have kids, you would not name them boy1, boy2, girl2, etc., would you?

No way!

myBoy1, myGirl2...

:wink:

Shermaine:
when i combined both code into one, it didn't work.

How about telling us something useful about the problem so we can help you.

Very few people ask questions here when their code DOES work.

And the amusing comments about children's names are serious. It is much easier to figure out what code does and therefore to debug it if things have meaningful names - for example leftFrontWheelMotor or rightElbowServo

Building your program as a series of short self-contained functions is also a good idea. See planning and implementing a program.

...R

aarg:
No way!

myBoy1, myGirl2...

:wink:

Surely you would use an array.....