Making a value increase then decreases

I am trying to make a variable increase from 0 to 255 and then back to 0 then back to 255 and so on. This is the code I have so far and it does not work. If I comment out either of the if statements it will work one direction but I can not get it to reverse in direction.

if (x >= 0);
{
x = x+1;
}

if (x <= 255);
{
x = x-1;
}

hello,

yes this is because both conditions are true at the same time !

i.e. if x = 3 then x IS superior to 0 AND inferior to 255 so x = x + 1 - 1 at each frame

Yes I had realized that… But what piece am I missing to make it switch directions and not just cancel each other out?

You probably need a separate boolean variable to indicate which direction you are going … just from an int you can’t tell whether you should be adding or subtracting.

Or you could use sine on frame number or something like that, though that won’t give exactly the same result (i.e. a sine wave vs. a triangle wave)

you could invert increment like :

int i = 1;

if(x < 0 || x > 255)
{
  i = i * -1;
}

x = x + i;
2 Likes

wont this just make it go to 255 then go to 254 where it will increase again to 255 then flip and go to 254 again?

also as a side note. What is the reason for

{
i = i * -1;
}

Isnt

{
i = -1;
}

The same thing? Considering i = 1

Can you elaborate on this? do you have any sample code?

you can simplify a bit by replacing :

 x = x + (1 * i);

by

x = x + i;

just think about it step by step.

i = i * -1;

is executed each time x < 0 or x > 255

if i = 1 then i switches to i = -1 and the other way around so x is either incremented or decremented

Here is some sample code to do this “palindrome” effect.

https://github.com/SAIC-ATS/ARTTECH-3135/blob/master/Week_0/PalindromeBrightness/sketch/PalindromeBrightness.sketch

1 Like

If you really wanted to use just one variable you could do something like this:

x++;
if(x>255) x = -254;

and then when you want to use the value, use abs(x) to get the absolute value … but it’s not a good way to do it (not easy to follow code)!

Or you can make the code a bit more concise if you use an int that is always 1 or -1 instead of a boolean … e.g. another way to write bakercp’s example:

int brightness = 0;
int brightnessDirection = 1;

void draw(){

        if(brightness >= 255 || brightness <= 0) brightnessDirection *= -1;
        brightness += brightnessDirection;

        // Do something with brightness
}

Though I do recognise that concise isn’t always what you want in example code to assist learning.

Or as I mentioned you can use Sine to fluctuate the value:

brightness = ofMap(sin(ofGetFrameNum() * 0.01),-1,1,0,255);

So we’re getting the sine value of the current frame number multiplied by a value (0.01 here) that controls the speed that we want the brightness value to modulate. Then because Sine always returns a value between -1 and 1 we map that to our desired output range, 0 to 255. This isn’t exactly an answer to your question because it will produce a different result - a more gradual rate of change as it nears the limits instead of a sharp change to a new constant rate. But in a lot of situations it can produce a nicer effect.

2 Likes

Thank you everyone for so many detailed answers. I finally got it to work and am slowly gaining some more understanding of this.

1 Like