error combine codes

i want to make a heartbeat counter and display it on webpage, i have done with counter and display on web codes, and they work. but when i try to combine them,they dont work,web page cant be opened when i opened serial monitor, it just show character “cacacacacaca…”

please help me fix my code,

#include <SPI.h>
#include <Ethernet.h>
 #include <LiquidCrystal.h>
 #include <SD.h>
 #include <Wire.h>
#include "RTClib.h"
 File webFile;
 File SDFile;
 const int chipSelect = 4;
LiquidCrystal lcd(14,15,16,17,18,19);
// this constant won't change:
const int  sensorPin = A0;
const int  buttonPin = 2;
// Variables will change:
int konter = 0;   // counter for the number of button presses
int detak = 0;
int lastdetak = 0;     // previous state of the button
int detik=0;
int detiks=0;
int BPM=0;
int buttonState = 0;
int sensor;
RTC_Millis rtc;
//Attention: Official SPI port use "D10" as SS interface.You need to define SS pin acording to the actual SS wire connection
 
////define the interface: Dreamer MEGA  X1 PORT
//#define SS  53   //Gadgeteer PIN 6
//#define nRST  46  //Gadgeteer PIN 4
//#define nPWDN 45  //Gadgeteer PIN 5
//#define nINT 2  //Gadgeteer PIN 3
 
//define the interface: Dreamer MEGA X2 PORT
#define SS    10   //Gadgeteer PIN 6
#define nRST  8  //Gadgeteer PIN 4
#define nPWDN 9  //Gadgeteer PIN 5
#define nINT 3  //Gadgeteer PIN 3
 
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,102);
 
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
 
void setup() {
 //Next setting parameter is very important!!!! If you don't have reliable reset setting, W5200 maybe will not work!!!!!  //
  rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
  pinMode(SS,OUTPUT);  //Define the interfave :Dreamer MEGA X2 PORT  Gadgeteer PIN 6 use SS
  pinMode(nRST,OUTPUT);
  pinMode(nPWDN,OUTPUT);
  pinMode(nINT,INPUT); 
  digitalWrite(nPWDN,LOW);  //enable power
  digitalWrite(SS,HIGH);
  digitalWrite(nRST,LOW);  //Reset W5200
  delay(10);
  digitalWrite(nRST,HIGH); 
 delay(200);       // wait W5200 work
 /////////////////////////////////////////////////////////////
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
 if (!SD.begin(4)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
 
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
   pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  // initialize serial communication:
  Serial.begin(9600);
lcd.begin(16, 2);
  lcd.clear();
  lcd.print("Selamat");
lcd.setCursor(0,1);
lcd.print("Datang");
}
 
void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState==HIGH){
    int a=6;
    while (a>1){
      a--;
     
   lcd.clear();
  lcd.print("Letakkan");
  lcd.setCursor(11,1);
  lcd.print(a);
  lcd.setCursor(0,1);
  lcd.print("Jari Anda");
  delay(1000);
    } 
  counter();
}
else
    internet();
}

void counter(){
  detik=1;
  DateTime now = rtc.now();
  while (detik<100){
    detik++;
  detiks= map(detik,0,99,0,9);
    // read the pushbutton input pin:
  lcd.clear();
  lcd.print("Menghitung...");
  lcd.setCursor(0,1);
  lcd.print(detiks);
  sensor = analogRead(sensorPin);
  if (sensor>400){
    detak=HIGH;
  }
  else
   detak=LOW; 
  Serial.print("\t");
  Serial.println(detiks);

  // compare the detak to its previous state
  if (detak != lastdetak) {
    // if the state has changed, increment the counter
    if (detak == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      konter++;
      Serial.println(konter);
    }  
    }
    lastdetak = detak;
    delay(100);
  }
    BPM=konter*6;
    lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("DETAK JNTNG ANDA");
   lcd.setCursor(0,1);
   lcd.print(BPM);
   lcd.setCursor(4,1);
   lcd.print("BPM");
   if (BPM > 12){
     
   SDFile = SD.open("cobacoba.htm", FILE_WRITE);
   if (SDFile) {
    SDFile.println("<tr>");
    SDFile.println("<td>");
    SDFile.print(now.year(), DEC);
    SDFile.print('/');
    SDFile.print(now.month(), DEC);
    SDFile.print('/');
    SDFile.print(now.day(), DEC);
    SDFile.print(' ');
    SDFile.print(now.hour(), DEC);
    SDFile.print(':');
    SDFile.print(now.minute(), DEC);

     SDFile.print("</td>");
     SDFile.println("<td>");
     SDFile.print(BPM);
     SDFile.print("</td>");
     SDFile.println("</tr>");

    Serial.println("simpan SD Berhasil");
    SDFile.close();
    // print to the serial port too:
   }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening file");
  }
   }
  // save the current state as the last state, 
  //for next time through the loop
  
  }


void internet(){
    // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          //webpage header
          client.println("<html>");
          client.println("<head>");
                    //judul
          client.println("<title>HEARTBEAT RATE COUNTER</title>");
          client.println("<head>");
          client.println("<body>");
          //format tabel
          client.println("<style>");
          client.println("table {");
          client.println("width:100%;");
          client.println("}");
          client.println("table, th, td {");
          client.println("border: 1px solid black;");
          client.println("border-collapse: collapse;");
          client.println("}");
          client.println("th, td {");
          client.println("padding: 5px;");
          client.println("text-align: center;");
          client.println("}");
          client.println("table#t01 tr:nth-child(even) {");
          client.println("background-color: #99FF99;");
          client.println("}");
          client.println("table#t01 tr:nth-child(odd) {");
          client.println("background-color:#FFFF66;");
          client.println("}");
          client.println("table#t01 th	{");
          client.println("background-color: blue;");
          client.println("color: white;");
          client.println("}");
          client.println("</style>");

          client.println("
");
          //tabel header
          client.println("<table id='t01'>");
          client.println("<tr>");
          client.println("<th>Waktu Pengukuran</th>");
          client.println("<th>Detak Jantung</th>");
          client.println("</tr>");
          
          //input dari sdcard
          webFile = SD.open("cobacoba.htm");        // open web page file
                    if (webFile) {
                        while(webFile.available()) {
                            client.write(webFile.read()); // send web page to client
                        }
                        webFile.close();
                    }
                    
          //tabel bawah
          client.println("</table>");
          client.println("
");
          client.println("<body>");
                    // add a meta refresh tag, so the browser pulls again every 5 seconds:
                
   
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

Anything that looks like this client.println("HTTP/1.1 200 OK"); should really look like this client.println(F("HTTP/1.1 200 OK"));

That is a large sketch. It could be not enough ram.
Do you run it on a Arduino Uno ? Then it is probably out of ram.
Or do you run it on a Arduino Mega 2560 ? Then I don’t know.

Every string of text will be copied into ram. You can avoid that with the ‘F()’ macro.
You can use the ‘F()’ macro for the print() and println() of the Serial library and the File libary and the Ethernet library. I’m not sure if the lcd library supports it.

client.println(F("<html>"));
Serial.println(F("error opening file"));
SDFile.print(F("<td>"));

(While I was writing this, AWOL wrote the same)

yes i use arduino uno..

thank you,, its work now. :grinning: