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