Time-base Pattern Help

Hello all, we are currently working on a time-based pattern project for class but i’ve manage to hit a wall working on my code. I am new to working in the program so i’m a tad confused.

  1. I would like to make it to where my animation starts at the initial press of the mouse then the ellipse changes/transitions color if the mouse button is held.
  2. I would like to have more than just one ellipse traveling on my sin wave!

I greatly appreciate any help or explanations given!

float rad;
float xOrigin, yOrigin;
float yPos;
boolean toLeft;

void setup() {
  size(500, 500);
  rad = 50;
  yOrigin = height/2;
  toLeft = false;

void draw() {

  yPos =cos(frameCount * 0.15) * rad;
  ellipse (xOrigin, yOrigin + yPos, 20, 20);
  if (toLeft==false) {
    xOrigin = xOrigin + 5;
    if (xOrigin > 500) {  
      toLeft = true;

  else {
    xOrigin = xOrigin - 5; 
    if (xOrigin < 0) {
      toLeft = false;

One way is to use mousePressed() and loop() / noLoop();

See the noLoop example sketch:

It sounds like you want the opposite – set noLoop by default, set loop when pressed, and set noLoop again when released.