problem joining 2 sketches together

hello there : ) i am stuck on a problem, and hope someone can help.

I have two working sketches, one sets up the arduino to communicate with shift registers on my breadboard using pins 8 ,11,12, and allows max/msp to control these three pins via serial, it works great. however the analog ins and other arduino digital pins are not set up. they are disabled,

The second sketch i have sets the arduino up to communicate via serial to max msp, so all the ins and outs are available. this also works great by itself too.

so i have been trying to join the sketches together so i can have all the pins work and also the arduino set up with the shift registers too.

if anyone can help i would be very grateful.

ive pasted the two sketches below, with an attempt of mine below to join them , which is not going to well : /

this is the shift register sketch.

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
byte data1;
byte data2;
byte data3;
void setup() {
Serial.begin(57600);
pinMode(latchPin, OUTPUT);
}
void loop() {
if(Serial.available() > 2) {
data1 = Serial.read();
data2 = Serial.read();
data3 = Serial.read();
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, data1);
shiftOut(dataPin, clockPin, data2);
shiftOut(dataPin, clockPin, data3);
digitalWrite(latchPin, 1);
}
}
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
digitalWrite(myDataPin, pinState);
digitalWrite(myClockPin, 1);
digitalWrite(myDataPin, 0);
}
digitalWrite(myClockPin, 0);
}

This is the max to arduino sketch.

/*
Beta, Beta, Beta, Beta, Beta, Beta, Beta, Beta, Beta, Beta, Beta, Beta, Beta, Beta 

Shared APRIL 2012 by Lasse Vestergaard <lassesvestergaard@gmail.com>

The protocol for communicating with this sketch is:

SERIAL INPUT:
  - set pin state (p) og set pin value (v)
    - When setting a pin state you can write 0 (input),1 (output),2 (pwm)
  - set pin number
  - set value
  - Example: [p 3 2] & [v 3 128] == Pin 3 is set to be pwm and addressed with 50/50 duty cycle (medium "speed")
  (just send as char values from any program that can communicate with serial ports ex. Max/MSP)
    - You´ll need "[" and "]" to start and end requests.

SERIAL OUTPUT:    
  - pin number
  - value
  - Example: 7 0 == Pin 7 is set to receive a digital input (a carriage return is send to end request.)
  - The board will only output through serial if an Arduino port is set to input.
  
*/

unsigned long sendtime;

int portregister[]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
String sample="";
String explodedSample[3];

void setup(){
  sendtime=millis();
  Serial.begin(115200);
}

void loop(){
    while(Serial.available()>0){
      char c=(char)Serial.read();
      if(c == '[')
         sample="";
       else if(c == ']'){
         explodeString(sample);
         if(explodedSample[0].equalsIgnoreCase("v")){
           setPinValue(convertStringToInt(explodedSample[1]),convertStringToInt(explodedSample[2]));
         }
         else if(explodedSample[0].equalsIgnoreCase("p")){
           setPinState(convertStringToInt(explodedSample[1]),convertStringToInt(explodedSample[2]));
         }
       }
       else
         sample+=c;
    }
 
 if(millis()>sendtime+5){
   readSendInput();
   sendtime=millis();
 }
 
}

void readSendInput(){
  for(int i=0;i<18;i++){
    if(portregister[i]==0){
      if(i<12){
        Serial.print(i+2);
        Serial.print(" ");
        Serial.println(digitalRead(i+2));
      }
      else{
        Serial.print(i+2);
        Serial.print(" ");
        Serial.println(analogRead(i+2));
      }
    }
  }
}

void setPinState(int pin, int pinState){
  portregister[pin-2]=pinState;
    if(pinState==2)
      pinState=1;
  pinMode(pin, pinState);
}

void setPinValue(int pin, int val){
  int pinState=portregister[pin-2];
  if(pinState==1){
    digitalWrite(pin,val);
  }
  else if(pinState==2){
    analogWrite(pin,val);
  }
}


/***********************************/
/*        HELPER FUNCTIONS         */
/***********************************/

void explodeString(String s){
  String tem="";
  int tempointer=0;
  
  for(int i=0;i<s.length();i++){
    if(s[i] == ' '){
      explodedSample[tempointer++]=tem;
      tem="";
    }
    else
      tem+=s[i];
  }
  explodedSample[2]=tem;
}

int convertStringToInt(String s){
  char test[s.length()+1];
  s.toCharArray(test, sizeof(test));
  return atoi(test);
}

And here is my novice attempt at joining the two together. : / ive put a ' --------------------------< ' so you can see the joins i made.

------------------------------------< below join is from the max-arduino sketch.

unsigned long sendtime;

int portregister[]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
String sample="";
String explodedSample[3];

----------------------<below join is from the shift register sketch.

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
byte data1;
byte data2;
byte data3;

------------------------<below join is from the max-arduino sketch.

void setup(){
  sendtime=millis();

-------------------------------<below join is from the shift register sketch.

 Serial.begin(57600);
pinMode(latchPin, OUTPUT);
}

 --------------------------<below join is from the max-arduino sketch.

void loop(){
    while(Serial.available()>0){
      char c=(char)Serial.read();
      if(c == '[')
         sample="";
       else if(c == ']'){
         explodeString(sample);
         if(explodedSample[0].equalsIgnoreCase("v")){
           setPinValue(convertStringToInt(explodedSample[1]),convertStringToInt(explodedSample[2]));
         }
         else if(explodedSample[0].equalsIgnoreCase("p")){
           setPinState(convertStringToInt(explodedSample[1]),convertStringToInt(explodedSample[2]));
         }
       }
       else
         sample+=c;
    }
 
 if(millis()>sendtime+5){
   readSendInput();
   sendtime=millis();
 }

--------------------------------------------------<below join is from the shift register sketch.


if(Serial.available() > 2) {
data1 = Serial.read();
data2 = Serial.read();
data3 = Serial.read();
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, data1);
shiftOut(dataPin, clockPin, data2);
shiftOut(dataPin, clockPin, data3);
digitalWrite(latchPin, 1);
 }
}

----------------------------------------<below join is from the max-arduino sketch.


void readSendInput(){
  for(int i=0;i<18;i++){
    if(portregister[i]==0){
      if(i<12){
        Serial.print(i+2);
        Serial.print(" ");
        Serial.println(digitalRead(i+2));
      }
      else{
        Serial.print(i+2);
        Serial.print(" ");
        Serial.println(analogRead(i+2));
      }
    }
  }
}

void setPinState(int pin, int pinState){
  portregister[pin-2]=pinState;
    if(pinState==2)
      pinState=1;
  pinMode(pin, pinState);
}

void setPinValue(int pin, int val){
  int pinState=portregister[pin-2];
  if(pinState==1){
    digitalWrite(pin,val);
  }
  else if(pinState==2){
    analogWrite(pin,val);
  }
}

--------------------------------------------< below join is from the shift register sketch.



void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
digitalWrite(myDataPin, pinState);
digitalWrite(myClockPin, 1);
digitalWrite(myDataPin, 0);
}
digitalWrite(myClockPin, 0);
} 


------------------------------------------------<< final join below ^ is from the max-arduino sketch.


/***********************************/
/*        HELPER FUNCTIONS         */
/***********************************/

void explodeString(String s){
  String tem="";
  int tempointer=0;
  
  for(int i=0;i<s.length();i++){
    if(s[i] == ' '){
      explodedSample[tempointer++]=tem;
      tem="";
    }
    else
      tem+=s[i];
  }
  explodedSample[2]=tem;
}

int convertStringToInt(String s){
  char test[s.length()+1];
  s.toCharArray(test, sizeof(test));
  return atoi(test);
}

thanks for your help : )

which is not going to well

Define "not going too well".

Assuming you remove the join marks, or at least comment them out, the code compiles, so what's the problem?

"merging" code is best done by starting from the largest from the two and add the functionality of (2) as new code. step by step.

If you just want to reuse your code the pattern below is a good start.
Major problem will be the scope (& duplication etc) of the variables, you might start with making them as global as possible.

void setup()
{
  setup1();
  setup2();
}

void loop()
{
  // declare all local vars of loop1 and loop2 here
  loop1();  
  loop2(); 
}

void setup1() 
{
}

void loop1()  
{
}

void setup2()
{
}

void loop2()  
{
}

advice: use method 1.