On startup, start loop at beginning of next minute

I am reading multiple sensors with the arduino and logging that data to an SD card. I do not have a real time clock, so to set the time on startup I send a message over the serial port from the unix terminal. However, I do not want the main loop to start reading the sensors until the start of the next minute once the time value is received by the arduino (i.e., if the time sent over serial port is 11:41:25, delay the start of the loop until 11:42:00).

I've looked around the forum for a while to find an answer to this, but cannot seem to get a solution; it's entirely possible I am just not searching with the correct terminology. Any ideas or directions would be appreciated. Thank you.
-Dave

Perhaps if you could provide the code you have now, the rest of the solution might present itself.

Seems fairly straight-forward: if the seconds value received isn't 0, then subtract it from 60 to get the number of seconds before starting. Multiply that number by 1000 to get milliseconds and call delay() with that value before returning from setup().

You might want to put that 'wait until' code in setup() which runs before loop() can start.

Why not have main loop sit & wait for the time message, hang out until next minute is reached, do the sensor stuff, then go back to waiting for a time message, or the next minute to occur as determined by reading millis().

dmarvs:
I am reading multiple sensors with the arduino and logging that data to an SD card. I do not have a real time clock, so to set the time on startup I send a message over the serial port from the unix terminal. However, I do not want the main loop to start reading the sensors until the start of the next minute once the time value is received by the arduino (i.e., if the time sent over serial port is 11:41:25, delay the start of the loop until 11:42:00).

I've looked around the forum for a while to find an answer to this, but cannot seem to get a solution; it's entirely possible I am just not searching with the correct terminology. Any ideas or directions would be appreciated. Thank you.
-Dave

Really easy........
Sloppy way to do it...

  1. Read minutes. (1)

  2. Read minutes (2)

  3. if minutes1 = minutes2 goto 2.

  4. dropdown into main loop

Thank you for all the suggestions. I ended up with:

void setup () {

while (incorrect time) {
  wait for time to be sent over serial port;}

continue with setup

if (second() != 0) {
  delay((60 - second()) * 1000);}

}

void loop () {

read sensors and log data

}

Thank you Morris, I had implemented that same idea, which didn't work because I forgot about the conversion to millis to properly delay.

You want to set up a loop to get the time set and note that by the time your Arduino has that it will be later than that by whole milliseconds. :wink: And if that matters you can add a compensation value.

Before that runs, you might as well get all non-timeset setup done.

When you have timeset, save the value of millis() (say to an unsigned long named 'mark') and note how many seconds from timeset to the next minute. Then,

while ( millis() - mark < milliseconds-to-go );