Seperating a function from mousePressed function?

Hi!

So, I’m at uni trying to figure out this code and how to get it to work how I want it. The project consists of an Arduino, which measures voltage going through a plant and also has a potentiometer. These inputs will control a midi output, but also a graphical output which is what I’m working with here.

The code includes a perlin noise background, which should be start over every time the mouse is pressed. Moreover, a randomly generated tree is present, however, this needs to only be generated, when a certain value is reached on the Arduino voltage (when the plant is touched). Therefore, I wish to seperate the background and the tree, so that they operate on different terms - the background should just automatically be generated, whereas the tree, should only be generated once the desired Arduino value is reached.

It’s worth mentioning, that this code is only the Processing code, as the Arduino part isn’t done.

ArrayList<Agent> agents = new ArrayList<Agent>();
float scale = 500;
float EPSILON = 0.01;
float zoff = 0;
float theta = radians(30);

void setup() {
  fullScreen(); //makes the sketch fit the screen, which looks more aesthetically pleasing
  colorMode(HSB, 360, 100, 100, 100); // HSB = hue, saturation, brightness, alpha (transparency)
  for (int i = 0; i < 2000; i++) {
    agents.add(new Agent());
  }
  background(60);
}

void mousePressed() {
  background(60);
  for (Agent agent : agents) {
    agent.reset();
  }
  zoff = random(100);
}

void draw() {
  noStroke();
  fill(60, 1); // 60 is background; lower = darker. 1 is line-length; lower = longer
  rect(0, 0, width, height);
  for (Agent agent : agents) {
    agent.update();
  }
  zoff += 0.001;
  translate(width/2, 600);
  drawBranch(100);
  /*  delay(50); /* if trees are constantly generated, not just when something happens, they will need a delay.
   However, this one slows everything down as apposed to just the tree-generating */
}

class Agent {
  PVector pos = new PVector();

  Agent() {
    pos = new PVector(random(width), random(height));
  }

  void reset() {
    pos = new PVector(random(width), random(height));
  }

  PVector gradient(PVector p) {
    float x = (noise(p.x+EPSILON, p.y, zoff)-noise(p.x-EPSILON, p.y, zoff))/(2*EPSILON);
    float y = (noise(p.x, p.y+EPSILON, zoff)-noise(p.x, p.y-EPSILON, zoff))/(2*EPSILON);
    return new PVector(x, y);
  }

  void update() {
    PVector grad = gradient(new PVector(pos.x/scale, pos.y/scale));
    pos.add(grad.copy().normalize().rotate(HALF_PI).mult(1));
    if (grad.mag() < 0.0001)reset();
    if (pos.x < 0 || pos.x > width || pos.y < 0 || pos.y > height) reset();
    if (random(1)<0.004)reset();
    fill(grad.mag()*36+200, 100, 100, 10);
    rect(pos.x, pos.y, 6, 6);
  }
}

void drawBranch(float len) {
  line(0, 0, 0, -len);
  translate(0, -len);
  if (len > 3) { // this if loop, which generates the trees, should only run when a criteria is met in Arduino (fx. specific volt, pot-meter position, etc.)
    pushMatrix();
    rotate(radians(random(20, 60)));
    drawBranch(len*random(0.5, 0.8));
    popMatrix();
    pushMatrix();
    rotate(radians(random(-20, -60)));
    drawBranch(len*random(0.5, 0.8));
    popMatrix();

    float a = random(0, 100);
    if (a < 30) {
      pushMatrix();
      rotate(-radians(random(-20, 20)));
      drawBranch(len*random(0.5, 0.8));
      popMatrix();
    }
  } else {
    fill(125, 90, 80); // hue, saturation, brightness
    stroke(#1a6910); // outline in dark green with hex no.
    ellipse(0, 0, 6, 12); // ellipse to be more leaf-shaped
    stroke(#4a3022); // tree-branches in brown with hex. no
    /* strokeWeight(2); /* I wanted to make the tree thicker, however not the outline of the leaves, which this does*/
  }
}

I hope my description of what I want to achieve is clear enough, but if it isn’t, please feel free to ask for clarifications!