# Help making code more concise

**URL:** <https://discourse.processing.org/t/help-making-code-more-concise/12734>\
**Category:** Beginners\
**Created:** [July 15, 2019, 10:43pm UTC](https://discourse.processing.org/t/help-making-code-more-concise/12734 "2019-07-15T22:43:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![r8th](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/r8th/32/5105_2.png) [@r8th](https://discourse.processing.org/u/r8th)\
**Post date:** [July 15, 2019, 10:43pm UTC](https://discourse.processing.org/t/help-making-code-more-concise/12734/1 "2019-07-15T22:43:42Z")

</div>

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.

```auto
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

---

<div class="post-metadata">

**Author:** ![madscientist](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@madscientist](https://discourse.processing.org/u/madscientist)\
**Post date:** [July 15, 2019, 10:54pm UTC](https://discourse.processing.org/t/help-making-code-more-concise/12734/2 "2019-07-15T22:54:11Z")

</div>

Maybe this?

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

```

---

<div class="post-metadata">

**Author:** ![r8th](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/r8th/32/5105_2.png) [@r8th](https://discourse.processing.org/u/r8th)\
**Post date:** [July 15, 2019, 11:03pm UTC](https://discourse.processing.org/t/help-making-code-more-concise/12734/3 "2019-07-15T23:03:43Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![madscientist](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@madscientist](https://discourse.processing.org/u/madscientist)\
**Post date:** [July 15, 2019, 11:13pm UTC](https://discourse.processing.org/t/help-making-code-more-concise/12734/4 "2019-07-15T23:13:24Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![r8th](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/r8th/32/5105_2.png) [@r8th](https://discourse.processing.org/u/r8th)\
**Post date:** [July 15, 2019, 11:31pm UTC](https://discourse.processing.org/t/help-making-code-more-concise/12734/5 "2019-07-15T23:31:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 16, 2019, 2:25am UTC](https://discourse.processing.org/t/help-making-code-more-concise/12734/6 "2019-07-16T02:25:07Z")

</div>

Hello,

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

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

```

[https://processing.org/reference/modulo.html](https://processing.org/reference/modulo.html)

🙂

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [July 16, 2019, 7:47pm UTC](https://discourse.processing.org/t/help-making-code-more-concise/12734/7 "2019-07-16T19:47:49Z")

</div>

Hey There!

```auto
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.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [July 17, 2019, 4:08am UTC](https://discourse.processing.org/t/help-making-code-more-concise/12734/8 "2019-07-17T04:08:10Z")

</div>

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.

```auto
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");
}

```

---

<div class="post-metadata">

**Author:** ![r8th](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/r8th/32/5105_2.png) [@r8th](https://discourse.processing.org/u/r8th)\
**Post date:** [July 17, 2019, 2:56pm UTC](https://discourse.processing.org/t/help-making-code-more-concise/12734/9 "2019-07-17T14:56:57Z")

</div>

wow…these are great everyone! sincere thanks! 🤘💥
