# Understanding millis()

**URL:** https://discourse.processing.org/t/understanding-millis/19964
**Category:** Coding Questions
**Tags:** homework
**Created:** [April 20, 2020, 3:54pm UTC](https://discourse.processing.org/t/understanding-millis/19964 "2020-04-20T15:54:18Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![14mmss](https://avatars.discourse-cdn.com/v4/letter/1/d07c76/32.png) [@14mmss](https://discourse.processing.org/u/14mmss)
#### Post date: [April 20, 2020, 3:54pm UTC](https://discourse.processing.org/t/understanding-millis/19964/1 "2020-04-20T15:54:18Z")

</div>

hello,  
I never seem to understand how the mills() function works, I am working on the variation simple stimulus assignment now and the time only continues counting if I keep pressing space, where should I put my initial time? the square is supposed to change colors every multiple of 5

```auto
int r;
int time;
int m;
int startTime;

void setup() {
  size(800, 800);
  textSize(46);
  text("Press SPACE to begin", 180, 200);
  textSize(32);
  text("Instructions:", 100, 600);
  text("When the square becomes blue, press B", 100, 650);
  text("When the square becomes red, press R", 100, 700);
}

void draw() {
  time = millis() - startTime;
}

void keyPressed() {
  if (key == ' ') {
    text("time" + (time), 100, 200);
    r = int(random(0, 2)); // selects a number between 1 and 2
    startTime = millis();
    startExperiment();
  }
}

void startExperiment() {
  m = millis() - time;
  background(255, 255, 255);
  fill(0);
  rectMode(CENTER);
  rect(400, 400, 400, 300);
  if (m % 5 == 0) {
    if (r == 0) {
      fill(255, 0, 0);
      rectMode(CENTER);
      rect(400, 400, 400, 300);
    }
    if (r == 1) { // 1 makes the rectangle blue
      fill(0, 0, 255);
      rectMode(CENTER);
      rect(400, 400, 400, 300);
    }
  }
}

```

---

<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: [April 20, 2020, 4:10pm UTC](https://discourse.processing.org/t/understanding-millis/19964/2 "2020-04-20T16:10:46Z")

</div>

Hello,

This is your related post:

> [@Reaction time experiment](https://discourse.processing.org/t/help-with-reaction-time-experiment/19813/5):
>
> thank you! I realized that. Now I’m having a problem with having the calculation of the reaction time work. It should show the time that it takes for the user to press space since the square turned red. Could you help? boolean startTimer = false; int startingTimer; int CONSTANT = 1000; int r; int timeOne; int timeTwo; int totalTime; int reactionTime; boolean reaction; void setup() { // setting up the initial screen size(800, 800); textSize(36); text("Reaction time experiment", 180, 200…

> [@14mmss](#):
>
> if (m % 5 == 0)

Read this again:

> [@Reaction time experiment](https://discourse.processing.org/t/help-with-reaction-time-experiment/19813/4):
>
> This condition may not ever be met because millis() is quietly updating in the background and you are only checking it every draw cycle which is 1/60 of a sec (default framerate is 60 fps). The chance that they are exactly == at the instance you check is not likely. Other options are \>, \<, \>=, \<=, != … Pick one that makes sense and try it. Reference: [https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html)slight_smile

Use println() statements (outputs to console) in your code to see what the variables are doing:

> **[println() / Reference](https://processing.org/reference/println_.html)**
>
> The println() function writes to the console area, the black rectangle at the bottom of the Processing environment. This function is often helpful for looking at the data a program is producing…

`:)`
