Speed in which Brightness changes defined by mouseX

Hi,
I made the brightness (HSB Mode) of an ellipse change up and down between 0 and 100
BUT I want to change the speed in which the brightness changes defined by mouseX.

Here is my code so far, I just don’t know how to use “speed” accuratly. It somehow should add to the steps I guess but clearly the problem is, it adds up more and more, if I do something like step+=speed

float bri = 0; //brightness
float step = 1; //Steps


void setup()
{

  size(200, 200);
  frameRate(24);
  colorMode(HSB, 360, 100, 100, 100);
}

void draw()
{

  background(0, 0, 0);

  //Speed in which brightness should change
  float speed = map(mouseX, 0, 200, 1, 3);

  //brightness goes up and down
  if (bri > 100 || bri < 0) {
    step *= -1;
  }

  bri += step;

  //drawing ellipse
  fill(100, 100, bri);
  ellipse(width/2, height/2, 50, 50);
}

Thanks in advance!

As your code is now, the brightness you use (bri) changes every frame by the amount of step.

And that’s okay! The value of step is either 1 or -1. Thus the brightness goes up and down by 1 each frame.

But you want it to go faster. So you need it to not change by step, but by the calculated amount speed.

It might seem like a good idea to just change your code so that bri changes by the amount of speed every frame. That is, do this:

bri += speed;

But that isn’t going to work! Can you see why?

That’s right - speed is always positive - and so the value in bri only goes up, and not up and down.

Instead, think about a way that you could use both step and speed together to get the effect you want. Imagine that you’re running up and down some stairs. If you’re going up, step is 1. If you’re running down, step is -1. And you do one step at a time.

But you don’t want to always just do one step on the stairs! You want to go faster sometimes. Thus, you need to make use of speed as well!

If you’re skipping up the stairs three at a time, you’re stepping in the positive direction (step is 1), and doing three steps at a time (speed is 3). How does this situation change if you’re going down the stairs instead? Your speed is still 3, three steps at a time, but your direction - step - has changed to -1.

Now, what is step * speed ??!? That’s the value you really want to use, isn’t it?

bri += ( step * speed );
2 Likes

Thank you very much! I was so focused on some much more difficult solutions, that I couldn’t come up with this easy one! Thanks a lot!

1 Like