# Making a simple counter/timer

**URL:** <https://discourse.processing.org/t/making-a-simple-counter-timer/21923>\
**Category:** Coding Questions\
**Created:** [June 16, 2020, 3:34pm UTC](https://discourse.processing.org/t/making-a-simple-counter-timer/21923 "2020-06-16T15:34:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![AverageBiped](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/averagebiped/32/9266_2.png) [@AverageBiped](https://discourse.processing.org/u/AverageBiped)\
**Post date:** [June 16, 2020, 3:34pm UTC](https://discourse.processing.org/t/making-a-simple-counter-timer/21923/1 "2020-06-16T15:34:26Z")

</div>

I am trying to do something very simple and can’t figure out how. I want to implement a counter that counts from 0 to 12, printing out 1 number per second (so on run it will immediately print “0”, followed by “1” 1 second later, etc.). My code works but the problem is that it prints out many instances of the same number per second. When I reduce the frame rate to 1, it does the trick, but doesn’t print out the first “0”. Here is my code:

```auto
int x = 0;
int timer = 1000;
int nextTime = 0;

void setup() {
  size(100, 100);
}

void draw() {
  if (millis() > timer) {
    x += 1;
    timer += 1000;
  }
  println(x);
}

```

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [June 16, 2020, 4:09pm UTC](https://discourse.processing.org/t/making-a-simple-counter-timer/21923/2 "2020-06-16T16:09:28Z")

</div>

Hi @AverageBiped,

Welcome to the community! 🙂

Is this what you are looking for?

In my opinion you can either:

1. 
  - try to calculate the elapsed time using the **millis()** function:

```auto
int x = 0;
int ellapsedMillis = 1000;
int nextTime = 0;
int previousTime = 0;
void setup() {
  size(100, 100);
}

void draw() {
  if (millis()-previousTime>= ellapsedMillis) {
    previousTime = millis();
    println(x);
    x += 1;
  }
}

```

or you can use the internal clock from the computer using **seconds()**

```auto
int x = 0;
int nextTime = 0;
int previousTime = 0;
void setup() {
  size(100, 100);
}

void draw() {
  if (second()-previousTime>= 1) {
    previousTime = second();
    println(x);
    x += 1;
  }
}

```

I hope it helps!  
Best regards

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [June 16, 2020, 4:36pm UTC](https://discourse.processing.org/t/making-a-simple-counter-timer/21923/3 "2020-06-16T16:36:14Z")

</div>

Since the default frameRate is 60 per second, did you try putting in setup:

`frameRate(1);`

EDIT:  
Oh sorry, I see you DID try that…

🤓

---

<div class="post-metadata">

**Author:** ![AverageBiped](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/averagebiped/32/9266_2.png) [@AverageBiped](https://discourse.processing.org/u/AverageBiped)\
**Post date:** [June 16, 2020, 7:49pm UTC](https://discourse.processing.org/t/making-a-simple-counter-timer/21923/4 "2020-06-16T19:49:32Z")

</div>

Thank you Miguel! Both of these examples are extremely helpful. I think the first one is better suited to what I’m trying to do, and basically solves the problem. My only other question would be if there’s a way you know of to output the first “0” immediately upon running the program, rather than after 1000 ms have elapsed. Thank you again - I’m excited to be a part of this community and have access to such great knowledge from such helpful people. 🙂

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [June 16, 2020, 7:58pm UTC](https://discourse.processing.org/t/making-a-simple-counter-timer/21923/5 "2020-06-16T19:58:04Z")

</div>

You can set “previousTime” to -1000 instead of 0 when you set it up.

---

<div class="post-metadata">

**Author:** ![AverageBiped](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/averagebiped/32/9266_2.png) [@AverageBiped](https://discourse.processing.org/u/AverageBiped)\
**Post date:** [June 16, 2020, 8:09pm UTC](https://discourse.processing.org/t/making-a-simple-counter-timer/21923/6 "2020-06-16T20:09:54Z")

</div>

Ha, that’s brilliant jb4x. Thank you !!

---

<div class="post-metadata">

**Author:** ![AverageBiped](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/averagebiped/32/9266_2.png) [@AverageBiped](https://discourse.processing.org/u/AverageBiped)\
**Post date:** [June 16, 2020, 10:58pm UTC](https://discourse.processing.org/t/making-a-simple-counter-timer/21923/7 "2020-06-16T22:58:46Z")

</div>

Okay, another question: if I wanted to make this into a function that returns an int, how could I do that? The problem I’m running into is that by changing “println(x)” to “return x”, I make it so that I can’t include the following line “x += 1” because the code becomes unreachable. Do I have to change the architecture of the whole program?

```auto
int count() {
    if (millis() - previousTime >= ellapsedMs) {
      previousTime = millis();
      x = x % max;
      return x;
      x += 1;  
    }

```

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [June 17, 2020, 8:06am UTC](https://discourse.processing.org/t/making-a-simple-counter-timer/21923/8 "2020-06-17T08:06:24Z")

</div>

Hi @AverageBiped

What about using a temporary variable? So that you can modify tempX and return the x value afterward

```auto
int x = 0;
int tempX = 0;

int count() {
    if (millis() - previousTime >= ellapsedMs) {
      previousTime = millis();
      x = tempX % max;
      tempX += 1; 
      return x;
    }
}

```

Best regards
