how to summarize code

Hello dear community

It happens that I have a code that works well but is extensive, and I fear that when I need to do something else, I will run out of memory.

So, my query is related to the schedules programmed in the void loop, which are repetitive, developing the same task at all hours of the day.

In another Topic they told me about the millis function, since I only need the routine to rest every 15 minutes, continuously, but honestly, I can't approve the programming using millis to do this same routine.

To the more experienced I humbly ask for help please.

Thank you very much!

P.S.: I enclose code in .txt since the page limited me by length of the text.

cod_v8.txt (16 KB)

Cod_V9.txt (14.8 KB)

The attachment failed.

Thank you very much, now it is attached.

Any help I will appreciate it!

What is the reason for the text to be cluttered with hundreds of backslashes ("")? It looks like HTML. Did you cut and paste it from a web page? Please post from the actual IDE window.

 if ( fecha.hour () == 16 && fecha.minute () == 45 ) \{\
    if (rut17 == true ) \{\
      digitalWrite (rele, LOW);\
      Serial.println ("Rutina 17 terminada");\
      apagado ();\
      rut17 = false;\
    \}\
  \}\
\
  \

You will save a considerable amount of dynamic memory (ram) by using the F() macro in the Serial.println statements

      Serial.println("Rutina 1 terminada"); //original code
      
      Serial.println(F("Rutina 1 terminada")); //F() macro surrounding quoted text stores text in flash memory instead of ram

There is no need to test for each hour of the day, you are doing the exact same thing for every hour, the only difference being the text you are sending to Serial, where you are printing the hour (actually the hour + 1).

Is the intent of the program to turn the relay on and call routina1() the at the start of every hour? Then after 45 minutes, turn the relay off and call apagado().

If so, there are ways to simplify what you have.

aarg:
What is the reason for the text to be cluttered with hundreds of backslashes ("")? It looks like HTML.

It's RTF. Save it as "foo.rtf" and open it with word or wordpad.

cessariuss:
So, my query is related to the schedules programmed in the void loop, which are repetitive, developing the same task at all hours of the day.

"summarise" is not the right word - it means to create a short statement that lists the main points and leaves out the details. A program needs all its details. I think you just mean "shorten"

If you have the same chunks of code in several places then that piece of code should be put into a function and the function can be called from several places without needing to repeat the code. Have a look at Planning and Implementing a Program

...R

Dude, all of your cases do the same thing. Why are you even checking the hour?

aarg:
What is the reason for the text to be cluttered with hundreds of backslashes ("")? It looks like HTML. Did you cut and paste it from a web page? Please post from the actual IDE window.

 if ( fecha.hour () == 16 && fecha.minute () == 45 ) \{\

if (rut17 == true ) {
      digitalWrite (rele, LOW);
      Serial.println ("Rutina 17 terminada");
      apagado ();
      rut17 = false;
    }
  }

  \

Hello,

sorry, it was a file save error.

now if it is correct, upload file cod_v9.txt

cod_v9.txt is incomplete

TheMemberFormerlyKnownAsAWOL:
cod_v9.txt is incomplete

dear, download it and it looks complete.

Can you try again and check if it downloads completely?

Look at reply #8. Why do you do the same thing 24 times?

Worse, after the first time, I don't think the steppers will ever move again. What are you trying to do?

david_2018:
You will save a considerable amount of dynamic memory (ram) by using the F() macro in the Serial.println statements

      Serial.println("Rutina 1 terminada"); //original code

Serial.println(F("Rutina 1 terminada")); //F() macro surrounding quoted text stores text in flash memory instead of ram




There is no need to test for each hour of the day, you are doing the exact same thing for every hour, the only difference being the text you are sending to Serial, where you are printing the hour (actually the hour + 1).

Dear, thanks for the data (F (. I will implement it.

Regarding the code, that I need, adjust in such a way that the routine works every 45 minutes and rests 15.
What are you suggesting?

cattledog:
Is the intent of the program to turn the relay on and call routina1() the at the start of every hour? Then after 45 minutes, turn the relay off and call apagado().

If so, there are ways to simplify what you have.

Exactly, I need it to work every 45 minutes with intervals of 15 minutes for rest.

Quote from: cattledog on Sep 13, 2020, 07:41 pm
Is the intent of the program to turn the relay on and call routina1() the at the start of every hour? Then after 45 minutes, turn the relay off and call apagado().

If so, there are ways to simplify what you have.
Exactly, I need it to work every 45 minutes with intervals of 15 minutes for rest.

I think that is this is the case, you can replace all the boolean control variables and multiple readings with standard state change tests. I have not reviewed any of the motor code so check out @wildbill's warning.

void loop() {

  DateTime fecha = RTC.now();
  static byte lastHour = fecha.hour();
  static byte lastMinute = fecha.minute();
  
  if (fecha.hour() != lastHour)
  {
    lastHour = fecha.hour();
    digitalWrite (rele, HIGH);
    Serial.print("Inicia Routina ");
    Serial.println(fecha.hour() + 1);
    rutina1();
  }
  //check rele state to ensure that at program startup, the on routine proceeds the off routine
  if (fecha.minute() != lastMinute and fecha.minute() == 45 and digitalRead(rele)==HIGH)
  {
    lastMinute = fecha.minute();
    digitalWrite(rele, LOW);
    Serial.print("Routina ");
    Serial.print(fecha.hour() + 1);
    Serial.println("terminada");
    apagado();
  }
}

PaulMurrayCbr:
Dude, all of your cases do the same thing. Why are you even checking the hour?

Dear, check the time because it was the way I found that I did the routine for 45 minutes with 15 minutes of rest.

What do you suggest to optimize the code?

You don't seem to understand that there is no reason to check the hour. Your code starts the motor at 1 minute past the hour, and stops the motor at 45 minutes past the hour, for every hour. You do not need to know the hour to test for 1 minute past the hour, just check to see if the minute ==1, the same for 45 minutes past the hour, just check for minute ==45. The only reason you would need to check the hour is if you wanted the motor to run at different times during a specific hour.

david_2018:
You don't seem to understand that there is no reason to check the hour. Your code starts the motor at 1 minute past the hour, and stops the motor at 45 minutes past the hour, for every hour. You do not need to know the hour to test for 1 minute past the hour, just check to see if the minute ==1, the same for 45 minutes past the hour, just check for minute ==45. The only reason you would need to check the hour is if you wanted the motor to run at different times during a specific hour.

That was the only way I could find him to work for 45 minutes straight with 15 off.

Surely it should be simpler, so I ask for help or guidance.