combine two sketches

I have recently started making my first robot using arduino and i want to combine two sketches for so but when i am combining it is not working properly.pls tell me someone how to combine these two sketches as i know very less about arduino programming.The sketches are-
1)
#include <Servo.h> //include Servo library

const int RForward = 0;
const int RBackward = 180;
const int LForward = RBackward;
const int LBackward = RForward;
const int RNeutral = 90;
const int LNeutral = 90; //constants for motor speed
const int pingPin = 7;
const int irPin = 0; //Sharp infrared sensor pin
const int dangerThresh = 10; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;
Servo leftMotor;
Servo rightMotor; //declare motors
long duration; //time it takes to recieve PING))) signal

void setup()
{
rightMotor.attach(11);
leftMotor.attach(10);
panMotor.attach(6); //attach motors to proper pins
panMotor.write(90); //set PING))) pan to center
}

void loop()
{
int distanceFwd = ping();
if (distanceFwd>dangerThresh) //if path is clear
{
leftMotor.write(LForward);
rightMotor.write(RForward); //move forward
}
else //if path is blocked
{
leftMotor.write(LNeutral);
rightMotor.write(RNeutral);
panMotor.write(0);
delay(500);
rightDistance = ping(); //scan to the right
delay(500);
panMotor.write(180);
delay(700);
leftDistance = ping(); //scan to the left
delay(500);
panMotor.write(90); //return to center
delay(100);
compareDistance();
}
}

void compareDistance()
{
if (leftDistance>rightDistance) //if left is less obstructed
{
leftMotor.write(LBackward);
rightMotor.write(RForward); //turn left
delay(500);
}
else if (rightDistance>leftDistance) //if right is less obstructed
{
leftMotor.write(LForward);
rightMotor.write(RBackward); //turn right
delay(500);
}
else //if they are equally obstructed
{
leftMotor.write(LForward);
rightMotor.write(RBackward); //turn 180 degrees
delay(1000);
}
}

long ping()
{
// Send out PING))) signal pulse
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

//Get duration it takes to receive echo
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

//Convert duration into distance
return duration / 29 / 2;
}

int ledblue=2;

int tx=1;

int rx=0;

char inSerial[15];

void setup(){ Serial.begin(9600);

pinMode(ledblue, OUTPUT);

pinMode(tx, OUTPUT);

pinMode(rx, INPUT);

allpinslow();

}

void loop()

{ int i=0;

int m=0;

delay(500);

if (Serial.available() > 0) { while (Serial.available() > 0) { inSerial*=Serial.read();*
i++; }
inSerial*='\0';*
Check_Protocol(inSerial); }}
void allpinslow() { digitalWrite(ledblue, HIGH);
digitalWrite(ledblue, LOW); } void Check_Protocol(char inStr[]){ int i=0;
int m=0;
Serial.println(inStr);
if(!strcmp(inStr,"2off")){ //Led Off allpinslow();
digitalWrite(ledblue, LOW); Serial.println("Blue Off");
for(m=0;m<11;m++){ inStr[m]=0;} i=0;} if(!strcmp(inStr,"2on")){ //Led on allpinslow();
digitalWrite(ledblue, HIGH);
Serial.println("Blue on"); for(m=0;m<11;m++){ inStr[m]=0;} i=0;} else{ for(m=0;m<11;m++){ inStr[m]=0; } i=0;
}}

it is not working properly

What should it do ?
What does it do ?

Why didn't you put [­code] ... [­/code] tags around you code to stop it being mangled ?

Read this before posting a programming question

Generally speaking, you don't "combine two sketches". You work out what you are trying to do and write one sketch to do it. Of course, this sketch may contain some c/p code from elsewhere.

Let's put it this way. I have a refrigerator and a motorcycle. How do I combine these two? There's no simple answer: it all depends on what you are hoping that the eventual item will eventually do: do you want a roadworthy fridge? A motorcycle that runs on compressed refrigerant? A fridge with a small petrol-powered backup generator? What?

I have a refrigerator and a motorcycle. How do I combine these two?

Two simple answers:

  • You put the refrigerator onto the motorcycle.
  • You put the motorcycle into the refrigerator

...

Neither are particularly easy to do.

Please use the code tag, that will make your code readable.

As Paul said there is no easy way, you need to merge it line by line.

If there is a will there is a way :slight_smile:

One strategy for merging two sketches is:

  1. resolve any pin conflicts (eg both sketches use pin 2 ) or global variable/function name conflicts (eg both scripts use the variable 'i' ) while the sketches are still separate. Attempt also to resolve other potential conflicts, say delay statements etc.
  2. Rename setup() and loop() from script 1 to setup1() and loop1() respectively.
  3. Rename setup() and loop() from script 2 to setup2() and loop2() respectively.
  4. combine all the code in a new script. The setup() in the new script simply calls setup1() and setup2(). The loop() in the new script simply calls loop1() and loop2().
  5. Debug / integrate the logic if required to get the separate bits to talk to each other / debug again etc.

It is not going to cover all cases, but it may get you started.

sarouje:
If there is a will there is a way :slight_smile:

Absolutely. :wink:

P1100362.png