# Variable must change from 3.67 to 3.15

**URL:** https://discourse.processing.org/t/variable-must-change-from-3-67-to-3-15/17929
**Category:** Beginners
**Created:** [February 19, 2020, 11:47am UTC](https://discourse.processing.org/t/variable-must-change-from-3-67-to-3-15/17929 "2020-02-19T11:47:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![JochemTD](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@JochemTD](https://discourse.processing.org/u/JochemTD)
#### Post date: [February 19, 2020, 11:47am UTC](https://discourse.processing.org/t/variable-must-change-from-3-67-to-3-15/17929/1 "2020-02-19T11:47:57Z")

</div>

Hi there!

I need my variable i to go from 3.67 to 3.15 to 3.67 to 3.15 to 3.67 and so on. I’m able to do this just fine from 3.67 to 3.15, however I can’t manage to let it go back to 3.67. This is the code i’m using right now:

```auto
  if (i > 3.16) {
    i = i / 1.001
  }
  
  if(i > 3.67) {
    i = i * 1.001
  }

```

I understand why it’s not working, but I dont know how to fix it. 😕  
pls help

---

<div class="post-metadata">

### Author: ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)
#### Post date: [February 19, 2020, 3:20pm UTC](https://discourse.processing.org/t/variable-must-change-from-3-67-to-3-15/17929/2 "2020-02-19T15:20:37Z")

</div>

You can use this:

`n = 0.52 - abs(frameCount*0.1 % (2*0.52) - 0.52) + 3.15;`

_Eh?_ It’s a function to generate a triangle wave between 0 and 0.52 (the spread of your numbers) with a fixed offset of 3.15; it uses `frameCount` for the time variable; multiplying this by `*0.1` slows down the speed of oscillation to a reasonable amount.

Alternatively,  
`n = 3.67 - abs(frameCount*0.1 % (2*0.52) - 0.52);`

---

<div class="post-metadata">

### Author: ![JochemTD](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@JochemTD](https://discourse.processing.org/u/JochemTD)
#### Post date: [February 19, 2020, 3:24pm UTC](https://discourse.processing.org/t/variable-must-change-from-3-67-to-3-15/17929/3 "2020-02-19T15:24:59Z")

</div>

Thank you so much!  
Clear explanation!

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [February 19, 2020, 6:14pm UTC](https://discourse.processing.org/t/variable-must-change-from-3-67-to-3-15/17929/4 "2020-02-19T18:14:08Z")

</div>

If you were trying to this with only introductory primitives (no modulo, no abs()) another way of expressing this idea in code is to set a step value and flip the sign on the step. This is the way that many bouncing ball implementations work

[https://processing.org/examples/bounce.html](https://processing.org/examples/bounce.html)

…only you are bouncing one variable along a single line, rather than in two dimensions x,y.

```auto
float step = 0.03; // or 0.003415 etc
float max = 3.67;
float min = 3.16;
float i = (min+max)/2.0;

void setup() {
  frameRate(4);
}
void draw() {
  i += step; // move
  if(i>max) { // high bounce
    i=max;
    step = step * -1;
    println("ping");
  }
  if(i<min) { // low bounce
    i=min;
    step = step * -1;
    println("pong");
  }
  println(step, i); // status
}

```
