Split code into multiple codes?!

I now have quite a long code for my carrier OS. So that this is clearer, I would like to split it up into several parts, i.e. files. Is the? How do I do that?
I have an MKR IoT Carrier Rev2 and MKR WiFi 1010 board.
Many, many thanks in advance!

#include <WiFiNINA.h>
#include <Arduino_MKRIoTCarrier.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <TimeLib.h> // Include the TimeLib library
#include <FastLED.h>

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
MKRIoTCarrier carrier;

float temperature = 0;
float humidity = 0;
unsigned long lastTempUpdate = 0;
int currentPage = 0;
bool inSubMenu = false;
int subMenuPage = 0;
bool ledState = false;
int brightness = -10;

void printDashboard() {
  timeClient.update();
  if (timeClient.getSeconds() == 0) {
    String formattedTime = timeClient.getFormattedTime().substring(0, 5);
    setTime(timeClient.getEpochTime()); 
    String formattedDate = String(day()) + "." + String(month()) + "." + String(year()); 
int16_t x1, y1, x2, y2;
uint16_t w1, h1, w2, h2;
uint16_t width = carrier.display.width();
uint16_t height = carrier.display.height();

uint8_t r, g, b;
if (hour() >= 5 && hour() < 7) {
  for (int y = 0; y < height; y++) {
    r = map(y, 0, height-1, 255, 255);
    g = map(y, 0, height-1, 255, 128);
    b = map(y, 0, height-1, 0, 0);
    carrier.display.drawFastHLine(0, y, width, carrier.display.color565(r, g, b));
    carrier.display.setTextColor(ST77XX_BLACK);
  }
} else if (hour() >= 20 || hour() < 4) {
  // set background color to gradient from dark blue to black
  for (int y = 0; y < height; y++) {
    r = map(y, 0, height-1, 0, 0);
    g = map(y, 0, height-1, 0, 0);
    b = map(y, 0, height-1, 64, 0);
    carrier.display.drawFastHLine(0, y, width, carrier.display.color565(r, g, b));
    carrier.display.setTextColor(ST77XX_WHITE);
  }
}
else {
  for (int y = 0; y < height; y++) {
    r = map(y, 0, height-1, 255, 0);
    g = 0;
    b = map(y, 0, height-1, 0, 255);
    carrier.display.drawFastHLine(0, y, width, carrier.display.color565(r, g, b));
    carrier.display.setTextColor(ST77XX_WHITE);
  }
}
    carrier.display.setTextSize(3);

    carrier.display.getTextBounds(formattedDate, 0, 0, &x1, &y1, &w1, &h1); 
    carrier.display.setTextSize(4);
    carrier.display.getTextBounds(formattedTime, 0, 0, &x2, &y2, &w2, &h2); 
    carrier.display.setTextSize(3);

    x1 = (carrier.display.width() - w1) / 2;
    y1 = (carrier.display.height() - h1 - h2 - h2) / 2; 

    x2 = (carrier.display.width() - w2) / 2;
    y2 = y1 + h1 + h2; 

    carrier.display.setCursor(x1, y1); 
    carrier.display.println(formattedDate); 
    
    carrier.display.setTextSize(4);
    carrier.display.setCursor(x2, y2);
    carrier.display.println(formattedTime); 
  }

  delay(1000);
}
void printTemperature() {
carrier.display.fillScreen(ST77XX_BLUE);
carrier.display.setTextColor(ST77XX_WHITE);
carrier.display.setTextSize(2);
int16_t x, y;
uint16_t width, height;
String tempStr = "Temp: " + String(temperature) + " C";
carrier.display.getTextBounds(tempStr, 0, 0, &x, &y, &width, &height);
carrier.display.setCursor((carrier.display.width() - width) / 2, (carrier.display.height() - height) / 2);
carrier.display.print(tempStr);
}

void printHumidity() {
carrier.display.fillScreen(ST77XX_GREEN);
carrier.display.setTextColor(ST77XX_WHITE);
carrier.display.setTextSize(2);
int16_t x, y;
uint16_t width, height;
String humidityStr = "Humidity: " + String(humidity) + " %";
carrier.display.getTextBounds(humidityStr, 0, 0, &x, &y, &width, &height);
carrier.display.setCursor((carrier.display.width() - width) / 2, (carrier.display.height() - height) / 2);
carrier.display.print(humidityStr);
}

void printWeather() {
carrier.display.fillScreen(ST77XX_YELLOW);
carrier.display.setTextColor(ST77XX_BLACK);
carrier.display.setTextSize(2);
int16_t x, y;
uint16_t width, height;
carrier.display.getTextBounds("Weather", 0, 0, &x, &y, &width, &height);
carrier.display.setCursor((carrier.display.width() - width) / 2, (carrier.display.height() - height) / 2);
carrier.display.print("Weather");
}


void printSettings() {
carrier.display.fillScreen(ST77XX_MAGENTA);
carrier.display.setTextColor(ST77XX_BLACK);
carrier.display.setTextSize(2);
int16_t x, y;
uint16_t width, height;
carrier.display.getTextBounds("Settings", 0, 0, &x, &y, &width, &height);
carrier.display.setCursor((carrier.display.width() - width) / 2, (carrier.display.height() - height) / 2);
carrier.display.print("Settings");
}

void printLight() {
  carrier.display.fillScreen(ST77XX_ORANGE);
  carrier.display.setTextColor(ST77XX_BLACK);
  carrier.display.setTextSize(2);
  int16_t x, y;
  uint16_t width, height;
  String text = "LED: ";
  
  // Check if button 0 is pressed
  if (digitalRead(0) == HIGH) {
    // Increase brightness by 10% until it reaches 100%
    brightness += 10;
    if (brightness > 100) {
      // Reset brightness to 0%
      brightness = 0;
      ledState = false;
    } else {
      ledState = true;
    }
  } else {
    ledState = false; // Turn off the LED if button 0 is not pressed
  }
  
  if (ledState) {
    text += "ON";
  } else {
    text += "OFF";
  }
  
  carrier.display.getTextBounds(text, 0, 0, &x, &y, &width, &height);
  
  carrier.display.setCursor((carrier.display.width() - width) / 2, (carrier.display.height() - height) / 2);
  
  carrier.display.print(text);
  
  if (ledState) {
    // Set the LED brightness according to the global variable
    carrier.leds.fill(CRGB::White);
    carrier.leds.setBrightness(brightness);
    carrier.leds.show();
    
  } else {
    // Set the LED color to black
    carrier.leds.fill(CRGB::Black);
    carrier.leds.show();
    
  }
} 

void setup() {
    // Start sound
  carrier.Buzzer.sound(523);
  delay(200);
  carrier.Buzzer.sound(659);
  delay(200);
  carrier.Buzzer.sound(784);
  delay(200);
  carrier.Buzzer.sound(880);
  delay(200);
  carrier.Buzzer.sound(1047);
  delay(600);
  carrier.Buzzer.noSound();
  CARRIER_CASE = false;
  carrier.begin();
  temperature = carrier.Env.readTemperature();
  humidity = carrier.Env.readHumidity();
  carrier.display.setRotation(0);
    const char* ssid = "XXX";
  const char* password = "XXX";

 // Serial.begin(9600);
  carrier.display.fillScreen(ST77XX_BLACK);
  carrier.display.setTextSize(2);

  // Connect to Wi-Fi network with SSID and password
 // Serial.print("Connecting to ");
 // Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
 //   Serial.println("Connecting to WiFi...");
  }

  timeClient.begin();
  // Set time offset to your local timezone in seconds
  timeClient.setTimeOffset(7200);
}

void loop() {
    timeClient.update();
printDashboard();
if (carrier.Buttons.onTouchDown(TOUCH0)) {
    if (inSubMenu && currentPage == 2 && subMenuPage == 0) {
      ledState = !ledState;
      printLight();
    }
    else if (currentPage == 2) {
      inSubMenu = true;
      subMenuPage = 0;
      printLight();
    }
}
 
  carrier.Buttons.update();
  unsigned long now = millis();
  
  if(now - lastTempUpdate > 60000){
    temperature = carrier.Env.readTemperature();
    humidity = carrier.Env.readHumidity();
    lastTempUpdate = now;
  }

  if (carrier.Buttons.onTouchDown(TOUCH2)) {
    currentPage = 0;
    inSubMenu = false;
    printDashboard();
  }
  else if (carrier.Buttons.onTouchDown(TOUCH1) || carrier.Buttons.onTouchDown(TOUCH3)) {
    if (!inSubMenu) {
      currentPage = (currentPage + 1) % 4;
      switch(currentPage) {
        case 0:
          printDashboard();
          break;
        case 1:
          printWeather();
          break;
        case 2:
          printSettings();
          break;
      }
    }
    else {
      subMenuPage = (subMenuPage + 1) % 2;
      switch(currentPage) {
        case 1:
          switch(subMenuPage) {
            case 0:
              printTemperature();
              break;
            case 1:
              printHumidity();
              break;
          }
          break;
        case 2:
          switch(subMenuPage) {
            case 0:
              printLight();
              break;
            case 1:
              printTemperature();
              break;
          }
          break;
      }
    }
  }
  else if (carrier.Buttons.onTouchDown(TOUCH4)) {
    if ((currentPage == 1 || currentPage ==2) && !inSubMenu) {
      inSubMenu = true;
      subMenuPage = (subMenuPage + 1) %2;
      switch(currentPage) {
        case 1:
          switch(subMenuPage) {
            case 0:
              printTemperature();
              break;
            case 1:
              printHumidity();
              break;
          }
          break;
        case 2:
          switch(subMenuPage) {
            case 0:
              printLight();
              break;
            case 1:
              printTemperature();
              break;
          }
          break; 
      }
    }
    else if (inSubMenu) {
      inSubMenu = false;
      switch(currentPage) {
        case 1:
          printWeather();
          break;
        case 2:
          printSettings();
          break; 
      }
    }
}}

Move each of your functions into a new file (in the project folder).
Write a header file declaring all these functions and other common items (libraries, variables, constants...).
#include that header file wherever required.

Just had that discussion in This Thread.

For a basic guide, see My Post #5 in this Thread.

Thanks, but I would have meant if there is someone who could do it specifically on my code.... Otherwise it fits too!

don't think putting each function into a separate file.

it makes sense to organize code supporting some function into the same file. for example, i'm working on a program that has i2c.cpp, wifi.cpp, pcRead.cpp (cli commands), signals.cpp (RR signal code) and sigMap.cpp (tables defining signals and blocks with no code).

.h files provide forward declarations for the public functions in each file

Wrong category! Post in "Jobs and Paid Consultancy".

Otherwise begin yourself and come back in case of problems.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.