Memory almost full

Hi,
I'm new at programing and not very good at it.
I know that my program could be written in a much more efficent way, but don't know how I can do that... My problem is memory size! (using an Arduino UNO)

How do I create a function that do the work in my Loop void?
And how do I put together my Strings in a better way?

#include <SD.h>
#include <SPI.h>

File myFile;

#define chipSelect 10
#define X21Pin A0
#define K200Pin A1
#define K330Pin A2
#define Y72Pin A3
#define Y74Pin A4
#define Y25Pin A5

unsigned long X21_F_TRIG;
unsigned long Y25_F_TRIG;
unsigned long K200_F_TRIG;
unsigned long K330_F_TRIG;
unsigned long X21_R_TRIG;
unsigned long Y25_R_TRIG;
unsigned long K200_R_TRIG;
unsigned long TIME_X21_DOWN;
unsigned long TIME_Y25_DOWN;
unsigned long TIME_K200_DOWN;
unsigned long TIME_K330_DOWN;
unsigned long X21_F_FILTERED;
unsigned long Y25_F_FILTERED;
unsigned long K200_F_FILTERED;
unsigned long K330_F_FILTERED;
unsigned long X21_F_FILTERED_OLD;
unsigned long Y25_F_FILTERED_OLD;
unsigned long K200_F_FILTERED_OLD;
unsigned long K330_F_FILTERED_OLD;
unsigned long X21_F_TRIG_DELAYED;
unsigned long Y25_F_TRIG_DELAYED;
unsigned long K200_F_TRIG_DELAYED;
unsigned long K330_F_TRIG_DELAYED;

int delay_ind_print = 150;
int filterTime = 20;
int StartUpTime = 3000;
int no_return = 15000;
int X21_OLD;
int Y25_OLD;
int K200_OLD;
int K330_OLD;
int X21;
int K200;
int K330;
int Y72;
int Y74;
int Y25;
int id = 0;
int init1 = 1;
String SdPrint1;
String SdPrint2;
//String SdPrint3;
String oc="Oc";

void setup() {
  // put your setup code here, to run once:
pinMode(10, OUTPUT);
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("Initializing SD card...");
  pinMode(chipSelect, OUTPUT);
  
  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    setup();
  }
  
  Serial.println("initialization done.");  
  
  pinMode (X21Pin, INPUT);
  pinMode (K200Pin, INPUT);
  pinMode (K330Pin, INPUT);
  pinMode (Y72Pin, INPUT);
  pinMode (Y74Pin, INPUT);
  pinMode (Y74Pin, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  X21 = digitalRead(X21Pin);
  K200 = digitalRead(K200Pin);
  K330 = digitalRead(K330Pin);
  Y72 = digitalRead(Y72Pin);
  Y74 = digitalRead(Y74Pin);
  Y25 = digitalRead(Y25Pin);

  X21_F_TRIG = (!X21 && X21_OLD);
  X21_F_FILTERED = (!X21 && (millis() - TIME_X21_DOWN) > no_return);
  X21_F_TRIG_DELAYED = (X21_F_FILTERED && !X21_F_FILTERED_OLD && (millis() >= StartUpTime));
  X21_R_TRIG = (X21 && !X21_OLD && (millis() >= StartUpTime) && (millis() - TIME_X21_DOWN) >= filterTime);
  K200_F_TRIG = (!K200 && K200_OLD);
  K200_F_FILTERED = (!K200 && (millis() - TIME_K200_DOWN) > delay_ind_print);
  K200_F_TRIG_DELAYED = (K200_F_FILTERED && !K200_F_FILTERED_OLD && (millis() >= StartUpTime));
  K200_R_TRIG = (K200 && !K200_OLD && (millis() >= StartUpTime) && (millis() - TIME_K200_DOWN) >= filterTime);
  K330_F_TRIG = (!K330 && K330_OLD);
  K330_F_FILTERED = (!K330 && (millis() - TIME_K330_DOWN) > delay_ind_print);
  K330_F_TRIG_DELAYED = (K330_F_FILTERED && !K330_F_FILTERED_OLD && (millis() >= StartUpTime) && (millis() - TIME_K330_DOWN) >= filterTime);
  Y25_F_TRIG = (!Y25 && Y25_OLD); 
  Y25_F_FILTERED = (!Y25 && (millis() - TIME_Y25_DOWN) > no_return);
  Y25_F_TRIG_DELAYED = (Y25_F_FILTERED && !Y25_F_FILTERED_OLD && (millis() >= StartUpTime));
  Y25_R_TRIG = (Y25 && !Y25_OLD && (millis() >= StartUpTime) && (millis() - TIME_Y25_DOWN) >= filterTime);

  if (K330_F_TRIG_DELAYED){
    NormalStop();
  }
  
  if (K330){
    TIME_K330_DOWN = millis();
  }
  
  if (K200_F_TRIG_DELAYED || K200_R_TRIG){
    RemoteShutdown();
  }
  
  if (K200){
    TIME_K200_DOWN = millis();
  }
 
  if (X21_F_TRIG_DELAYED || X21_R_TRIG ){
    X21Las();
  }
  
  if (X21){
    TIME_X21_DOWN = millis();
  }
    
  if (Y25_R_TRIG || Y25_F_TRIG_DELAYED){
    RadioBlink();
  }
  if (Y25){
    TIME_Y25_DOWN = millis();
  }

  SdPrint1.remove(0);
  SdPrint2.remove(0);
    
  X21_OLD = X21;
  Y25_OLD = Y25;
  K330_OLD = K330;
  K200_OLD = K200;
  X21_F_FILTERED_OLD = X21_F_FILTERED;
  Y25_F_FILTERED_OLD = Y25_F_FILTERED;
  K330_F_FILTERED_OLD = K330_F_FILTERED;
  K200_F_FILTERED_OLD = K200_F_FILTERED;
  init1 = 0;
}

void X21Las() {
    
    id++;
    
    if(X21_R_TRIG){
       SdPrint1 = int(id) +  String(" ; ") + long(TIME_X21_DOWN) + String(" ; ") + String("7") + String(" ; ")  + long(millis() - TIME_X21_DOWN) + String(" ; ");
       SdPrint2 = String(" X21 OFF ON, DOWNTIME: ") + long(millis() - TIME_X21_DOWN) + String(" TIME OF FALLING EDGE: ") + long(TIME_X21_DOWN);
    }
    if(X21_F_TRIG_DELAYED){
      SdPrint1 = int(id) +  String(" ; ") + long(TIME_X21_DOWN) + String(" ; ") + String("8") + String(" ; ")  + long(millis() - TIME_X21_DOWN) + String(" ; ");
      SdPrint2 = String(" X21 OFF MORE THAN 15s, TIME OF FALLING EDGE: ") + long(TIME_X21_DOWN);
    }
      
      
  SdWrite();
  
}


void NormalStop(){
     
    id++;
    
    if(Y72 || Y74){
       SdPrint1 = int(id) +  int(" ; ") + long(TIME_K330_DOWN) + int(" ; ") + int("0") + int(" ; ")  + long(millis() - TIME_K330_DOWN) + int(" ; ");
       SdPrint2 = String(" NORMAL STOP: ") + String(" WRONG STOP INDICATION Y72: ") + int(Y72) + int(" Y74: ") + int(Y74) + String(" TIME OF FALLING EDGE: ") + long(TIME_K330_DOWN);
    }

    if(!Y72 && !Y74){
       SdPrint1 = int(id) +  String(" ; ") + long(TIME_K330_DOWN) + String(" ; ") + int("1") + int(" ; ")  + long(millis() - TIME_K330_DOWN) + int(" ; ");
       SdPrint2 = String(" NORMAL STOP")  + String(" TIME OF FALLING EDGE: ") + long(TIME_K330_DOWN);
    }
  
            
  SdWrite();
  }

There is more code in between...

void SdWrite(){

  Serial.println(millis());

    myFile = SD.open("Log.txt", FILE_WRITE);                             // open the file.

    if(myFile){                                                         // if the file opened okay, write to it:
      Serial.print("Writing to Log.txt...");
      myFile.print(SdPrint1);
      myFile.println(SdPrint2);
      // close the file:
      myFile.close();
      Serial.println("done printing...");
      Serial.print(SdPrint1);
      Serial.println(SdPrint2);
  } 
  
    else {
    // if the file didn't open, print an error:
    Serial.println("error opening Log.txt");
  }
  Serial.println(millis());
}

Logger code.txt (8.9 KB)

I wouldn't use String.
I would use the F() macro.
I would use code tags.

I would use variable names that make sense :o

Thanx, I will try to learn how the F() macro works...
Yes regarding the tags... Thay will soon be there :slight_smile:

  X21_F_FILTERED = (!X21 && (millis() - TIME_X21_DOWN) > no_return);
  X21_F_TRIG_DELAYED = (X21_F_FILTERED && !X21_F_FILTERED_OLD && (millis() >= StartUpTime));
  X21_R_TRIG = (X21 && !X21_OLD && (millis() >= StartUpTime) && (millis() - TIME_X21_DOWN) >= filterTime);
  K200_F_TRIG = (!K200 && K200_OLD);
  K200_F_FILTERED = (!K200 && (millis() - TIME_K200_DOWN) > delay_ind_print);
  K200_F_TRIG_DELAYED = (K200_F_FILTERED && !K200_F_FILTERED_OLD && (millis() >= StartUpTime));
  K200_R_TRIG = (K200 && !K200_OLD && (millis() >= StartUpTime) && (millis() - TIME_K200_DOWN) >= filterTime);
  K330_F_TRIG = (!K330 && K330_OLD);
  K330_F_FILTERED = (!K330 && (millis() - TIME_K330_DOWN) > delay_ind_print);
  K330_F_TRIG_DELAYED = (K330_F_FILTERED && !K330_F_FILTERED_OLD && (millis() >= StartUpTime) && (millis() - TIME_K330_DOWN) >= filterTime);
  Y25_F_TRIG = (!Y25 && Y25_OLD);
  Y25_F_FILTERED = (!Y25 && (millis() - TIME_Y25_DOWN) > no_return);
  Y25_F_TRIG_DELAYED = (Y25_F_FILTERED && !Y25_F_FILTERED_OLD && (millis() >= StartUpTime));
  Y25_R_TRIG = (Y25 && !Y25_OLD && (millis() >= StartUpTime) && (millis() - TIME_Y25_DOWN) >= filterTime);

How many times do you REALLY need to call millis()?

And you should subtract to prevent rollover errors.