Start functions displaced in time?

Hey,

I’m just starting processing at university, so I feel a little lost in my code. I want to start my first void line1 and then, after some seconds, start the second void line2 too. I know that it should be possible to make if work with a if condition in draw somehow, but I just don’t get it?!

It would be grate if you could help me please. Thanks a lot!

int timePassed = 1;
int timeSpan = 100; //Zeitspanner in Timer 


int anzahl = 1;

float BOLD = 0.5;

int RAD_MIN_STEP = 1;
int RAD_MAX_STEP = 20;
int LIM = 50;

PVector prevPos = new PVector();
PVector currPos = new PVector(prevPos.x, prevPos.y);

float ang, rad, dir;
boolean paused;

void setup() {
  size(1080, 960);
  smooth(2);
  frameRate(60);

  strokeCap(ROUND);
  strokeJoin(ROUND);
  strokeWeight(BOLD);

  restart();
}

void draw () {
  linie1();

  //if ((millis() - timePassed) > timeSpan) {
  //  linie1();
  //  println ("tick");
  //  timePassed = millis ();
  
  if count>10{
    linie2();
  }
}

void linie1() {

  ang += dir/rad;
  currPos.add(cos(ang) * rad, sin(ang) * rad);

  int x = round(currPos.x), y = round(currPos.y);

  if (x < 0 || x >= width || y < 0 || y >= height) {
    bounce();

    x = constrain(x, -LIM, width  + LIM);
    y = constrain(y, -LIM, height + LIM);

    currPos.set(x, y);
  } else if (get(x, y) != 255)  bounce();

  if ((millis() - timePassed) > timeSpan) {
    Linie2();
    timePassed = millis ();
  }

  line(prevPos.x, prevPos.y, currPos.x, currPos.y);
  prevPos.set(currPos);
}

void Linie2() {
  ang += dir/rad;
  currPos.add(cos(ang) * rad, sin(ang) * rad);

  int x = round(currPos.x), y = round(currPos.y);

  if (x < 0 || x >= width || y < 0 || y >= height) {
    bounce();

    x = constrain(x, -LIM, width  + LIM);
    y = constrain(y, -LIM, height + LIM);

    currPos.set(x, y);
  } else if (get(x, y) != 255)  bounce();

  if ((millis() - timePassed) > timeSpan) {
    stroke (random (0, 255), random (0, 255), random (0, 255));
    //println ("tick");
    timePassed = millis ();
  }

  line(prevPos.x, prevPos.y, currPos.x, currPos.y);
  prevPos.set(currPos);
}


void mousePressed() {
  if (mouseButton == LEFT)
    if (paused ^= true)  noLoop();
    else                 loop();
  else restart();
}

void bounce() {
  rad = random(RAD_MIN_STEP, RAD_MAX_STEP);
  //ang += PI * (dir *= -1);
  ang += PI * (dir *= -1);
}

void restart() {
  background(0);
  //stroke(random(COLORS), 1, 1);

  prevPos.set(width>>1, height>>1);
  //prevPos.set(width>>2, height>>2);
  currPos.set(prevPos);

  //rad = dir = 1;
  rad = dir = 80;
}
1 Like

i can not understand you timer concept,
i see:

draw
  line1
     timer call
         line2
            timer change color

-a- if you want 2 timer you need two sets of variables
long timePassed1, timePassed2;
could be also two different timings
int timeSpan1 = 100,timeSpan2 = 100;
and to see anything pls. ( for test ) use 3000 == 3sec.
add a println(“timerx”); for console diagnostic.

-b- if you want 2 lines you need two sets of variables
PVector prevPos1, currPos1,prevPos2, currPos2;

because i not see any second line drawn? or i misunderstand what you want to do?

here a example:

long startT, delayT=3000;

void setup() {
  size(200, 200); 
  startT = millis();
  background(200, 200, 0);
  stroke(0, 0, 200);
}

void draw() {
  translate(width/2, height/2);
  line(10, 10, random(10, 40), random(10, 40));
  if ( myStartUpTimer(false) ) line(50, 50, random(50, 80), random(50, 80));
}

boolean myStartUpTimer(boolean restart) {
  boolean timeout = false;
  if ( millis() - startT > delayT ) { 
    timeout = true;
    if (restart) startT = millis();
  }
  return timeout;
}


1 Like

Hey, thank you for your help. I know the code I’m working with is a mass. Well what I wanted to do:

I made a void function called void linie1. The I copy it to another function and called these one void linie2. Then I wanted to invoke them in draw and make a if-condition to start the second line some delay to the first (maybe 5 seconds). Is there not a if condition with something like:

if count >5 {
  drawline2();
)
?

i was thinking that is exactly what i show you in my example
ok, little rewrite

long startT, delayT=5000;

void setup() {
  size(200, 200); 
  startT = millis();
  background(200, 200, 0);
  stroke(0, 0, 200);
}

void draw() {
  translate(width/2, height/2);
  line1();
  if ( myStartUpTimer(false) ) line2();
}


void line1() {
  int posX=10, posY=10,w=30;
  line(posX, posY, random(posX, posX+w), random(posY, posY+w));  
}

void line2() {
  int posX=50, posY=50,w=30;
  line(posX, posY, random(posX, posX+w), random(posY, posY+w));  
}


boolean myStartUpTimer(boolean restart) {
  boolean timeout = false;
  if ( millis() - startT > delayT ) { 
    timeout = true;
    if (restart) startT = millis();
  }
  return timeout;
}

for copy your code into this still needed that double line variables
so you have 2 separate lines,

  • line1 blue,
  • line2 start later, and change color by a extra color timer
    ( sorry, that is what i interpreted from you above code )
test
long startT, delayT=3000;
long colstartT, coldelayT=2000;

float BOLD = 2;
int RAD_MIN_STEP = 1;
int RAD_MAX_STEP = 20;
int LIM = 50;
PVector prevPos = new PVector();
PVector currPos = new PVector(prevPos.x, prevPos.y);
float ang, rad, dir;
color thisstroke; 
  //line2
PVector prevPos2 = new PVector();
PVector currPos2 = new PVector(prevPos2.x, prevPos2.y);
float ang2, rad2, dir2;
color thisstroke2; 

void setup() {
  size(1080, 960);
  thisstroke = color(0, 0, 200); 
  thisstroke2 = color(200, 0, 200); 
  frameRate(10);
  startT = millis();
  strokeCap(ROUND);
  strokeJoin(ROUND);
  strokeWeight(BOLD);
  restart();
}


void draw() {
  line1();
  if ( myStartUpTimer(false) ) line2();
}


void line1() {
  stroke(thisstroke);
  ang += dir/rad;
  currPos.add(cos(ang) * rad, sin(ang) * rad);
  int x = round(currPos.x), y = round(currPos.y);
  if (x < 0 || x >= width || y < 0 || y >= height) {
    bounce();
    x = constrain(x, -LIM, width  + LIM);
    y = constrain(y, -LIM, height + LIM);
    currPos.set(x, y);
  } else if (get(x, y) != 255) bounce();
  line(prevPos.x, prevPos.y, currPos.x, currPos.y);
  prevPos.set(currPos);
}

void bounce() {
  rad = random(RAD_MIN_STEP, RAD_MAX_STEP);
  ang += PI * (dir *= -1);
}

  //line2
void line2() {
  if ( myColTimer() ) thisstroke2 = color(random (0, 255), random (0, 255), random (0, 255));
  stroke(thisstroke2);
  ang2 += dir2/rad2;
  currPos2.add(cos(ang2) * rad2, sin(ang2) * rad2);
  int x = round(currPos2.x), y = round(currPos2.y);
  if (x < 0 || x >= width || y < 0 || y >= height) {
    bounce2();
    x = constrain(x, -LIM, width  + LIM);
    y = constrain(y, -LIM, height + LIM);
    currPos2.set(x, y);
  } else if (get(x, y) != 255) bounce2();
  line(prevPos2.x, prevPos2.y, currPos2.x, currPos2.y);
  prevPos2.set(currPos2);
}

void bounce2() {
  rad2 = random(RAD_MIN_STEP, RAD_MAX_STEP);
  ang2 += PI * (dir2 *= -1);
}

void restart() {
  background(0);
  prevPos.set(width>>1, height>>1);
  currPos.set(prevPos);
  rad = dir = 80;
  //line2
  prevPos2.set(width>>1, height>>1);
  currPos2.set(prevPos2);
  rad2 = dir2 = 80;
}



boolean myStartUpTimer(boolean restart) {
  boolean timeout = false;
  if ( millis() - startT > delayT ) { 
    timeout = true;
    if (restart) startT = millis();
    //println("timeout");
  }
  return timeout;
}

  //line2 only
boolean myColTimer() {
  boolean timeout = false;
  if ( millis() - colstartT > coldelayT ) { 
    timeout = true;
    colstartT = millis();
  }
  return timeout;
}



Hey, thank you so much for your help! Unfortunately it did not wok out and I don’t know why? but anyway, thanks for your time :slight_smile:

This is the whole code:

// Der Timer der zweiten Linie funktioniert nicht!..?
long startT, delayT=5000;

int timePassed = 1; //Timer Farbwechsel Linie
int timeSpan = 100; 

int anzahl = 1;
float BOLD = 0.4;

int RAD_MIN_STEP = 1;
int RAD_MAX_STEP = 20;
int LIM = 50;

PVector prevPos = new PVector();
PVector currPos = new PVector(prevPos.x, prevPos.y);

float ang, rad, dir;
boolean paused;

void setup() {
  size(1080, 1920);
  
  //millis Befehl funktioniert nicht!..?
  startT = millis();
  smooth(2);
  frameRate(60);

  strokeCap(ROUND);
  strokeJoin(ROUND);
  strokeWeight(BOLD);


  restart();
}

void draw() {
  line1();

  //Der Aufruf der zweiten Linie funktioniert nicht!..?
  if (myStartUpTimer(false)) line2();
}


void line1() {

  ang += dir/rad;
  currPos.add(cos(ang) * rad, sin(ang) * rad);

  int x = round(currPos.x), y = round(currPos.y);

  if (x < 0 || x >= width || y < 0 || y >= height) {
    bounce();

    x = constrain(x, -LIM, width  + LIM);
    y = constrain(y, -LIM, height + LIM);

    currPos.set(x, y);
  } else if (get(x, y) != 255)  bounce();

  if ((millis() - timePassed) > timeSpan) {
    line2();
    timePassed = millis ();
  }

  line(prevPos.x, prevPos.y, currPos.x, currPos.y);
  prevPos.set(currPos);
}

void line2() {
  ang += dir/rad;
  currPos.add(cos(ang) * rad, sin(ang) * rad);

  int x = round(currPos.x), y = round(currPos.y);

  if (x < 0 || x >= width || y < 0 || y >= height) {
    bounce();

    x = constrain(x, -LIM, width  + LIM);
    y = constrain(y, -LIM, height + LIM);

    currPos.set(x, y);
  } else if (get(x, y) != 255)  bounce();

  if ((millis() - timePassed) > timeSpan) {
    stroke (random (0, 255), random (0, 255), random (0, 255));
    //println ("tick");
    timePassed = millis ();
  }

  line(prevPos.x, prevPos.y, currPos.x, currPos.y);
  prevPos.set(currPos);
}

//Boolean funktioniert nicht!..?

boolean myStartUpTimer(boolean restart) {
  boolean timeout = false;
  if ( millis() - startT > delayT ) { 
    timeout = true;
    if (restart) startT = millis();
  }
  return timeout;
}

void mousePressed() {
  if (mouseButton == LEFT)
    if (paused ^= true)  noLoop();
    else                 loop();
  else restart();
}

void bounce() {
  rad = random(RAD_MIN_STEP, RAD_MAX_STEP);
  //ang += PI * (dir *= -1);
  ang += PI * (dir *= -1);
}

void restart() {
  background(0);

  prevPos.set(width>>1, height>>1);
  currPos.set(prevPos);

  //rad = dir = 1;
  rad = dir = 80;
}

here / in a smaller window / with my above provided test code, it looks like
2018-12-19_07-30-28_snap

and the function / i guessed from your code / i described above

your comments that the implementation of my timercode not work
tell me that you not tested it.
if you use in

void line2() {
  println("+ line2");
//...
}

you will see if it works or not.
( i see that here with your last code… )

also you see that your “line1” works with changing colors,
what could only be generated by the bad timer call inside “line2”,
so you are already sure that “line2” is executed ( but not working )

i already told you

  • in my first answer
  • in my second post
  • in my test example

that you need separate variables
for the line variables ( if you want see 2 different lines )
and for the timers ( if you want different settings )

so pls. compare your code with my above test code.

1 Like