Help making code more concise

Dear community,

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.

if(hour() == 9 && minute() == 01 && second() <= 2)
 {
 saveFrame("frames/screen-901##.jpg");
 }
if(hour() == 9 && minute() == 30 && second() <= 2)
 {
 saveFrame("frames/screen-930##.jpg");
 }

if(hour() == 10 && minute() == 01 && second() <= 2)
 {
 saveFrame("frames/screen-1001##.jpg");
 }
if(hour() == 10 && minute() == 30 && second() <= 2)
 {
 saveFrame("frames/screen-1030##.jpg");
 }
etc 
etc
...

Using Processing 3.5.3 on MacOS 10.14.4.

Humbly,

R8th

1 Like

Maybe this?

if(minute() == 01 && second() <= 2)
 {
 saveFrame("frames/screen-" + str(hour()) + "01##.jpg");
 }
if(minute() == 01 && second() <= 2)
 {
 saveFrame("frames/screen-" + str(hour()) + "30##.jpg");
 }

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).

Thanks again!

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(minute() == 01 && second() <= 2 || minute() == 30 && second() <= 2)
 {
 saveFrame("frames/screen-" + str(hour()) +  str(minute()) + "##.jpg");
 }

1 Like

Awesome. I don’t know anything about cron jobs. I’ll try this–thanks again!
r

1 Like

Hello,

This will do it for 0 and 30 minutes and repeat:

void draw()
  {
  if (minute()%30 == 0 && second()<=2) 
    println("Capturing.. ", 2-second());
  }

https://processing.org/reference/modulo.html

:slight_smile:

2 Likes

Hey There!

boolean clocked () {
    return minute()%30 == 0 && second()<=2;
}

This can be useful as if u like to use the timer more than once don’t have to write it all.

2 Likes

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");
}
3 Likes

wow…these are great everyone! sincere thanks! :metal::boom:

2 Likes