Understanding library creation

Hi All,
I am struggling to understand how to create my own library for the random amounts of code that I have on one tab. I have researched it and tried several times to figure it out and I am still stuck after almost 3, possibly 4 months of rewriting and testing and still cannot figure it out.
I'm also stuck trying to figure out some other issues but they can wait.
this is my original code (the edited version has got so messy that i struggle to see what i have done). i just need help figuring out which parts go where.
this is a WEMOS D1 MINI, seven segment led clock with temperature and humidity sensors, future webpage for updating, time/date changes and sensor display via web. (HTML not working, its the newest part i am stuck on so ignore that)
all the parts i have "emphasised" are the parts that control the Seven Segment display, which is the parts i would like to put into a library, just so that it is out of view while i work on other areas.
any help would be greatly appreciated, and please dumb it down lol

#include <FS.h>
*#include <TimeLib.h>*
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Wire.h>
*#include "DHT.h"*
#include <ESP8266WebServer.h>

*#define DHTPIN D5*
*#define DHTTYPE DHT11*
//#define ntpServerName "au.pool.ntp.org"
//#define timeZone "AEST-10AEDT,M10.0,M4.1.0/3"

*DHT dht(DHTPIN, DHTTYPE);  //DHT_Unified*
ESP8266WebServer server(80);



const char ssid[] = "SSID";  //  your network SSID (name)
const char pass[] = "PASS";       // your network password

// NTP Servers:
static const char ntpServerName[] = "au.pool.ntp.org";
const int timeZone = +10;


WiFiUDP Udp;
unsigned int localPort = 8888;

time_t getNtpTime();
void digitalClockDisplay();
void printDigits(int digits);
void sendNTPpacket(IPAddress &address);

*const int g_pinData = D3;         //D1*
*const int g_pinCommLatch = D2;    //D2*
*const int g_pinClock = D1;        //D3*
*const int dot1 = D7;*
*const int dot2 = D8;*
*int temp = 0;*
*int humid = 0;*
*int timeH = 0;*
*int timeM = 0;*
*int datetime = 0;*


*byte g_digits [10];             // Definitions of the 7-bit values for displaying digits*
*byte g_digits2 [10];*
*int g_numberToDisplay = 0;      // default number being displayed, 0 *
*const int g_registers = 4;      // Number of shift registers in use, 4*
*byte g_registerArray [g_registers]; // Array of numbers to pass to shift registers*

void setup()
{
  Serial.begin(115200);
  while (!Serial) ; // Needed for Leonardo only
  delay(250);
  Serial.println("Warehouse Clock");
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);
  //dht.begin();
  //configTime(timeZone, ntpServerName);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.print("IP number assigned by DHCP is ");
  Serial.println(WiFi.localIP());
  Serial.println("Starting UDP");
  Udp.begin(localPort);
  Serial.print("Local port: ");
  Serial.println(Udp.localPort());
  Serial.println("waiting for sync");
  setSyncProvider(getNtpTime);
  setSyncInterval(300);
  server.on("/", handle_OnConnect);
  server.onNotFound(handle_NotFound);

  server.begin();
  Serial.println("HTTP server started");

 // when the digital clock was displayed

  *pinMode (g_pinData, OUTPUT);            //define 74595 pin as output*
*  pinMode (g_pinCommLatch, OUTPUT);       //define 74595 pin as output*
*  pinMode (g_pinClock, OUTPUT);           //define 74595 pin as output*
*  pinMode (dot1, OUTPUT);*
*  pinMode (dot2, OUTPUT);*

*int a = 1, b = 2, c = 4, d = 8, e = 16, f = 32, g = 64;*

*    g_digits [0] = 8 + 4 + 2 + 64 + 32 + 1;             //1 + 2 + 4 + 8 + 16 + 32;*
*    g_digits [1] = 4 + 2;                               //2 + 4;*
*    g_digits [2] = 8 + 4 + 16 + 32 + 64;                //1 + 2 + 64 + 16 + 8;*
*    g_digits [3] = 8 + 4 + 16 + 2 + 64;                 //1 + 2 + 4 + 8 + 64;*
*    g_digits [4] = 1 + 16 + 4 + 2;                      //32 + 64 + 2 + 4;*
*    g_digits [5] = 8 + 1 + 16 + 2 + 64;                 //1 + 32 + 64 + 4 + 8;*
*    g_digits [6] = 8 + 1 + 16 + 2 + 64 + 32;            //1 + 32 + 16 + 8 + 4 + 64;*
*    g_digits [7] = 8 + 4 + 2;                           //1 + 2 + 4;*
*    g_digits [8] = 1 + 2 + 4 + 8 + 16 + 32 + 64;        //1 + 2 + 4 + 8 + 16 + 32 + 64;*
*    g_digits [9] = 1 + 8 + 4 + 16 + 2 + 64;             //8 + 4 + 2 + 1 + 32 + 64;*
*    g_digits2 [0] = 1 + 8 + 4 + 16;                     //1 + 2 + 64 + 32;           // Degree dot*
*    g_digits2 [1] = 8 + 1 + 32 + 64;                    //1 + 8 + 16 + 32;           // Capital C*
*    g_digits2 [2] = 32 + 16;                            //64 + 16;                   //  r, 80*
*    g_digits2 [3] = 1 + 32 + 16 + 2;                    //4 + 16 + 32 + 64;           //  h, 116*
*    g_digits [99] = 0;*

    for (int i=0;i<5;i++) {
  
    g_registerArray [0] = g_digits [8];
    g_registerArray [1] = g_digits [8];
    g_registerArray [2] = g_digits [8];
    g_registerArray [3] = g_digits [8];
   
    sendSerialData (g_registers, g_registerArray);
    delay(200);

    g_registerArray [0] = g_digits [99];
    g_registerArray [1] = g_digits [99];
    g_registerArray [2] = g_digits [99];
    g_registerArray [3] = g_digits [99];
   
    sendSerialData (g_registers, g_registerArray);
    delay(200);
}
}
*time_t prevDisplay = 0;*

*void sendSerialData (byte registerCount, byte *pValueArray) {*
*  // Signal to the 595s to listen for data*
*  digitalWrite (g_pinCommLatch, LOW);*

*  for (byte reg = registerCount; reg > 0; reg--)*
*  {*
*    byte value = pValueArray [reg - 1];*

*    for (byte bitMask = 128; bitMask > 0; bitMask >>= 1)*
*    {*
*      digitalWrite (g_pinClock, LOW);*
*      digitalWrite (g_pinData, value & bitMask ? HIGH : LOW);*
*      digitalWrite (g_pinClock, HIGH);*
*    }*
*  }*
*  // Signal to the 595s that I'm done sending*
*  digitalWrite (g_pinCommLatch, HIGH);*
*}*





void loop(){
  
  if (timeStatus() != timeNotSet) {
    if (now() != prevDisplay) { //update the display only if time has changed
      prevDisplay = now();
      if ((second() == 00) || (second() == 15) ||(second() == 30) ||(second() == 45)){
        digitalTempDisplay();
        delay (100);
        digitalHumidDisplay();
        delay (200);
      }else{
        digitalClockDisplay();
      }
    }
  }

}

*void digitalClockDisplay()*
*{*
*  Serial.print(hour());*
*  Serial.print(':');*
*  char buf[2];*
*  sprintf(buf,"%2d",minute());*
*  Serial.print(buf);*
*  Serial.print(':');*
*  Serial.print(second());*
*  Serial.println();*
*  *
*  // digital clock display of the time*
*   g_registerArray [0] = g_digits [hourFormat12() / 10];*
*   g_registerArray [1] = g_digits [hourFormat12() % 10];*
*   g_registerArray [2] = g_digits [minute() / 10];*
*   g_registerArray [3] = g_digits [minute() % 10];*

*   sendSerialData (g_registers, g_registerArray);*
*   digitalWrite(dot1, HIGH);*
*   digitalWrite(dot2, HIGH);*
*   delay(500);*
*   digitalWrite(dot1, LOW);*
*   digitalWrite(dot2, LOW);*
*   delay(500);*
*}*

*void digitalDateDisplay(){*

*  Serial.print(day());*
*  Serial.print(':');*
*  Serial.print(month());*
*  Serial.print(':');*
*  Serial.print(year());*
*  Serial.println();*

*  g_registerArray [0] = g_digits [day() / 10];*
*  g_registerArray [1] = g_digits [day() % 10];*
*  g_registerArray [2] = g_digits [month() / 10];*
*  g_registerArray [3] = g_digits [month() % 10];*

*  sendSerialData (g_registers, g_registerArray);*

*  digitalWrite(dot1, HIGH);*
*  digitalWrite(dot2, HIGH);*
*  delay(500);*
*  digitalWrite(dot1, LOW);*
*  digitalWrite(dot2, LOW);*
*  delay(500);*
*}*

*void digitalYearDisplay(){*
*  *
*  g_registerArray [0] = g_digits [year() / 1000];*
*  g_registerArray [1] = g_digits [(year() % 1000) / 100];*
*  g_registerArray [2] = g_digits [(year() % 100) / 10];*
*  g_registerArray [3] = g_digits [year() % 10];*

*  sendSerialData (g_registers, g_registerArray);*

*  digitalWrite(dot1, HIGH);*
*  digitalWrite(dot2, HIGH);*
*  delay(500);*
*  digitalWrite(dot1, LOW);*
*  digitalWrite(dot2, LOW);*
*  delay(500);*
*}*

*void digitalTempDisplay(){*
*  *
*  Serial.print(temp);*
*  Serial.println();*
*  *
*  temp = dht.readTemperature();*
*  *
*  g_registerArray [0] = g_digits [temp / 10];*
*  g_registerArray [1] = g_digits [temp % 10];*
*  g_registerArray [2] = g_digits2 [0];*
*  g_registerArray [3] = g_digits2 [1];*

*  sendSerialData (g_registers, g_registerArray);*

*  digitalWrite(dot1, HIGH);*
*  digitalWrite(dot2, HIGH);*
*  delay(500);*
*  digitalWrite(dot1, LOW);*
*  digitalWrite(dot2, LOW);*
*  delay(500);*
*}*

*void digitalHumidDisplay(){*
*  *
*  Serial.print(humid);*
*  Serial.println();*

*  humid = dht.readHumidity();*
*  *
*  g_registerArray [0] = g_digits [humid / 10];*
*  g_registerArray [1] = g_digits [humid % 10];*
*  g_registerArray [2] = g_digits2 [2];*
*  g_registerArray [3] = g_digits2 [3];*

*  sendSerialData (g_registers, g_registerArray);*

*  digitalWrite(dot1, HIGH);*
*  digitalWrite(dot2, HIGH);*
*  delay(500);*
*  digitalWrite(dot1, LOW);*
*  digitalWrite(dot2, LOW);*
*  delay(500);*
*}*

void printDigits(int digits)
{
  // utility for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

/*-------- NTP code ----------*/

const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets

time_t getNtpTime()
{
  IPAddress ntpServerIP; // NTP server's ip address

  while (Udp.parsePacket() > 0) ; // discard any previously received packets
  Serial.println("Transmit NTP Request");
  // get a random server from the pool
  WiFi.hostByName(ntpServerName, ntpServerIP);
  Serial.print(ntpServerName);
  Serial.print(": ");
  Serial.println(ntpServerIP);
  sendNTPpacket(ntpServerIP);
  uint32_t beginWait = millis();
  while (millis() - beginWait < 1500) {
    int size = Udp.parsePacket();
    if (size >= NTP_PACKET_SIZE) {
      Serial.println("Receive NTP Response");
      Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
      unsigned long secsSince1900;
      // convert four bytes starting at location 40 to a long integer
      secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
      secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
      secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
      secsSince1900 |= (unsigned long)packetBuffer[43];
      return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
    }
  }
  Serial.println("No NTP Response :-(");
  return 0; // return 0 if unable to get the time
}

// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12] = 49;
  packetBuffer[13] = 0x4E;
  packetBuffer[14] = 49;
  packetBuffer[15] = 52;
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer, NTP_PACKET_SIZE);
  Udp.endPacket();
}

void handle_OnConnect() {
  struct tm timeinfo;

  temp = temp,DEC; // Gets the values of the temperature
  humid = dht.readHumidity(); // Gets the values of the humidity
  timeH = hour();
  timeM = minute();

    
  datetime = timeStatus(),DEC;
  server.send(200, "text/html", SendHTML(temp,humid,timeH,timeM));
}

void handle_NotFound(){
  server.send(404, "text/plain", "Not found");
}

String SendHTML(float tempstat,float humidstat,float timeHstat,float timeMstat){
  String ptr = "<!DOCTYPE html> <html>\n";
  ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  ptr +="<title>ESP32 Weather Report</title>\n";
  ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
  ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
  ptr +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
  ptr +="</style>\n";
  ptr +="</head>\n";
  ptr +="<body>\n";
  ptr +="<div id=\"webpage\">\n";
  ptr +="<h1>Warehouse Clock</h1>\n";
  ptr +="<h6>Built By Sean Wilson</h6>\n";
  ptr +="<p>Temperature: ";
  ptr +=(int)tempstat;
  ptr +=char(176);
  ptr +="C</p>";
  ptr +="<p>Humidity: ";
  ptr +=(int)humidstat;
  ptr +="%</p>";
  ptr +="<p>Time: ";
  ptr +=(int)timeHstat;
  ptr +=":";
  ptr +=(int)timeMstat;
  ptr +="</p>";
  ptr +="</div>\n";
  ptr +="</body>\n";
  ptr +="</html>\n";
  return ptr;
}

sterretje: code tags changed to cpp

and which part would you like to detach?

Welcome to the forum

Do you actually need a library ?

Why not put the code that you want to remove from the main sketch tab in its own .ino file in the sketch directory ? It will show up as a second tab in the IDE and will be compiled automatically as part of the project

Hi Kolaha,
All the part with an * on the left.
Everything with an * controls the display for the seven segments via shift registers.

Ukhelibob,
But doing that won’t include it into the main sketch, i was under the impression you cannot have two ino files for the one project. Hence the use of libraries.
Creating a library for my code was the only option i thought i have…

You can have multiple ino files. In general it works well but you can encounter some problems; for that reason you're far better off using .h and .cpp files.

Note:
The main file has to have the name of the sketch and should in the sketch directory; the name for the other one is free. If you want to open the sketch fro a file explorer, you'll have to double click the main ino file

myProject
+-- myProject.ino
+-- display.ino

And that’s where i get lost. I know how to add the .h file and .cpp file. I just don’t quite understand the process of what part goes into what file.
I assume the header file is more like a directory to the code in the cpp file. I have tried to create the files myself but always end up with multiple errors and “this was not declared” or “no such type” and so on. So i don’t know where i am going wrong.
I will try to add them on my git page at some stage this week and link it to here. And you can have a major laugh at my failure! :rofl:

Why not post them directly here along with a sketch that uses the library ?

I’ll do that tomorrow.
It is a bloody mess though! Lol

so here is the main.cpp file


#include <TimeLib.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Wire.h>
#include "DHT.h"
#include <ESP8266WebServer.h>
#include <FS.h>
#include <LittleFS.h>
#include "secrets.h"
#include "Home.h"
#include "Seg_Display.h"


#define DHTPIN D5
#define DHTTYPE DHT11
//#define ntpServerName "au.pool.ntp.org"
//#define timeZone "AEST-10AEDT,M10.0,M4.1.0/3"

DHT dht(DHTPIN, DHTTYPE);  //DHT_Unified
ESP8266WebServer server(80);


// NTP Servers:
static const char ntpServerName[] = "au.pool.ntp.org";
const int timeZone = +10;


WiFiUDP Udp;
unsigned int localPort = 8888;

time_t getNtpTime();
time_t prevDisplay = 0;
void printDigits(int digits);
void sendNTPpacket(IPAddress &address);

/* // when the digital clock was displayed
byte g_digits [10];                  // Definitions of the 7-bit values for displaying digits
byte g_digits2 [10];
int g_numberToDisplay = 0;          // default number being displayed, 0 
const int g_registers = 4;          // Number of shift registers in use, 4
byte g_registerArray [g_registers]; // Array of numbers to pass to shift registers
*/
const int g_pinData = D3;         //D1
const int g_pinCommLatch = D2;    //D2
const int g_pinClock = D1;        //D3
const int dot1 = D7;
const int dot2 = D8;
int temp = 0;
int humid = 0;
int timeH = 0;
int timeM = 0;

void setup(){

  
  
int a = 1, b = 2, c = 4, d = 8, e = 16, f = 32, g = 64;

    g_digits [0] = 8 + 4 + 2 + 64 + 32 + 1;             //1 + 2 + 4 + 8 + 16 + 32;
    g_digits [1] = 4 + 2;                               //2 + 4;
    g_digits [2] = 8 + 4 + 16 + 32 + 64;                //1 + 2 + 64 + 16 + 8;
    g_digits [3] = 8 + 4 + 16 + 2 + 64;                 //1 + 2 + 4 + 8 + 64;
    g_digits [4] = 1 + 16 + 4 + 2;                      //32 + 64 + 2 + 4;
    g_digits [5] = 8 + 1 + 16 + 2 + 64;                 //1 + 32 + 64 + 4 + 8;
    g_digits [6] = 8 + 1 + 16 + 2 + 64 + 32;            //1 + 32 + 16 + 8 + 4 + 64;
    g_digits [7] = 8 + 4 + 2;                           //1 + 2 + 4;
    g_digits [8] = 1 + 2 + 4 + 8 + 16 + 32 + 64;        //1 + 2 + 4 + 8 + 16 + 32 + 64;
    g_digits [9] = 1 + 8 + 4 + 16 + 2 + 64;             //8 + 4 + 2 + 1 + 32 + 64;
    g_digits2 [0] = 1 + 8 + 4 + 16;                     //1 + 2 + 64 + 32;           // Degree dot
    g_digits2 [1] = 8 + 1 + 32 + 64;                    //1 + 8 + 16 + 32;           // Capital C
    g_digits2 [2] = 32 + 16;                            //64 + 16;                   //  r, 80
    g_digits2 [3] = 1 + 32 + 16 + 2;                    //4 + 16 + 32 + 64;           //  h, 116
    g_digits [99] = 0;

    /*for (int i=0;i<5;i++) {
  
    g_registerArray [0] = g_digits [8];
    g_registerArray [1] = g_digits [8];
    g_registerArray [2] = g_digits [8];
    g_registerArray [3] = g_digits [8];
   
    sendSerialData (g_registers, g_registerArray);
    delay(200);

    g_registerArray [0] = g_digits [99];
    g_registerArray [1] = g_digits [99];
    g_registerArray [2] = g_digits [99];
    g_registerArray [3] = g_digits [99];
   
    sendSerialData (g_registers, g_registerArray);
    delay(200);
}*/
}
time_t prevDisplay = 0;

Serial.begin(115200);
  /*while (!Serial) ; // Needed for Leonardo only
  delay(250);
  Serial.println("Warehouse Clock");
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, passPhrase);
  //dht.begin();
  //configTime(timeZone, ntpServerName);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }*/

  //Serial.print("IP number assigned by DHCP is ");
  //Serial.println(WiFi.localIP());
  //Serial.println("Starting UDP");
  //Udp.begin(localPort);
  //Serial.print("Local port: ");
  //Serial.println(Udp.localPort());
  //Serial.println("waiting for sync");
  //setSyncProvider(getNtpTime);
  //setSyncInterval(300);
  //server.on("/", handle_OnConnect);
  //server.onNotFound(handle_NotFound);

  //server.begin();
  //Serial.println("HTTP server started");

void loop(){
  
  if (timeStatus() != timeNotSet) {
    if (now() != prevDisplay) { //update the display only if time has changed
      prevDisplay = now();
      if ((second() == 00) || (second() == 15) ||(second() == 30) ||(second() == 45)){
        digitalTempDisplay();
        delay (100);
        digitalHumidDisplay();
        delay (200);
      }else{
        digitalClockDisplay();
      }
    }
  }

}


void printDigits(int digits)
{
  // utility for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

/*-------- NTP code ----------*/

const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets

time_t getNtpTime()
{
  IPAddress ntpServerIP; // NTP server's ip address

  while (Udp.parsePacket() > 0) ; // discard any previously received packets
  Serial.println("Transmit NTP Request");
  // get a random server from the pool
  WiFi.hostByName(ntpServerName, ntpServerIP);
  Serial.print(ntpServerName);
  Serial.print(": ");
  Serial.println(ntpServerIP);
  sendNTPpacket(ntpServerIP);
  uint32_t beginWait = millis();
  while (millis() - beginWait < 1500) {
    int size = Udp.parsePacket();
    if (size >= NTP_PACKET_SIZE) {
      Serial.println("Receive NTP Response");
      Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
      unsigned long secsSince1900;
      // convert four bytes starting at location 40 to a long integer
      secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
      secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
      secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
      secsSince1900 |= (unsigned long)packetBuffer[43];
      return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
    }
  }
  Serial.println("No NTP Response :-(");
  return 0; // return 0 if unable to get the time
}

// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12] = 49;
  packetBuffer[13] = 0x4E;
  packetBuffer[14] = 49;
  packetBuffer[15] = 52;
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer, NTP_PACKET_SIZE);
  Udp.endPacket();
}

void handle_OnConnect() {
  struct tm timeinfo;

  temp = temp,DEC; // Gets the values of the temperature
  humid = dht.readHumidity(); // Gets the values of the humidity
  timeH = hour();
  timeM = minute();
  server.on("/$upload.htm",[](){
  server.send(200, "text/html", FPSTR(uploadContent));
  //server.send(200, "text/html", SendHTML(temp,humid,timeH,timeM));
});
}

void handle_NotFound(){
  server.send(404, "text/plain", "Not found");
}

//String SendHTML(float tempstat,float humidstat,float timeHstat,float timeMstat);

this is my display.cpp file

#include "Seg_Display.h"
#include <TimeLib.h>
#include <Arduino.h>

byte g_digits [10];                  // Definitions of the 7-bit values for displaying digits
byte g_digits2 [10];
int g_numberToDisplay = 0;          // default number being displayed, 0 
const int g_registers = 4;          // Number of shift registers in use, 4
byte g_registerArray [g_registers];

  pinMode(g_pinData, OUTPUT);            //define 74595 pin as output
  pinMode(g_pinCommLatch, OUTPUT);       //define 74595 pin as output
  pinMode(g_pinClock, OUTPUT);           //define 74595 pin as output
  pinMode(dot1, OUTPUT);
  pinMode(dot2, OUTPUT);

void sendSerialData (byte registerCount, byte *pValueArray) {
  // Signal to the 595s to listen for data
  digitalWrite (g_pinCommLatch, LOW);

  for (byte reg = registerCount; reg > 0; reg--)
  {
    byte value = pValueArray [reg - 1];

    for (byte bitMask = 128; bitMask > 0; bitMask >>= 1)
    {
      digitalWrite (g_pinClock, LOW);
      digitalWrite (g_pinData, value & bitMask ? HIGH : LOW);
      digitalWrite (g_pinClock, HIGH);
    }
  }
  // Signal to the 595s that I'm done sending
  digitalWrite (g_pinCommLatch, HIGH);
}

void digitalClockDisplay(){
  Serial.print(hour());
  Serial.print(':');
  char buf[2];
  sprintf(buf,"%2d",minute());
  Serial.print(buf);
  Serial.print(':');
  Serial.print(second());
  Serial.println();
  
  // digital clock display of the time
   g_registerArray [0] = g_digits [hourFormat12() / 10];
   g_registerArray [1] = g_digits [hourFormat12() % 10];
   g_registerArray [2] = g_digits [minute() / 10];
   g_registerArray [3] = g_digits [minute() % 10];

   sendSerialData (g_registers, g_registerArray);
   digitalWrite(dot1, HIGH);
   digitalWrite(dot2, HIGH);
   delay(500);
   digitalWrite(dot1, LOW);
   digitalWrite(dot2, LOW);
   delay(500);
}

void digitalDateDisplay(){

  Serial.print(day());
  Serial.print(':');
  Serial.print(month());
  Serial.print(':');
  Serial.print(year());
  Serial.println();

  g_registerArray [0] = g_digits [day() / 10];
  g_registerArray [1] = g_digits [day() % 10];
  g_registerArray [2] = g_digits [month() / 10];
  g_registerArray [3] = g_digits [month() % 10];

  sendSerialData (g_registers, g_registerArray);

  digitalWrite(dot1, HIGH);
  digitalWrite(dot2, HIGH);
  delay(500);
  digitalWrite(dot1, LOW);
  digitalWrite(dot2, LOW);
  delay(500);
}

void digitalYearDisplay(){
  
  g_registerArray [0] = g_digits [year() / 1000];
  g_registerArray [1] = g_digits [(year() % 1000) / 100];
  g_registerArray [2] = g_digits [(year() % 100) / 10];
  g_registerArray [3] = g_digits [year() % 10];

  sendSerialData (g_registers, g_registerArray);

  digitalWrite(dot1, HIGH);
  digitalWrite(dot2, HIGH);
  delay(500);
  digitalWrite(dot1, LOW);
  digitalWrite(dot2, LOW);
  delay(500);
}

void digitalTempDisplay(){
  
  Serial.print(temp);
  Serial.println();
  
  temp = dht.readTemperature();
  
  g_registerArray [0] = g_digits [temp / 10];
  g_registerArray [1] = g_digits [temp % 10];
  g_registerArray [2] = g_digits2 [0];
  g_registerArray [3] = g_digits2 [1];

  sendSerialData (g_registers, g_registerArray);

  digitalWrite(dot1, HIGH);
  digitalWrite(dot2, HIGH);
  delay(500);
  digitalWrite(dot1, LOW);
  digitalWrite(dot2, LOW);
  delay(500);
}

void digitalHumidDisplay(){
  
  Serial.print(humid);
  Serial.println();

  humid = dht.readHumidity();
  
  g_registerArray [0] = g_digits [humid / 10];
  g_registerArray [1] = g_digits [humid % 10];
  g_registerArray [2] = g_digits2 [2];
  g_registerArray [3] = g_digits2 [3];

  sendSerialData (g_registers, g_registerArray);

  digitalWrite(dot1, HIGH);
  digitalWrite(dot2, HIGH);
  delay(500);
  digitalWrite(dot1, LOW);
  digitalWrite(dot2, LOW);
  delay(500);
}




 

and lastly this is the display.h file

#ifndef SEG_DISPLAY_H
#define SEG_DISPLAY_H
#include "Arduino.h"

class Seg_Display{
  public:
  void digitalClockDisplay();
  void digitalDateDisplay();
  void digitalYearDisplay();
  void digitalTempDisplay();
  void digitalHumidDisplay();
  
  private:
  int g_digits;
  int g_digits2;
  
};
#endif

not need to be declared somewhere in disp...h ?

main.cpp not contain disp.h/cpp include, but double sev_seg-h

I know it is all wrong as it does not work, and I've only just realised today while trying to teach someone about excel formulas, that my "library" has hard coded requests for time, temp and humidity, which I am guessing is just one of many issues.

sendSerialData is my function to blank the registers and await data to be sent.
a friend helped me write that up, and I vaguely remember what it all means.
the project was built back 2016 using an arduino uno.

I have edited the code and uploaded it to GitHub. all the files are there, but I am still stuck lol.
https://github.com/mrwolf81/Warehouse-sized-clock-WEMOS.git

In line 34 of Seg_Display.h you declare sendSerialData() as:
void sendSerialData();
but on line 22 of Seg_Display.cpp you declare sendSerialData() as:
void Seg_Display::sendSerialData((byte registerCount, byte *pValueArray))
The extra set of parens around the argument list are confusing. Removing them and adding the argument list to Seg_Display.h fixes those problems.

const int g_pinData = D3;         //D1
const int g_pinCommLatch = D2;    //D2
const int g_pinClock = D1;        //D3
const int dot1 = D7;
const int dot2 = D8;

These declarations are needed in Seg_Display.cpp so move them to Seg_Display.h.

You can't call the member function "sendSerialData()" from Wemos_Clock.ino because you have not created an instance of your Seg_Display object. You should add an instance like this:
Seg_Display display;
Then you can call:
display.sendSerialData()

Around line 95 you have a second declaration of "prevDisplay".

You commented out the declarations of "digitalTempDisplay()", "digitalHumidDisplay()" and "digitalClockDisplay()". Did you intend those to be in your library as member functions of your class?

The class constructor (Seg_Display()) should be in the library.

After all that I gave up. Still a bunch of compile errors.

It might be time to start over. Here is a tutorial I wrote on making a library. If you don't need to handle multiple displays you can skip the second half about encapsulating your library functions into an object class.

When you are first starting out developing a library you can start with a simple demonstration sketch that shows what you want the library to do. Let's make a library that blinks an LED at a specified rate. We can start with something like File->Examples->02.Digital->BlinkWithoutDelay (I am removing most of the comments for compactness.)

const int ledPin =  LED_BUILTIN;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 1000; 

void setup() 
{
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  unsigned long currentMillis = millis();
  
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;

    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

The next step is to move the part you want the library to do into one or more functions. This sketch will do exactly the same thing as before but with very little in setup() and loop() and most code in a function:

const int ledPin =  LED_BUILTIN;
int ledState = LOW;
unsigned long previousMillis = 0;

void setup()
{
  pinMode(ledPin, OUTPUT);
}

void blink(unsigned long interval)
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;

    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

void loop()
{
  blink(1000);
}

Now comes the 'library' split. You start by creating a new tab. At the right end of the bar just above the sketch is a little triangle in a square. Click on that and select "New Tab". Name the new tab "BlinkLib.cpp". Now cut the 'blink' function out of the main tab and paste it into the new tab.

If you try to Verify your sketch now you will find some errors. The first is:

BlinkLib.cpp:3:33: error: 'millis' was not declared in this scope
   unsigned long currentMillis = millis();
                                 ^~~~~~

This is because, unlike the '.ino' files, the '.cpp' files don't automatically get
#include <Arduino.h>
inserted at compile time. Add that line to the top of BlinkLib.cpp and do another Verify.

The next error is:

BlinkLib.cpp:6:23: error: 'previousMillis' was not declared in this scope
   if (currentMillis - previousMillis >= interval)
                       ^~~~~~~~~~~~~~

This is because that global variable declaration didn't get moved to the library. In this case, only the library needs it so move the declaration to the top of BlinkLib.cpp. While you're at it, move 'ledState' and 'interval', too. Then try another Verify.

Now you get the error:

sketch_jan05a:13:3: error: 'blink' was not declared in this scope
   blink(1000);
   ^~~~~

That's because the main sketch no longer contains a declaration for the blink() function. This is a good time to create another tab to share declarations between the library and the sketch. Create a new tab named BlinkLib.h (h for 'header'). In that new tab, put the line:
void blink(unsigned long);
Then in the main sketch, add the line:
#include "BlinkLib.h"
This will allow the main sketch to see a declaration of "blink()".

That leads you to the next error:

BlinkLib.cpp:21:18: error: 'ledPin' was not declared in this scope
     digitalWrite(ledPin, ledState);
                  ^~~~~~

We used 'ledPin' in setup() so we left the declaration there, but now BlinkLib.cpp needs the declaration. We need a way for setup() to set the pin mode so we create another function in BlinkLib.cpp:

void setPin(int pin)
{
  ledPin = pin;
  pinMode(ledPin, OUTPUT);
}

Put the declaration of setPin() into BlinkLib.h so the sketch can use it:
void setPin(int);
Move the declaration of 'ledPin' into BlinkLib.cpp. Now you can have setup() call setPin().

All-together you have:
Main Sketch:

#include "BlinkLib.h"

void setup()
{
  setPin(LED_BUILTIN);
}

void loop()
{
  blink(1000);
}

Library header:

void blink(unsigned long);
void setPin(int);

Library source:

#include <Arduino.h>
#include "BlinkLib.h"

int ledState = LOW;
unsigned long previousMillis = 0;
int ledPin =  LED_BUILTIN;

void setPin(int pin)
{
  ledPin = pin;
  pinMode(ledPin, OUTPUT);
}

void blink(unsigned long interval)
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;

    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Your library is complete! Now you can move the files out of the sketch directory and into a library folder. In your sketchbook directory (see Preferences if you don't know where that is) find (or create) a subdirectory names 'libraries'. That is where all your third-party (not built-in) libraries are kept. In the 'libraries' directory, create a directory named BlinkLib and move BlinkLib.h and BlinkLib.cpp to there.

Now you can delete those two tabs from your sketch and the sketch will still Verify! The compiler will find the BlinkLib directory and make those files part of the Verify process.

Now you can turn your sketch into a library example. Move the .ino file to libraries/BlinkLib/examples/test/test.ino. It will show up in the IDE under: File->Examples->BlinkLib->test

Ah, but there is one small problem. Now you want to blink multiple LEDs at different rates! There is only one "ledPin" variable so if you call "setPin()" a second time, the first pin number is replaced by the second pin number, and blink() will only blink the second pin. Also the "ledState" and "previousMillis" globals are shared.

To fix that, we are going to 'encapsulate' the functionality of the library into an 'object'. You can then make multiple 'instances' of the object, each with its own variables.

Let's create an object class named 'Blinker'. This goes into BlinkLib.h in place of the shared function prototypes.

class Blinker
{
  public:
    Blinker(const byte pin);  // "Constructor" called when the object is defined
    
    void blink(const unsigned long interval);

  private:
    const byte ledPin;
    int ledState;
    unsigned long previousMillis;
};

Now we can change the test sketch to blink two LEDs:

#include "BlinkLib.h"

Blinker blinker(LED_BUILTIN);  // Blink the built-in LED
Blinker blinker2(9);  // Blink the LED on Pin 9

void setup()
{
}

void loop()
{
  blinker.blink(1000);
  blinker2.blink(750);
}

We can try to Verify the test sketch but we have not defined the functions ("member functions" or "methods") declared in the class yet. We get undefined reference errors for Blinker::Blinker() (the constructor) and Blinker::blink() (the function that does stuff!). The second one is easy to fix because we just have to change the declaration of 'blink' from:
void blink(unsigned long interval)
to
void Blinker::blink(unsigned long interval)
That tells the compiler that we are not defining just a function, but a function that is a member ("member function" or "method") of the object class "Blinker". Nothing else about 'blink()' has to be changed because the blink() "method" is perfectly happy to use the "member variables" of its class in place of the old globals. We can remove the globals and the setPin() function from BlinkLib.cpp because they are no longer used:

#include <Arduino.h>
#include "BlinkLib.h"

void Blinker::blink(unsigned long interval)
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;

    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Now that "Blinker::blink()" is taken care of, we need to define Blinker::Blinker(). This is the "constructor": the function automatically called when an object of this class (an "instance") is created ("instantiated"). It is a good place to do some initialization. We can put this function in BlinkLib.cpp:

Blinker::Blinker(const byte pin)
{
  ledPin = pin;
  ledState = LOW; 
  previousMillis = 0;
  
  pinMode(ledPin, OUTPUT);
  previousMillis = millis();
}

The only problem is, "ledPin" is declared with the keyword 'const' so it can't be assigned a value inside the constructor. It has to be given a value BEFORE the body of the constructor so we have to use some special syntax:

Blinker::Blinker(const byte pin) : ledPin(pin)
{
  ledState = LOW; 
  previousMillis = 0;
  
  pinMode(ledPin, OUTPUT);
  previousMillis = millis();
}

Yay! The sketch compiles without any errors or warnings!

Except for one thing. A thing the compiler can't warn you about because it doesn't know. The Arduino library isn't initialized until just before your setup() function is called. That is AFTER all of the global variables are initialized. That includes calling the constructor of every global object instance. In other words, your object class can't safely call any Arduino functions (like "pinMode()" or "millis()") in the constructor! You may have wondered why some Arduino library objects have a 'begin()' method that you have to call from setup(). That's why.

To make our object safe to use we add a 'begin()' method (the name is just an Arduino convention) and call the Arduino functions from there.

void Blinker::begin()
{
  pinMode(ledPin, OUTPUT);
  previousMillis = millis();
}

That goes in BlinkLib.cpp and we call it in setup().

We have to add the "void begin();" declaration to the class (in BlinkLib.h) and we're ready to go!

All-together you have:
Main Sketch:

#include "BlinkLib.h"

Blinker blinker(LED_BUILTIN);  // Blink the built-in LED
Blinker blinker2(9);  // Blink the LED on Pin 9

void setup()
{
  blinker.begin();
  blinker2.begin();
}

void loop()
{
  blinker.blink(1000);
  blinker2.blink(750);
}

Library header:

class Blinker
{
  public:
    Blinker(const byte pin);  // "Constructor" called when the object is defined
    void begin(); // A function to call to do initialization

    void blink(const unsigned long interval);

  private:
    const byte ledPin;
    int ledState;
    unsigned long previousMillis;
};

Library source:

#include <Arduino.h>
#include "BlinkLib.h"

Blinker::Blinker(const byte pin) : ledPin(pin)
{
  ledState = LOW;
  previousMillis = 0;
}

void Blinker::begin()
{
  pinMode(ledPin, OUTPUT);
  previousMillis = millis();
}

void Blinker::blink(unsigned long interval)
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;

    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Hi John,
thank you very much! this has been a massive help!
I think I have worked it out, well kind of, still have lots of errors but it is a work in progress.

well this is far as i have managed to get.
Wemos_Clock.ino

#include "secrets.h"
#include "Seg_Display.h"


const int dot1 = D7; //column dot 1
const int dot2 = D8; //column dot 2

/*
int a = 1, b = 2, c = 4, d = 8, e = 16, f = 32, g = 64;

    g_digits [0] = 8 + 4 + 2 + 64 + 32 + 1;             //1 + 2 + 4 + 8 + 16 + 32;
    g_digits [1] = 4 + 2;                               //2 + 4;
    g_digits [2] = 8 + 4 + 16 + 32 + 64;                //1 + 2 + 64 + 16 + 8;
    g_digits [3] = 8 + 4 + 16 + 2 + 64;                 //1 + 2 + 4 + 8 + 64;
    g_digits [4] = 1 + 16 + 4 + 2;                      //32 + 64 + 2 + 4;
    g_digits [5] = 8 + 1 + 16 + 2 + 64;                 //1 + 32 + 64 + 4 + 8;
    g_digits [6] = 8 + 1 + 16 + 2 + 64 + 32;            //1 + 32 + 16 + 8 + 4 + 64;
    g_digits [7] = 8 + 4 + 2;                           //1 + 2 + 4;
    g_digits [8] = 1 + 2 + 4 + 8 + 16 + 32 + 64;        //1 + 2 + 4 + 8 + 16 + 32 + 64;
    g_digits [9] = 1 + 8 + 4 + 16 + 2 + 64;             //8 + 4 + 2 + 1 + 32 + 64;
    g_digits2 [0] = 1 + 8 + 4 + 16;                     //1 + 2 + 64 + 32;           // Degree dot
    g_digits2 [1] = 8 + 1 + 32 + 64;                    //1 + 8 + 16 + 32;           // Capital C
    g_digits2 [2] = 32 + 16;                            //64 + 16;                   //  r, 80
    g_digits2 [3] = 1 + 32 + 16 + 2;                    //4 + 16 + 32 + 64;           //  h, 116
    g_digits [99] = 0;
    */


void setup(){

}
void loop(){
  Seg_Display();
}