Looping counter inside function

Hi, I was trying to initiate a variable inside a function that saves a time reference so I know how much time has passed since it was used (to sync with the BPM). This is my function:

void rainbow(int bpm){
  
  float bpmt = 60000/bpm; //calcula el tiempo de cada beat
  float xt = t-rt2;
  float speed = map(xt,0,bpmt,0,255);
  if(xt>bpmt){rt2=t-int(xt-bpmt);}//calcula el fin y compensa la sobra de bpm
  int hue = int(speed);
  fill(wheel(hue));
  rect(0,0,SIZE_X,SIZE_Y);

}

rt2 here is a global variable, but I need it to be a variable inside the function. What I don´t know is how to initiate rt2 (as rt) inside the function without it being reinitiated in every loop.

t is the value of millis()
xt is the time that has passed since rt was recorded
bpmt is the duration of a beat
rt is the reference time at which a beat “started”

Maybe I’m thinking it wrong, but basically what I want to do is use the passing of time inside a beat to do some actions accordingly. The problem is that I need to do this in various functions that run on the same frame, so I can’t use a global variable and I don’t want to create “placeholder” global variables either (that’s what I’m doing now).

I share below another function that uses the value of xt for some operations.

void senoMask(int bpm){
  
  float a = 0.0;//para dibujar seno
  float b = 0.0;//para modificar la altura
  float inc = TWO_PI/SIZE_X;//incremento vertical entre linea y linea
  
  long xt = t-rt1;
  int bpmt = 60000/bpm*2; //calcula el tiempo de cada beat
  float incb = TWO_PI/bpmt*xt; //calcula el incremento en relaciĂłn a los BPM
  
  for (int i = -1; i < SIZE_X; i++) {
    float y = cos(b)*SIZE_Y;
    float z = cos(a)*y;//alto de la linea que se dibuja
    if(z>0){z=0;}//se asegura de que solo se muestre la altura cuando es positiva
    stroke(1);
    line(i, 0, i, SIZE_Y+z);//dibuja cada linea
    a += inc; //aplica el incremento en a
    b = incb; //aplica el incremento en b
  }
  if(xt>bpmt){rt1=t-(xt-bpmt);}

}
1 Like

Java don’t have static local variables like C. Few languages got such feature AFAIK: :open_mouth:

In order to retain stored values inside variables under Java, they have to be declared as fields. :neutral_face:

So you’re gonna have to wrap your function inside a class in order to have fields under its control. :gift:

1 Like

Hi, thank you. I wasn’t much into Classes yet, I’m a little now (not so much a coding expert). It works like I want now. :rofl::+1:

@spade In many basic Processing example sketches that demonstrate this kind of thing before introducing classes, they simply declare a global variable for the counter at the top of the sketch.

A helpful example sketch for understanding this is Variable Scope:

https://processing.org/examples/variablescope.html

…that global variable actually does become a class variable eventually – it is a variable inside the Processing sketch class, which is not visible to the Processing programmer in the PDE editor before the sketch is compiled and run.

1 Like