# How can I fix my Timer Bug?

**URL:** <https://discourse.processing.org/t/how-can-i-fix-my-timer-bug/27044>\
**Category:** Coding Questions\
**Created:** [January 14, 2021, 12:08am UTC](https://discourse.processing.org/t/how-can-i-fix-my-timer-bug/27044 "2021-01-14T00:08:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Radioaktiv](https://avatars.discourse-cdn.com/v4/letter/r/bbe5ce/32.png) [@Radioaktiv](https://discourse.processing.org/u/Radioaktiv)\
**Post date:** [January 14, 2021, 12:08am UTC](https://discourse.processing.org/t/how-can-i-fix-my-timer-bug/27044/1 "2021-01-14T00:08:42Z")

</div>

the first second the timer does not work. is there any way to fix it? also is there a way to make it mor compact?

```auto
String milliseconds;
int second;
int timer;
int index;

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

void draw() {
  milliseconds = str(millis()); // Puts millisecods in a String
  second = int(milliseconds.charAt(index)); // Always looks at the three-digit position from behind. The digit with the number of seconds
  timer = second % 2; // looks if there is a leftover in the division of second / 2. The acctual timer
 
  if (timer == 1) { // if there is a leftover, the background is turned to white
    background(0);
  }

  if (timer == 0) { // if there isnt a leftover, the background is turned to black
    background(255);
  }

  if (milliseconds.length() == 4) { // whenever the length of the string changes, the position to be looked at is also changed, so that the second digit is always looked at
    index = 0;
  }

  if (milliseconds.length() == 5) {
    index = 1;
  }

  if (milliseconds.length() == 6) {
    index = 2;
  }

  if (milliseconds.length() == 7) {
    index = 3;
  }

  if (milliseconds.length() == 8) {
    index = 4;
  }

  if (milliseconds.length() == 9) {
    index = 5;
  }

  if (milliseconds.length() == 10) {
    index = 6;
  }

  if (milliseconds.length() == 11) {
    index = 7;
  }

  if (milliseconds.length() == 12) {
    index = 8;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)\
**Post date:** [January 14, 2021, 3:03am UTC](https://discourse.processing.org/t/how-can-i-fix-my-timer-bug/27044/2 "2021-01-14T03:03:38Z")

</div>

Maybe I misunderstand, what you try to do, but why don’t you do:

leftover = int(seconds/2) - (seconds/2)

If leftover is not zero, then white, if it is zero, then black.

You shouldn’t check for exactly leftover == 0, because there might be rounding errors.

---

<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:** [January 14, 2021, 9:50am UTC](https://discourse.processing.org/t/how-can-i-fix-my-timer-bug/27044/3 "2021-01-14T09:50:16Z")

</div>

Hello,

Consider this:

```auto
println(millis()/1000); // integer math

```

You can use:

> println()

statements in your code to see the state of the variables.  
It helps.

`:)`
