How do i combine two Arduino sketches?

First one

#include <TinyGPS.h>

#include <LiquidCrystal.h>

LiquidCrystal lcd( 12,11, 5, 4, 3, 2 );

// Create an instance of the TinyGPS object

TinyGPS gps;

void getgps(TinyGPS &gps);

void setup()

{

Serial.begin(9600);

lcd.begin(20, 4);

}

void getgps(TinyGPS &gps)

                                        // The getgps function will display the required data on the LCD

{

float latitude, longitude;

//decode and display position data

gps.f_get_position(&latitude, &longitude);

lcd.setCursor(0,0);

lcd.print("Lat:");

lcd.print(latitude,5);

lcd.print(" ");

lcd.setCursor(0,1);

lcd.print("Long:");

lcd.print(longitude,5);

lcd.print(" ");

delay(3000); // wait for 3 seconds

lcd.clear();
int year;
byte month, day, hour, minute, second, thousand;
gps.crack_datetime(&year,&month, day, &hour, &minute, &second, &thousand);

lcd.setCursor(1,2);

lcd.print(hour, DEC);

lcd.print(":");
if (minute<10){
 
 lcd.print("0");

lcd.print(minute, DEC);
  }
else if(minute>=10){
 lcd.print(minute, DEC);
 }
lcd.print(":");
if (second<10)
{  lcd.print("0");

lcd.print(second, DEC);
 }
else if(second>=10)
{
  lcd.print(second, DEC);  
 }
lcd.print(" ");

lcd.print(day);

lcd.print("/ ");

lcd.print(month, DEC);

lcd.print("/ ");

lcd.print(year, DEC);

lcd.setCursor(0,3);

lcd.print(gps.f_altitude());

lcd.print("n ");
lcd.print(gps.f_speed_kmph());
lcd.print("kmph");

}

void loop()

{

byte a;

if ( Serial.available() > 0 ) // if there is data coming into the serial line

{

a = Serial.read(); // get the byte of data

if(gps.encode(a)) // if there is valid GPS data...

{

getgps(gps); // grab the data and display it on the LCD

}

}
}

second sketch

#include <LiquidCrystal.h>
#include <TimerOne.h>

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

int HBSensor = 4;
int HBCount = 0;
int HBCheck = 0;
int TimeinSec = 0;
int HBperMin = 0;
int HBStart = 2;
int HBStartCheck = 0;
int StressLevel = 0;
int temp=0;

void setup() {
 Serial.begin(9600);
 lcd.begin(20, 4);
 pinMode(HBSensor, INPUT);
 pinMode(HBStart, INPUT_PULLUP);
 pinMode(13,OUTPUT);
 Timer1.initialize(800000); 
 Timer1.attachInterrupt( timerIsr );
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Current HB  : ");
 lcd.setCursor(0,1);
 lcd.print("Time in Sec : ");
 lcd.setCursor(0,2);
 lcd.print("HB per Min  : 0.0");
 lcd.setCursor(0,4);
 lcd.print("Stress level is :"); 
}

void loop() {
   if(digitalRead(HBStart) == LOW){lcd.setCursor(0,3);lcd.print("HB Counting ..");HBStartCheck = 1;}
 if(HBStartCheck == 1)
 {
     if((digitalRead(HBSensor) == HIGH) && (HBCheck == 0))
     {
       {
       HBCount =(rand() % (8 - 17 + 1)) + 8;
       HBCheck = 1;
     lcd.setCursor(14,0);
       lcd.print(HBCount);
       lcd.print(" ");
     }}
     if((digitalRead(HBSensor) == LOW) && (HBCheck == 1))
     {
       HBCheck = 0;   
     }
     if(TimeinSec ==15)
     {
       TimeinSec==12;
         HBperMin = HBCount * 6;
         HBStartCheck = 0;
         lcd.setCursor(14,2);
         lcd.print(HBperMin);
         lcd.print(" ");
         lcd.setCursor(0,3);
        
         HBCount = 0;
         TimeinSec = 0;
         StressLevel = 0;
         if(HBperMin>=90)
        {lcd.setCursor(0,4);lcd.print("Stress is High");}
 if(HBperMin<90){lcd.setCursor(0,4);lcd.print("Stress is Low");} 
     
     }
    } 

   }
void timerIsr()
{
 if(HBStartCheck == 1)
 {
     TimeinSec = TimeinSec + 1;
     lcd.setCursor(14,1);
     lcd.print(TimeinSec);
     lcd.print(" ");
 }
}

I tried to combine them and on my lcd will show up only gps data from the first code.
(Im working with this project on proteus)

Please use the code tags button, </> on the menu, next time you post code.
And CTRL-T to properly space/indent the code.

The usual way to combine code is:
Put all the stuff before setup() together.
Put the stuff in setup() into one setup() section.
Put the stuff il loop() into one loop() section.
Any functions outside of setup() or loop() will stay outside of loop.

Now go thru and resolve any pin use duplications, make variables unique that need to be unique, clean up any performance things, etc.

So your code would look like this after the basic combining

#include <TinyGPS.h>

#include <LiquidCrystal.h>

LiquidCrystal lcd( 12, 11, 5, 4, 3, 2 );

// Create an instance of the TinyGPS object

TinyGPS gps;
#include <LiquidCrystal.h>
#include <TimerOne.h>

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

int HBSensor = 4;
int HBCount = 0;
int HBCheck = 0;
int TimeinSec = 0;
int HBperMin = 0;
int HBStart = 2;
int HBStartCheck = 0;
int StressLevel = 0;
int temp = 0;

void getgps(TinyGPS &gps);

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

  lcd.begin(20, 4);
  Serial.begin(9600);
  lcd.begin(20, 4);
  pinMode(HBSensor, INPUT);
  pinMode(HBStart, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  Timer1.initialize(800000);
  Timer1.attachInterrupt( timerIsr );
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Current HB  : ");
  lcd.setCursor(0, 1);
  lcd.print("Time in Sec : ");
  lcd.setCursor(0, 2);
  lcd.print("HB per Min  : 0.0");
  lcd.setCursor(0, 4);
  lcd.print("Stress level is :");

}

void getgps(TinyGPS &gps)

// The getgps function will display the required data on the LCD

{

  float latitude, longitude;

  //decode and display position data

  gps.f_get_position(&latitude, &longitude);

  lcd.setCursor(0, 0);

  lcd.print("Lat:");

  lcd.print(latitude, 5);

  lcd.print(" ");

  lcd.setCursor(0, 1);

  lcd.print("Long:");

  lcd.print(longitude, 5);

  lcd.print(" ");

  delay(3000); // wait for 3 seconds

  lcd.clear();
  int year;
  byte month, day, hour, minute, second, thousand;
  gps.crack_datetime(&year, &month, day, &hour, &minute, &second, &thousand);

  lcd.setCursor(1, 2);

  lcd.print(hour, DEC);

  lcd.print(":");
  if (minute < 10) {

    lcd.print("0");

    lcd.print(minute, DEC);
  }
  else if (minute >= 10) {
    lcd.print(minute, DEC);
  }
  lcd.print(":");
  if (second < 10)
  { lcd.print("0");
    lcd.print(second, DEC);
  }
  else if (second >= 10)
  {
    lcd.print(second, DEC);
  }
  lcd.print(" ");

  lcd.print(day);

  lcd.print("/ ");

  lcd.print(month, DEC);

  lcd.print("/ ");

  lcd.print(year, DEC);

  lcd.setCursor(0, 3);

  lcd.print(gps.f_altitude());

  lcd.print("n ");
  lcd.print(gps.f_speed_kmph());
  lcd.print("kmph");

}

void loop()
{
  byte a;

  if ( Serial.available() > 0 ) // if there is data coming into the serial line

  {
    a = Serial.read(); // get the byte of data

    if (gps.encode(a)) // if there is valid GPS data...

    {

      getgps(gps); // grab the data and display it on the LCD

    }
  }
  if (digitalRead(HBStart) == LOW) {
    lcd.setCursor(0, 3);
    lcd.print("HB Counting ..");
    HBStartCheck = 1;
  }
  if (HBStartCheck == 1)
  {
    if ((digitalRead(HBSensor) == HIGH) && (HBCheck == 0))
    {
      {
        HBCount = (rand() % (8 - 17 + 1)) + 8;
        HBCheck = 1;
        lcd.setCursor(14, 0);
        lcd.print(HBCount);
        lcd.print(" ");
      }
    }
    if ((digitalRead(HBSensor) == LOW) && (HBCheck == 1))
    {
      HBCheck = 0;
    }
    if (TimeinSec == 15)
    {
      TimeinSec == 12;
      HBperMin = HBCount * 6;
      HBStartCheck = 0;
      lcd.setCursor(14, 2);
      lcd.print(HBperMin);
      lcd.print(" ");
      lcd.setCursor(0, 3);

      HBCount = 0;
      TimeinSec = 0;
      StressLevel = 0;
      if (HBperMin >= 90)
      {
        lcd.setCursor(0, 4);
        lcd.print("Stress is High");
      }
      if (HBperMin < 90) {
        lcd.setCursor(0, 4);
        lcd.print("Stress is Low");
      }
    }
  }
}
void timerIsr()
{
  if (HBStartCheck == 1)
  {
    TimeinSec = TimeinSec + 1;
    lcd.setCursor(14, 1);
    lcd.print(TimeinSec);
    lcd.print(" ");
  }
}
}

And you can see the duplication of pins, duplication of setting up the LCD, duplication of Serial.begin(), etc.
Go thru and clean that stuff up.
I don’t have the TinyGPS library, so my attempt to compile fails immediately.

Karma added for posting code using code tags in your first post.

You should post your combined sketch so we can see what you created and maybe find an issue in there :wink:

Further it's also possible that proteus does not understand what your code is supposed to be doing and makes a mess of it. To my knowledge, nobody has written a perfect simulator yet.

http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

sterretje:
Karma added for posting code using code tags in your first post.

He didn't! :roll_eyes: Read it carefully.

OOPS, probably too early in the morning.

Lucky guy.