Running a loop with a condition

Hey, I can’t seem to solved the proper way to write my loop with a condition now I get the basics but I need to run a loop in which runs 24 times, but only runs once every hour. Therefore i need it to run once in an hour then ignore the loop then go back to the loop. I can’t really think of how to execute this via code, even a rough example could help me. A simple starting point.

You don't need a loop to do that. Study the Blink without delay example ;)

TLuck: I can't really think of how to execute this via code, even a rough example could help me. A simple starting point.

#define INTERVAL 3600

void setup() {
  Serial.begin(9600);
  Serial.println("Good night and good luck!");
}

unsigned long seconds, lastMillis;

void loop() {
  if (millis()-lastMillis>=1000)
  {
    lastMillis+=1000;
    seconds++;
    if (seconds%INTERVAL==0)
    {
      Serial.print(INTERVAL);Serial.println(" seconds are over");
      // do something once per interval 
    }
  }
}

Alternately this should also work:

#define INTERVAL 3600000

void setup() {
  // Put setup code here
}


void loop() {
  int i;

  // Wait one hour
  delay(INTERVAL);

  // Do something 24 times
  for (i = 0; i < 24; i++) {
      // do something
  }
}

I was more focused on how to time it, seen as I have access to an RTC. I am now abusing the time functions built in. Now I just for stepping stones tell me if you think these if statements will work to document data on an hourly basis (obviously on exactly the hour at 0 minutes and what not) and on a daily basis( once again at exactly midnight). Im not too concerned yet with the loop to display the arrays.

#include <Time.h>  
#include <Wire.h>  
#include <DS1307RTC.h>
int x;
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  x = hour();
  
  // The hourly base data storing.
  if(( minute() == 0) && (second())){
   //Store the data 
   //array[x] x being based off the hour
  }
   if(( hour() == 0 ) && (minute() == 00) && ( second() == 0 )){
    //Send the email. 
    //loop to make sure i send all the data from the arrays
   }
}

Why are you using the value of second() inconsistently ?

Why diddle around checking minute() and second() ? Why not just save the value to the array when hour() changes ?

With your method, if your code become more complex, you risk missing a second sometimes, so checking if it's exactly midnight could possibly fail. One solution is to check if the hour changed.

Exemple:

int previousHour;

void setup()
{
    previousHour = hour();
}

void loop()
{
    if ( hour() != previousHour )
    {
        previousHour = hour();

        doSomethingOnceEveryHour();

        if ( hour() == 0 )
        {
            doSomethingOnceAtMidnight();
        }
    }
}

TLuck: I was more focused on how to time it, seen as I have access to an RTC.

RTC? Oh! :o

Several days later, after your initial posting you tell about additional hardware connected to your Arduino.

In your original question not even the type of board was mentioned. No circuit schematics. No code. Not a word about additional RTC hardware connected.

For what you have asked, you'd got an answer by me in reply #2, I think.

I don't like it very much, if all the preconditions and assumptions, are changed after I posted a reply to a question.

You could have posted about the RTC hardware in your initial question. You could have posted a circuit schematics or desicription of the circuit. You could have posted demonstration code. But you didn't.

My answer and the example code I had posted would have been different if I had known about the RTC. But now it is as it is.

I'm out.

Bye.

One solution is to check if the hour changed.

This is a good idea, especially since I have tested i similar program with different math equations with second() and minutes(). Now I'm very much considering your idea but i want to know your input to the idea of simply storing second() = seconds; inside a variable same with any other time function used by the RTC would this fix that minor error? If not ill be strongly leading towards using your suggested code.

Also, could I not use the following code to replace the hour() == 0 if statement.

if (day() != previousday)
        {
            doSomethingOnceAtMidnight();
        }

Yes, as long as the value of previousday is what it’s name suggests.

If you have a DS3231 it can be configured to generate alarms for what you're asking for.