I am trying to capture 2 seconds-worth of frames, every 30 minutes using a webcam. The following code works, but I feel that there must be a more concise way to write this. I’ve searched but cannot find anything on this topic.
Oh ya, of course! I guess you don’t need the hours in there—makes sense, thanks!
More specifically, I was wondering whether I need essentially 48 conditional statements, or whether there is a more concise way to structure that aspect of code (ie: if I want to capture frames for every half-hour for 24 hours).
That will do it for every half hour for 24 hours.
if you’re are familiar with cron jobs, this is similar. This essentially checks if the minute is 01 or 30, regardless of the hour. Then it justs grabs the current hour and saves the frame. Thinking about it, you could actually make it even more concise, like so:
If you want clock time, then the solutions given are the way to go. However if you want the first capture to begin once the sketch starts, then you might want to use millis() instead.
int duration = 2*1000; // sec × milli
int cycle = 30*60*1000; // min × sec × milli
int now = millis();
if(now%cycle < duration){ // if < 2 sec into any half hour
saveframe("frame" + str(now) + ".png");
}