# Problem with a variable. Don't know where to declare

**URL:** <https://discourse.processing.org/t/problem-with-a-variable-dont-know-where-to-declare/5155>\
**Category:** Coding Questions\
**Created:** [November 3, 2018, 11:40am UTC](https://discourse.processing.org/t/problem-with-a-variable-dont-know-where-to-declare/5155 "2018-11-03T11:40:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bking](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@bking](https://discourse.processing.org/u/bking)\
**Post date:** [November 3, 2018, 11:40am UTC](https://discourse.processing.org/t/problem-with-a-variable-dont-know-where-to-declare/5155/1 "2018-11-03T11:40:49Z")

</div>

Hello,  
I made a program with works when two variables (here two pulsations) are declared before the setup with a constant value.

Like that:

float w1 = 0.1;  
float w2 = 0.11;

But actually I want the second value is a fonction of the first value.

Like that:

// float w1 = 0.1;  
// float w2 = 11/10\*w1;

And in the future the pulsation w1 will depending of the mouse.

Thanks for helping me.

```auto
float w1 = 0.1;
float w2 = 0.11; //11/10*w1;

int x;

int Um=100;
int Vm=100;

int num=1000;

float phiU= 2*PI;
float phiV= 2*PI;

float X1t= Um/2* cos (w1*x+phiU)-Vm/2*cos (w2*x+phiV);
float X2t= Um/2* cos (w1*x+phiU)+Vm/2*cos (w2*x+phiV);

void setup() {
  size (1000, 800);
  frameRate(30);
  colorMode(RGB, 255, 255, 255, 100);
  
  // float w1 = 0.01;
  // float w2 = 11/10*w1;
  //noStroke();
  //fill(0, 50, 0);
  
 
}

void draw() {
  
  background(255);
   calcWave();
  renderWave();
}

void calcWave (){
  
 
   
 
  
  // stroke(val);
  
   X1t= Um/2* cos (w1*x+phiU)-Vm/2* cos (w2*x+phiV);
   X2t= Um/2* cos (w1*x+phiU)+Vm/2* cos (w2*x+phiV);
  
 
  
  
  }

void renderWave() {
  
 // float w1 = 0.1;
 // float w2 = 11/10*w1;
  
   
 
  
     for(int i=1; i<num; i++) { 
   
    float x = float(i/1000)+i;
 
   X1t= Um/2* cos (w1*x+phiU)-Vm/2* cos (w2*x+phiV);
   X2t= Um/2* cos (w1*x+phiU)+Vm/2* cos (w2*x+phiV);
  
 
 
    noStroke();
    fill(0,num,50);
    ellipse (x, X1t+200, 5,5); 
    
    fill(0,50,num);
    ellipse (x, X2t+400, 5,5); 
     
     print(w1,X1t);
     
   }  
    
    }
    
    

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [November 3, 2018, 11:48am UTC](https://discourse.processing.org/t/problem-with-a-variable-dont-know-where-to-declare/5155/2 "2018-11-03T11:48:32Z")

</div>

Hey,

Just declare them like this :

```auto
float w1, w2;
int x;
int Um=100;
int Vm=100;
int num=1000;
float phiU= 2*PI;
float phiV= 2*PI;
float X1t, X2t;

void setup() {
  size (1000, 800);
  frameRate(30);
  colorMode(RGB, 255, 255, 255, 100);
}

void draw(){
  //For example
  w1 = mouseX;
  w2 = 11/10*w1;
  X1t= Um/2* cos (w1*x+phiU)-Vm/2*cos (w2*x+phiV);
  X2t= Um/2* cos (w1*x+phiU)+Vm/2*cos (w2*x+phiV);

  //Draw something...
}

```

So in the draw loop, the values of w1 and w2 are changing all the time (and X1t/X2t too).

PS : it’s a `Processing` coding question, not a `p5.js` one

---

<div class="post-metadata">

**Author:** ![bking](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@bking](https://discourse.processing.org/u/bking)\
**Post date:** [November 3, 2018, 1:15pm UTC](https://discourse.processing.org/t/problem-with-a-variable-dont-know-where-to-declare/5155/3 "2018-11-03T13:15:44Z")

</div>

> [@bking](#):
>
> noStroke(); fill(0,num,50); ellipse (x, X1t+200, 5,5); fill(0,50,num); ellipse (x, X2t+400, 5,5);

Hi Josephhh,  
Thanks for your quick answer but:  
When I declare variables in void draw(); I don’t have good value of w1 and w2, unlike before setup() with w1=0.1 and w2=0.11.  
I don’t understand why?

```auto
float w1, w2;
int x;
int Um=100;
int Vm=100;
int num=1000;
float phiU= 2*PI;
float phiV= 2*PI;
float X1t, X2t;

void setup() {
  size (1000, 800);
  frameRate(30);
  colorMode(RGB, 255, 255, 255, 100);
}

void draw(){
  //For example
  w1 = 0.1;
  w2 = 11/10*w1;
  x=x+1;
  X1t= Um/2* cos (w1*x+phiU)-Vm/2*cos (w2*x+phiV);
  X2t= Um/2* cos (w1*x+phiU)+Vm/2*cos (w2*x+phiV);

  //Draw something...
  noStroke();
    fill(0,num,50);
    ellipse (x, X1t+200, 5,5); 
    
    fill(0,50,num);
    ellipse (x, X2t+400, 5,5); 
     
     println(w1);
     println(w2);
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 3, 2018, 1:28pm UTC](https://discourse.processing.org/t/problem-with-a-variable-dont-know-where-to-declare/5155/4 "2018-11-03T13:28:24Z")

</div>

> [@bking](#):
>
> When I declare variables in void draw(); I don’t have good value of w1 and w2, unlike before setup() with w1=0.1 and w2=0.11.  
> I don’t understand why?

that are constants,

> but for info about constants see  
> [final / Reference / Processing.org](https://processing.org/reference/final.html)

and it does not matter where you declare them  
at the beginning

```auto
float w1 = 0.1;
float w2 = 11/10*w1;

```

or setup or draw,  
i see same picture.

but HA!

```auto
float w1 = 0.1;
float w2 = 11.0/10.0*w1;

```

that produces **different** results!

but as i not understood what you want it to look like  
my version might be more funny as use mouse X and Y and mousewheel

```auto

float w1, w2;
float w12twist = 10.0;
//int x=1;
int Um=100;
int Vm=100;
int num=255;
float phiU=HALF_PI;
float phiV=HALF_PI;
float X1t, X2t;

void setup() {
  size (500, 500);
// frameRate(30);
// colorMode(RGB, 255, 255, 255, 100);
  println("use mouse X for freq, mouse Y for VM phase and mouseWheel for w1/w2");
}

void draw() {
  background(150,150,0);
  //For example
  for (int x=0;x<500;x++){
  w1 = mouseX/500.0/PI;
  w2 = 11.0/w12twist*w1;
  phiV=PI*mouseY/500.0;
  X1t= Um/2*cos(w1*x+phiU) - Vm/2*cos(w2*x+phiV);
  X2t= Um/2*cos(w1*x+phiU) + Vm/2*cos(w2*x+phiV);

  //Draw something...

  noStroke();
  fill(0, num,50);
  ellipse (x,X1t+150, 5, 5); 
  fill(0,50, num);
  ellipse (x,X2t+350, 5, 5); 
  
  //println(w1, X1t, X2t);
  }
}

void mouseWheel(MouseEvent event) {
  w12twist += event.getCount()/5.0;  
}

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [November 3, 2018, 2:15pm UTC](https://discourse.processing.org/t/problem-with-a-variable-dont-know-where-to-declare/5155/5 "2018-11-03T14:15:48Z")

</div>

> [@bking](#):
>
> When I declare variables in void draw(); I don’t have good value of w1 and w2, unlike before setup() with w1=0.1 and w2=0.11.

You are trying to have both constants and variables at the same time, which is not possible.  
If you plan to have w1 depending of the mouse value, you need to affect w1 and w2 in the draw function.  
Otherwise just declare w1 and w2 as constants outside the functions.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 3, 2018, 2:24pm UTC](https://discourse.processing.org/t/problem-with-a-variable-dont-know-where-to-declare/5155/6 "2018-11-03T14:24:09Z")

</div>

> [@josephh](#):
>
> void draw(){ //For example w1 = mouseX; w2 = 11/10\*w1;

your integer calc was the killer

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [November 3, 2018, 2:27pm UTC](https://discourse.processing.org/t/problem-with-a-variable-dont-know-where-to-declare/5155/7 "2018-11-03T14:27:59Z")

</div>

Yes you are right.  
Just had to say :

```auto
w2 = 1.1 * w1;
//or
w2 = 11.0/10 * w1;

```

---

<div class="post-metadata">

**Author:** ![bking](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@bking](https://discourse.processing.org/u/bking)\
**Post date:** [November 4, 2018, 7:51pm UTC](https://discourse.processing.org/t/problem-with-a-variable-dont-know-where-to-declare/5155/8 "2018-11-04T19:51:27Z")

</div>

Yep Guys.  
Thanks you all.  
It works perfectly, now I have to find the formula to simulate, interaction between several pulsation, not only with two.  
I’m looking for a model which simulate several N coupled oscillator and not just two.  
If you have any idea, I will study it.  
Thanks again.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 4, 2018, 11:32pm UTC](https://discourse.processing.org/t/problem-with-a-variable-dont-know-where-to-declare/5155/9 "2018-11-04T23:32:54Z")

</div>

lucky you like it,  
one way would be just to create random noise and add it,  
or more sin waves,  
here you should study the harmonics…  
like how to make a saw or rectangle wave form by adding harmonics,

now from the coding there could be also a interesting way to use  
inverse FFT instead manually adding sinus ( but its actually the same. )

and possibly adding key strokes to start stop adding NOTES  
like a piano.

and when your created wave is very beautiful,  
you might want to hear it too.
