Understanding millis()

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

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);
    }
  }
}
1 Like

Hello,

This is your related post:

Read this again:

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

:)

1 Like